saxulum / saxulum-accessor
此包已被废弃,不再维护。没有建议的替代包。
Saxulum Accessor
2.0.3
2015-09-12 13:44 UTC
Requires
- php: >=5.4
- saxulum/saxulum-hint: ~1.0
Requires (Dev)
- doctrine/orm: ~2.3
- phpunit/phpunit: 4.0.*
- symfony/form: ~2.3
- symfony/property-access: ~2.3
- twig/twig: ~1.12
This package is not auto-updated.
Last update: 2020-09-22 19:11:19 UTC
README
功能
- 包含一个访问器特质,允许注册访问器
- 包含一个添加访问器,这意味着您无需再编写简单的添加器
- 包含一个获取访问器,这意味着您无需再编写简单的获取器
- 包含一个是访问器,这意味着您无需再编写简单的is
- 包含一个移除访问器,这意味着您无需再编写简单的移除器
- 包含一个设置访问器,这意味着您无需再编写简单的设置器
要求
- PHP 5.4+
安装
通过Composer作为saxulum/saxulum-accessor。
引导
AccessorRegistry::registerAccessor(new Add());
AccessorRegistry::registerAccessor(new Get());
AccessorRegistry::registerAccessor(new Is());
AccessorRegistry::registerAccessor(new Remove());
AccessorRegistry::registerAccessor(new Set());
使用方法
/**
* @method string getName()
* @method $this setName(string $name)
* @method bool isActive()
* @method $this setActive(bool $active)
* @method $this addManies(Many $manies)
* @method Many[] getManies()
* @method $this removeManies(Many $manies)
*/
class One
{
use AccessorTrait;
/**
* @var string
*/
protected $name;
/**
* @var bool
*/
protected $active;
/**
* @var Many[]
*/
protected $manies = array();
protected function _initProps()
{
$this
->_prop((new Prop('name', Hint::STRING))
->method(Get::PREFIX)
->method(Set::PREFIX)
)
->_prop((new Prop('active', Hint::BOOL))
->method(Is::PREFIX)
->method(Set::PREFIX)
)
->_prop((new Prop('manies', 'Many[]', true, 'one', Prop::REMOTE_ONE))
->method(Add::PREFIX)
->method(Get::PREFIX)
->method(Remove::PREFIX)
)
;
}
}
/**
* @method string getName()
* @method $this setName(string $name)
* @method One getOne()
* @method $this setOne(One $name)
*/
class Many
{
use AccessorTrait;
/**
* @var string
*/
protected $name;
/**
* @var One
*/
protected $one;
protected function _initProps()
{
$this
->_prop((new Prop('name', Hint::STRING))
->method(Get::PREFIX)
->method(Set::PREFIX)
)
->_prop((new Prop('one', 'One', true, 'manies', Prop::REMOTE_MANY))
->method(Add::PREFIX)
->method(Get::PREFIX)
->method(Remove::PREFIX)
)
;
}
}
$one = new One();
$one
->setName('one')
->setActive(true)
;
$many = new Many();
$many
->setName('many')
->setOne($one)
;
$one->getName(); // return: string 'one'
$one->isActive(); // return: bool true
$one->getManies(); // return: an array with one instance of 'Many'
PhpDoc生成
在使用的对象上调用_generatePhpDoc
方法
$one = new One();
$one->_generatePhpDoc()
参数
优点
- 编写自己的代码更少
- 调试自己的代码更少
- 标量类型提示
- 处理双向关系
缺点
@method
phpdoc,需要手动调用_generatePhpDoc()
- 较慢(没有基准测试)
- 更难调试
method_exists
不起作用
常见问题解答
它是否与doctrine orm/odm (代理)兼容?
是的,多亏了__call
ist 是否支持与 symfony/property-access (symfony/form) 一起使用
它是否支持 twig
是的,它可以,多亏了直接的 属性方法调用包装器
版权
- Dominik Zogg dominik.zogg@gmail.com
贡献者
- Dominik Zogg
- Patrick Landolt