Yii2 Fluent Component Behavior
Behavior that implements fluent interface methods for component attributes., (*1)
, (*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
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)