consik / yii2-fluent
实现组件属性流畅接口的Yii2行为
1.0.0
2016-12-23 04:44 UTC
Requires
- php: ^5.6 || ^7.0
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-14 19:58:35 UTC
README
实现组件属性流畅接口方法的行为。
安装
安装此扩展的首选方法是通过 composer。
运行以下命令之一:
composer require consik/yii2-fluent
或添加
"consik/yii2-fluent": "^1.0"
FluentComponentBehavior 类描述
属性
array $attributes = []
可以使用流畅接口更改的属性关联或简单数组。对于关联定义,key
是属性方法的别名;值始终是组件属性名称;
string $initArraysIfEmpty = true
定义是否需要行为初始化属性为数组,如果它为空且调用数组访问流畅方法(如 add*Property*($item)
)时 !is_array();
公共方法
针对所有者组件的通用流畅方法
$this setProperty(string $property, mixed $value)
将值设置到组件属性
$this unsetProperty(string $property)
取消设置组件属性
$this addItemTo(string $arrName, mixed $value, bool $initOnEmpty = true)
将 $item
添加到具有名称 $arrName
的数组属性中;
如果 $component->{$arrName}
是 !empty() && !is_array()
,则抛出异常;
如果 ($initOnEmpty && empty($component->{$arrName}))
,则初始化 $component->{$arrName}
为空数组;
示例
行为的简短定义。行为将实现所有可用的流畅方法,用于所有组件属性
<?php use consik\yii2fluent\FluentComponentBehavior; class Test extends \yii\base\Component { public $isNew; public $comments; public function behaviors() { return [ FluentComponentBehavior::className() ]; } }
此定义可用的流畅方法
- (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' ] ]; } }
此定义可用的流畅方法
- (new Test())
- ->setProperty($name, $value)
- ->unsetProperty($name)
- ->addItemTo($arrName, $arrayItem)
- ->setNew($value)
- ->unsetNew()
- ->addNew($arrayItem)
- ->setComments($value)
- ->unsetComments()
- ->addComments($arrayItem)
有帮助!
不要忘记其他开发者,并为你的类编写注释!
所有具有附加 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 { ... }
并且,请别忘了为你的组件属性定义的流畅方法编写注释!!!
最好的祝愿,谢尔盖·波尔塔拉宁。