Behavior that implements fluent interface methods for component attributes.
The preferred way to install this extension is through composer.
Either run
composer require consik/yii2-fluent
or add
"consik/yii2-fluent": "^1.0"
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;
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)
)
Universal fluent methods for owner component
$this setProperty(string $property, mixed $value)
Sets value to component property
$this unsetProperty(string $property)
Unsets component property
$this addItemTo(string $arrName, mixed $value, bool $initOnEmpty = true)
Adds $item
to array property with name $arrName
;
Throws exception if $component->{$arrName}
is !empty() && !is_array()
;
initializes $component->{$arrName}
as empty array if ($initOnEmpty && empty($component->{$arrName}))
;
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)
<?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)
Don't forget about other developers and write comments for your classes!
Basic comment for all components with attached FluentInterfaceBehavior
@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!!!
Best regards, Sergey Poltaranin.