2017 © Pedro Peláez
 

lithium-library li3_behaviors

Model behaviors for the li₃ PHP framework

image

unionofrad/li3_behaviors

Model behaviors for the li₃ PHP framework

  • Thursday, July 12, 2018
  • by davidpersson
  • Repository
  • 7 Watchers
  • 3 Stars
  • 511 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 14 Versions
  • 18 % Grown

The README.md

li₃ Behaviors

This library provides base classes for implementing model behaviors. Model behaviors provide a simple way to extend models. This pattern allow common logic to be encapsulated inside behaviors for keeping models light and composed only by its own business logic., (*1)

Installation

The preferred installation method is via composer. You can add the library as a dependency via:, (*2)

composer require unionofrad/li3_behaviors

li₃ plugins must be registered within your application bootstrap phase as they use a different (faster) autoloader., (*3)

Libraries::add('li3_behaviors')

The official manual has more information on how to register the plugin with the app or use alternative installation methods (i.e. via Git)., (*4)

Usage

Managing and Loading Behaviors

First to add the ability of using behaviors in a model, use the behaviors trait in your model. After that define all behaviors you plan to use in the $_actsAs property of the model class., (*5)

// ...
class Posts extends \lithium\data\Model {

   use li3_behaviors\data\model\Behaviors;

   protected $_actsAs = [
       'Sluggable' => ['field' => 'slug', 'label' => 'title']
   ];

   // ...

The Behaviors trait also makes some static methods available in the model, which allows to manage behaviors as follows., (*6)

// Bind the sluggable behavior with configuration.
Posts::bindBehavior('Sluggable', ['field' => 'slug', 'label' => 'title']);

// Accessing configuration.
Posts::behavior('Sluggable')->config();
Posts::behavior('Sluggable')->config('field');

// Updating configuration.
Posts::behavior('Sluggable')->config('field', 'alt');

// Unbinding it again.
Posts::unbindBehavior('Sluggable');

Creating a Behavior

Now that we are able to load and manage behaviors we can create our own behavior which must extend the Behavior base class. In the following example we create a Sluggable behavior in extensions/data/behavior/Sluggable.php., (*7)


namespace app\extensions\data\behavior; use lithium\util\Inflector; class Sluggable extends \li3_behaviors\data\model\Behavior { protected static $_defaults = [ 'field' => 'slug', 'label' => 'title' ]; protected static function _filters($model, $behavior) { Filters::apply($model, 'save', function($params, $next) use ($behavior) { $params['data'][$behavior->config('field')] = static::_generate( $params['data'][$behavior->config('label')] ); return $next($params); }); } protected static function _generate($value) { return strtolower(Inflector::slug($value)); } }

Behavior Configuration

The configuration of each behavior can be accessed from within the behavior via config(). By default configuration for the behavior will be set automatically using the user provided configuration from the $_actsAs property of the model and the defaults provided in the behavior as $_defaults., (*8)

The defaults are merged with the provided configuration using simple array addition ($config += $defaults). If you want to change the way configuration is merged read further., (*9)

Providing Custom Configuration Logic

Behaviors often come with different requirements towards configuration. In some cases just a 1-dimensional array needs to be merged in other cases nested multi-dimensional arrays must be merged or even normalized in a custom way., (*10)

That's why merging the defaults with the provided configuration can be controlled easily by yourself - the implementer. By default we do a simple one-dimensional merge adding defaults and configuration to eachother. To control configuration merging overwrite the _config() method of the base class., (*11)

In the following example we will normalize certain configuration options while merging with the behavior's defaults., (*12)

// ...

class Serializable extends \li3_behaviors\data\model\Behavior {

    protected static $_defaults = [
        'fields' => []
    ];

    protected static function _config($model, $behavior, $config, $defaults) {
        $config += $defaults;
        $config['fields'] = Set::normalize($config['fields']);

        foreach ($config['fields'] as $field => &$pass) {
            if (!$pass) {
                $pass = 'json';
            }
        }
        return $config;
    }

    // ...

The _config() method gets the configuration and the defaults as defined in $_defaults as the 3rd and 4th parameters. The method must finally return the configuration that should be used for the behavior instance., (*13)

Exposing Static Methods to the Model

Any public static method present in the behavior is automically exposed on the model. This allows for adding methods to the model easily. Each static method that is exposed gets the name of the current model class as its first and the instance of the behavior as a second parameter., (*14)

This is useful if you i.e. need to query the model for results or when you want to retrieve configuration from the behavior., (*15)

The example below shows how we expose a token generation method to the model., (*16)

// ...

class TokenGenerator extends \li3_behaviors\data\model\Behavior {

    protected static $_defaults = [
        'short' => false
    ];

    // Generates a random token either short (8 chars) or long (16 chars) and
    // returns it. Default expiration is one year.
    public static function token($model, $behavior) {
        $token = substr(md5(Random::generate(32)), 0, $behavior->config('short') ? 8 : 16);
        $expires = date('Y-m-d H:i:s', strtotime('+1 year'));

        return compact('token', 'expires');
    }

    // ...

Exposing Instance Methods to the Model

Analog to exposing static methods, instance methods can also be exposed to the model easily. Any concrete method implemented in the behavior will be exposed. Each behavior method will in addition to the $entity parameter also receive the name of the model and an instance of the behavior as a second parameter i.e. to access configuration., (*17)

// ...

class Publishable extends \li3_behaviors\data\model\Behavior {

    protected static $_defaults = [
        'field' => 'is_published'
    ];

    public function publish($model, $behavior, $entity) {
        $field = $behavior->config('field');
        $entity->{$field} = true;
    }

    // ...

Dynamically Adding Model Instance Methods

Sometimes you need to dynamically add methods to a model instance. I.e. when a field name of a behavior is user configurable and needs to be added as a method on the entity., (*18)

This can be achived by overwriting the _methods() method and returning an array of methods keyed by their alias on the model instance., (*19)

// ...

class Taggable extends \li3_behaviors\data\model\Behavior {
    // ...

    protected static function _methods($model, $behavior) {
        return [
            $behavior->config('field') => function() { /* ... */  }
        ]
    }

    // ...

The above exemplaric behavior would then enable the following methods on each entity returned from the model., (*20)

Posts::bindBehavior('Taggable', ['field' => 'taxonomy']);
$item = Posts::create();
$item->taxonomy();

Attaching Filters to the Model

To modify existing model methods, filters should be used. If your behavior needs to use filters a good place to attach them is the behavior's _filters() method. Overwrite it to attach filters to the model during initialization phase., (*21)

// ...

class Timestamp extends \li3_behaviors\data\model\Behavior {
    // ...

    protected static function _filters($model, $behavior) {
        Filters::apply($model, 'save', function($params, $next) use ($behavior) {
            $params['data'] = static::_timestamp($behavior, $params['entity'], $params['data']);

            return $next($params);
        });
    }

    protected static function _timestamp($behavior, $entity, $data) {
        // ...
    }

    // ...

Attaching Finders to the Model

To add finders to the model's find method, finders should be used. If your behavior needs to use finders a good place to attach them is the behavior's _finders() method. Overwrite it to add finders to the model during initialization phase., (*22)

// ...

class Taggable extends \li3_behaviors\data\model\Behavior {
    // ...

    protected static function _finders($model, $behavior) {
        $model::finder('tag', function($params, $next) use ($behavior) {
            // ...
            return $next($params);
        });
    }

    // ...

Copyright 2010 Union of RAD. All rights reserved. This library is distributed under the terms of the BSD 3-Clause License. The full license text can be found in the LICENSE.txt file., (*23)

Credits for previous Implementations

  • David Persson, https://github.com/davidpersson/li3_behaviors
  • Nate Abele, https://github.com/nateabele/li3_behaviors
  • Simon Jaillet, https://github.com/jails/li3_behaviors

The Versions

12/07 2018

2.1.x-dev

2.1.9999999.9999999-dev

Model behaviors for the li₃ PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

plugin model behavior lithium li3

12/07 2018

v2.1.2

2.1.2.0

Model behaviors for the li₃ PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

plugin model behavior lithium li3

04/07 2018

v2.1.1

2.1.1.0

Model behaviors for the li₃ PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

plugin model behavior lithium li3

02/05 2018

v2.1.0

2.1.0.0 https://github.com/unionofrad/li3_behaviors

Model behaviors for the li₃ PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

plugin model behavior lithium li3

30/03 2017

2.0.x-dev

2.0.9999999.9999999-dev https://github.com/unionofrad/li3_behaviors

Model behaviors for the li₃ PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

plugin model behavior lithium li3

30/03 2017

dev-master

9999999-dev https://github.com/unionofrad/li3_behaviors

Model behaviors for the li₃ PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

plugin model behavior lithium li3

26/03 2017

v2.0.2

2.0.2.0 https://github.com/unionofrad/li3_behaviors

Model behaviors for the li₃ PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

plugin model behavior lithium li3

16/07 2016

v2.0.1

2.0.1.0 https://github.com/unionofrad/li3_behaviors

Model behaviors for the li₃ PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

plugin model behavior lithium li3

06/11 2015

v2.0.0

2.0.0.0 https://github.com/davidpersson/li3_behaviors

Model behaviors for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php model behavior lithium li3

05/09 2015

v2.0.0-beta

2.0.0.0-beta https://github.com/davidpersson/li3_behaviors

Model behaviors for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php model behavior lithium li3

24/03 2015

v2.0.0-alpha2

2.0.0.0-alpha2 https://github.com/davidpersson/li3_behaviors

Model behaviors for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php model behavior lithium li3

24/03 2015

v2.0.0-alpha

2.0.0.0-alpha https://github.com/davidpersson/li3_behaviors

Model behaviors for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php model behavior lithium li3

06/01 2015

v1.1.0

1.1.0.0 https://github.com/davidpersson/li3_behaviors

Model behaviors for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php model behavior lithium li3

02/04 2014

v1.0.0

1.0.0.0 https://github.com/davidpersson/li3_behaviors

Model behaviors for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php model behavior lithium li3