saxulum/saxulum-accessor

This package is abandoned and no longer maintained. No replacement package was suggested.

Saxulum Accessor

2.0.3 2015-09-12 13:44 UTC

README

Build Status Total Downloads Latest Stable Version Scrutinizer Code Quality

Features

  • Contains a accessor trait which allows to register accessors
  • Contains a add accessor, which means you don't have to write simple adders anymore
  • Contains a get accessor, which means you don't have to write simple getters anymore
  • Contains a is accessor, which means you don't have to write simple is anymore
  • Contains a remove accessor, which means you don't have to write simple removers anymore
  • Contains a set accessor, which means you don't have to write simple setters anymore

Requirements

  • PHP 5.4+

Installation

Through Composer as saxulum/saxulum-accessor.

Bootstrap:

AccessorRegistry::registerAccessor(new Add());
AccessorRegistry::registerAccessor(new Get());
AccessorRegistry::registerAccessor(new Is());
AccessorRegistry::registerAccessor(new Remove());
AccessorRegistry::registerAccessor(new Set());

Usage

/**
 * @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 generation

Call the method _generatePhpDoc on the object using it

$one = new One();
$one->_generatePhpDoc()

Arguments

Pros:

  • less own code to write
  • less owm code to debug
  • scalar type hints
  • handles bidirection relations

Cons:

  • @method phpdoc, needs manually call _generatePhpDoc()
  • slower (no benchmark)
  • more complex to debug
  • method_exists does not work

FAQ

Does it work with doctrine orm/odm (proxy)

Yes it does, thx to __call

Does ist work with symfony/property-access (symfony/form)

Yes it does, thx to __get, __set

Does it work with twig

Yes it does, thx to the plain property method call wrapper

Copyright

Contributors

  • Dominik Zogg
  • Patrick Landolt