saxulum/saxulum-accessor

此包已被废弃,不再维护。没有建议的替代包。

Saxulum Accessor

2.0.3 2015-09-12 13:44 UTC

README

Build Status Total Downloads Latest Stable Version Scrutinizer Code Quality

功能

  • 包含一个访问器特质,允许注册访问器
  • 包含一个添加访问器,这意味着您无需再编写简单的添加器
  • 包含一个获取访问器,这意味着您无需再编写简单的获取器
  • 包含一个是访问器,这意味着您无需再编写简单的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) 一起使用

是的,它可以,多亏了 __get__set

它是否支持 twig

是的,它可以,多亏了直接的 属性方法调用包装器

版权

贡献者

  • Dominik Zogg
  • Patrick Landolt