benrowe / properties
一个简单的运行时包,用于处理类上的属性
1.1.1
2018-06-05 21:21 UTC
Requires
- php: ~7.1
Requires (Dev)
- phpunit/phpunit: ~5.0
- scrutinizer/ocular: ~1.1
- squizlabs/php_codesniffer: ^2.3
README
增加在运行时向类中添加属性的功能。
安装
通过 Composer
$ composer require benrowe/properties
用法
class Sample extends AbstractBase { public function __construct() { // do stuff $this->configureProperties(); // properties now available } /** * This method is automatically called by AbstractBase once * the constructor has finished executing */ public function configureProperties() { $this-> addProperty('simple') $this ->addProperty('complex', 'string', 'default value') ->setter(function($value) { return trim($value); }) ->getter(function($value) { return str_reverse($value); }) ->validate(function ($value) { return strlen($value) > 0; }); } } $sample = new Sample; $sample->simple = 'test'; $sample->undefined = 'newval'; // throws PropertyException "Undefined property 'undefined'" $sample->complex; // 'default value' $sample->complex = ''; // throws PropertyException "property 'complex' invalid value on set" $sample->complex = ' hello world'; echo $sample->complex; //'dlrow olleh';
每个属性都是通过程序定义的(在运行时),并且需要类运行此代码(使用钩子)。
每个属性可以具有
- 名称:string
- 数据类型(s):string
- 验证:string|closure
- 设置器:closure
- 获取器:string
- 默认值(null):mixed
- 值:mixed(取决于数据类型/验证/设置器)
待办事项
- 添加设置器拒绝值的权限,通过抛出异常
- 为设置值构建自定义验证支持
- 而不是为设置器提供一个闭包,提供一个类似 Laravel 风格的验证字符串。
- 添加一个辅助函数,将设置器限制为已知值列表
$this->addProperty('key')->isIn(['foo', 'bar']);
变更日志
有关最近更改的更多信息,请参阅CHANGELOG。
测试
$ composer test
贡献
请参阅CONTRIBUTING 和 CONDUCT 以获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件ben.rowe.83@gmail.com 而不是使用问题跟踪器。
鸣谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅许可证文件。