GithubHelp home page GithubHelp logo

saxulum-accessor's Introduction

saxulum-accessor

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

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

saxulum-accessor's People

Contributors

dominikzogg avatar

Watchers

James Cloos avatar

Forkers

scuben mikemeier

saxulum-accessor's Issues

Add tests

  • replace getter by creating it
  • extends class, replace getter by creating it
  • extends class, replace getter by creating it, and call parent

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.