travy/cake_behaviors

This package is abandoned and no longer maintained. No replacement package was suggested.

Model behaviors for CakePHP

Installs: 306

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 1

Type:cakephp-plugin

1.0.0 2016-05-30 07:04 UTC

This package is not auto-updated.

Last update: 2016-12-30 12:11:31 UTC


README

Build Status Software License

Provides useful CakePHP 3 model behaviors for use in applications. Versions of CakePHP prior to 3.x are not supported.

Installation

1) Add cake_behaviors to your composer.json file

composer self-update && composer require 'travy/cake_behaviors:dev-master'

2) Load the plugin by modifying the config/bootstrap.php file within your CakePHP 3 project and adding the following line with your other plugins

Plugin::load('CakeBehaviors');

3) Add any wanted behaviors to your table by specifying CakeBehaviors.<<behaviorName>> in the desired Table class's initialization method

public function initialize(array $config)
{
...

$this->addBehavior('CakeBehaviors.Addressable');

...
}

Provided Behaviors

At the moment cake_behaviors only supports one behavior but more are in the works. The one implemented behavior is the AddressableBehavior, but the others referenced are in the works and should be supported soon.

AddressableBehavior

The AddressableBehavior assumes that a Table has the fields address, city, state and zipcode. The fields have a rule which states that all of the fields can either be set or empty but not a combination of the two. In otherwords you will not be able to have the address field have a value of say '101 Wicks Road' while city, state and zipcode are either empty strings or null. The insert/update operation will be aborted.

The behavior also handles input validation which forces both address and city to be strings of length 2 to 255 characters. Zipcode is assumed to be a string of 5 to 10 characters in length and state will only ever contain 2 characters. The value of state will also be validated to ensure that it is a valid 2 character uppercase string. The values for states can be acquired using the CakeBehaviors\Utils\Enums\StatesEnumeration class.

SoftDeletableBehavior <Not Implemented>

The SoftDeletableBehavior class will provide a mechanism for avoiding true deletes within the database. Each table which uses this Behavior will be expected to have the field active which will be a single character string that will contain either 'Y' or 'N'.

Finder methods will be provided for assisting with querying only entities that are marked as active.

This behavior is not supported as of May 30, 2016 but I hope to implement it soon.

HistoricalBehavior <Not Implemented>

The HistoricalBehavior will allow users to track changes made to specific table fields within the database. The behavior will assist in tracking of all or a subset of each CRUD operation as defined by the user. Not much more information is available at this time.

This behavior is not supported as of May 30, 2016 but I hope to implement it soon.

Utility Classes

The following utility classes were originally created for use within the behavior classes but are perfectly suited for use without separate from them as well.

StatesEnumeration

The states enumeration object provides a mechanism for validating US state codes. This is a static class where each state is referenced with its full name as a fully uppercased constant:

echo StatesEnumeration::NEW_YORK; //  > 'NY'
echo StatesEnumeration::CALIFORNIA; //  > 'CA'

In addition to the enumerated list of states an array can be created which provides a user friendly full name as the key with the value still set to the abbreviated result.

var_dump(StatesEnumeration::getStatesArray(); //  ['Alabama' => 'AL', 'Alaska' => 'AK', ..., 'New York' => 'NY', ...];

Values can be checked if they are valid states by utilizing the get($name), getByName($name) and getByOrdinal($ordinal) functions.

StatesEnumeration was built using the package marc-mabe/php-enum found here on github. Please read the documentation there for more Enumeratable methods or if you would like to add Enumerations to your projects.