2017 © Pedro Peláez
 

library model-traits

Traits to reduce boilerplate when creating models.

image

ac/model-traits

Traits to reduce boilerplate when creating models.

  • Thursday, February 6, 2014
  • by americancouncils
  • Repository
  • 5 Watchers
  • 7 Stars
  • 414 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Model Traits

Build Status, (*1)

This is a small convenience library for reusing some common patterns in model classes. Each trait is defined in a section below., (*2)

Installing

Just require "ac/model-traits": "0.2.0" in your composer.json and run composer update ac/model-traits., (*3)

AutoGetterSetterTrait

This will use some reflection magic to allow you to use getters/setters for any property, without having to define them yourself. Of course you are still free to explicitly override any getters/setters for properties where you want or need custom logic. Some assumptions are baked into this:, (*4)

  • public and protected properties are both settable and gettable
  • private properties are only gettable

Example:, (*5)

use AC\ModelTraits\AutoGetterSetterTrait;

class MyModel
{
    use AutoGetterSetterTrait;

    private $id;
    protected $name;
    protected $age;

    public function __construct($id = null)
    {
        $this->id = $id;
    }
}

$m = new MyModel(5);
$m->setName('John');
$m->setAge(23);

echo $m->getId();       //5
echo $m->getName();     //John
echo $m->getAge();      //23

AnnotatedGetterSetterTrait

If you want to more precisely specify which properties will have getters/setters created, you can use the annotation-based trait:, (*6)

use AC\ModelTraits\AnnotatedGetterSetterTrait;
use AC\ModelTraits\Annotation as MT;

class MyModel
{
    use AnnotatedGetterSetterTrait;

    /**
     * @MT\Getter
     */
    private $id

    /**
     * @MT\GetterAndSetter
     */
    private $name;

    /**
     * @MT\GetterAndSetter
     */
    protected $age;
}

Properties can have any combination of access level (public, protected, or private) and accessor methods (getter and/or setter)., (*7)

ArrayFactoryTrait

This adds a static factory method to allow creating an instance of a model with a map of property names to values. It does assume that any exhibiting model does not require custom arguments in its constructor. This will allow setting private properties during creation., (*8)

Example:, (*9)

use AC\ModelTraits\GetterSetterTrait
use AC\ModelTraits\ArrayFactoryTrait;

class MyModel
{
    use ArrayFactoryTrait, GetterSetterTrait;

    private $id;
    protected $name;
    protected $description;
}

$m = MyModel::createFromArray([
    'id' => 5,
    'name' => 'John',
    'age' => 23
]);

echo $m->getId();       //5
echo $m->getName();     //John
echo $m->getAge();      //23

The Versions

06/02 2014

dev-master

9999999-dev

Traits to reduce boilerplate when creating models.

  Sources   Download

MIT

The Requires

 

by Evan Villemez

trait model factory getter setter

31/01 2014

0.2.0

0.2.0.0

Traits to reduce boilerplate when creating models.

  Sources   Download

MIT

The Requires

 

by Evan Villemez

trait model factory getter setter

15/01 2014

0.1.0

0.1.0.0

Traits to reduce boilerplate when creating models.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Evan Villemez

trait model factory getter setter