2017 © Pedro Peláez
 

yii2-extension yii2-fluent

Yii2 behavior that implements fluent interface for changing component's properties

image

consik/yii2-fluent

Yii2 behavior that implements fluent interface for changing component's properties

  • Friday, February 3, 2017
  • by consik
  • Repository
  • 0 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Yii2 Fluent Component Behavior

Behavior that implements fluent interface methods for component attributes., (*1)

Latest Stable Version Total Downloads License, (*2)

Installation

The preferred way to install this extension is through composer., (*3)

Either run, (*4)

composer require consik/yii2-fluent

or add, (*5)

"consik/yii2-fluent": "^1.0"

FluentComponentBehavior class description

Properties

  • array $attributes = []

Associative or simple array of attributes that can be changed using fluent interface. For associative definition key is alias for attributes methods; Value is always component attribute name;, (*6)

  • string $initArraysIfEmpty = true

Defines need bahavior to initialize property as array if it's empty and !is_array() when calling array-access fluent methods(like add*Property*($item)), (*7)

Public methods

Universal fluent methods for owner component, (*8)

  • $this setProperty(string $property, mixed $value)

Sets value to component property, (*9)

  • $this unsetProperty(string $property)

Unsets component property, (*10)

  • $this addItemTo(string $arrName, mixed $value, bool $initOnEmpty = true)

Adds $item to array property with name $arrName;, (*11)

Throws exception if $component->{$arrName} is !empty() && !is_array();, (*12)

initializes $component->{$arrName} as empty array if ($initOnEmpty && empty($component->{$arrName})) ;, (*13)

Examples

Short definition of behavior. Behavior will implement all available fluent methods for ALL component attributes

<?php
use consik\yii2fluent\FluentComponentBehavior;

class Test extends \yii\base\Component
{
    public $isNew;
    public $comments;

    public function behaviors()
     {
         return [
            FluentComponentBehavior::className()
         ];
     }
}

Available fluent methods for this definition: * (new Test()) * ->setProperty($name, $value) * ->unsetProperty($name) * ->addItemTo($arrName, $arrayItem) * ->setIsNew($value) * ->unsetIsNew() * ->addIsNew($arrayItem) * ->setComments($value) * ->unsetComments() * ->addComments($arrayItem), (*14)

Extended definition of behavior, for enumerated properties, with alias for one of property.

<?php
use consik\yii2fluent\FluentComponentBehavior;

class Test extends \yii\base\Component
{
    public $isNew;
    public $comments;
    public $fluentUnaccessable;

    public function behaviors()
     {
         return [
            [
                'class' => FluentComponentBehavior::className(),
                'attributes' => [
                    'new' => 'isNew',
                    'comments'
                ]
         ];
     }
}

Available fluent methods for this definition: * (new Test()) * ->setProperty($name, $value) * ->unsetProperty($name) * ->addItemTo($arrName, $arrayItem) * ->setNew($value) * ->unsetNew() * ->addNew($arrayItem) * ->setComments($value) * ->unsetComments() * ->addComments($arrayItem), (*15)

Be helpful!

Don't forget about other developers and write comments for your classes!, (*16)

Basic comment for all components with attached FluentInterfaceBehavior, (*17)

@method $this setProperty(string $name, $value)
@method $this unsetProperty(string $name)
@method $this addItemTo(string $arrName, mixed $item, bool $initOnEmpty = true)
<?php
/*
 * Class YourClass
 * @method $this setProperty(string $name, $value)
 * @method $this unsetProperty(string $name)
 * @method $this addItemTo(string $arrName, mixed $item, bool $initOnEmpty = true)
 */
 class YourClass extends \yii\base\components { ... }

And, please, don't forget writing comments about defined fluent methods for your component properties!!!, (*18)

Best regards, Sergey Poltaranin., (*19)

The Versions

03/02 2017

dev-master

9999999-dev

Yii2 behavior that implements fluent interface for changing component's properties

  Sources   Download

MIT

The Requires

 

by Sergey Poltaranin (Consik)

component yii2 model behavior chain property fluent method

23/12 2016

1.0.0

1.0.0.0

Yii2 behavior that implements fluent interface for changing component's properties

  Sources   Download

MIT

The Requires

 

by Sergey Poltaranin (Consik)

component yii2 model behavior chain property fluent method