2017 © Pedro Peláez
 

library composite-utils

Composite Object Model Utilities

image

spaark/composite-utils

Composite Object Model Utilities

  • Wednesday, December 20, 2017
  • by EmilyShepherd
  • Repository
  • 1 Watchers
  • 0 Stars
  • 105 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 6 Versions
  • 3 % Grown

The README.md

Composite Utils

![Build Status][travis-img] ![Scrutinizer Code Quality][scrutinizer-img] Code Coverage, (*1)

A set of utilities which make interacting with Composite Objects easier, (*2)

The problem

Composite objects are normally little more than a collection of properties with meaningful names. While this is definitely better than just using an array, this normally requires a long set of getters and setters; these often do nothing but move the value around:, (*3)

public function setProperty($value)
{
    $this->property = $value;
}

public function getProperty()
{
    return $this->property;
}

Bloat, bloat, bloat!, (*4)

That's nine lines of code to reimplement what is just $object->property = $value. At best, these lines might enforce some form of datatype value:, (*5)

public function setProperty(RequiredPropertyType $value)
{
    $this->property = $value;
}

This problem also exists in constructors, how often have you seen something like this:, (*6)

public function __construct($value1, $value2, $value3, $value4, $value5)
{
    // Set values
    $this->value1 = $value1;
    $this->value2 = $value2;
    $this->value3 = $value3;
    $this->value4 = $value4;
    $this->value5 = $value5;

    // Init Values
    $this->value6 = new Collection();
    $this->value7 = new HashMap();
}

The solution

This library introduces a set of tools to remove the need to composite getters, setters and bloated constructors without giving up your control over property access and data type enforcement., (*7)

class MyAwesomeComposite
{
    use AutoConstructTrait;
    use PropertyAccessTrait;

    /**
     * @var int
     */
    protected $property = 42;

    /**
     * @var string
     * @readable
     * @writable
     */
    protected $stringProperty = 'some value';

    /**
     * @var DataType
     * @readable
     * @construct required
     */
     protected $iNeedThis;

    /**
     * @var ArrayObject
     * @readable
     * @construct new
     */
    protected $collection;
}

That's it. Nothing more required! All the properties other than $property can be accessed when required, and $collection will be automatically constructed for you. Although you can't see it, this entity also has a constructor present which requires... you guessed it... The $iNeedThis property, and it needs to be a DataType. $stringProperty is also writable, if anything other than a string is passed to it, it will be automatically cast to one if possible:, (*8)


$class = new MyAwesomeComposite(); // Fails, $iNeedThis is required $iNeedThis = new OtherDataType(); $class = new MyAwesomeComposite(); // Fails, $iNeedThis should be a DataType $iNeedThis = new DataType(); $class = new MyAwesomeComposite($iNeedThis); // All good! var_dump($class->property); // Fails, not readable var_dump($class->stringProperty); // string(10) "some value" $class->stringProperty = 00000000123; var_dump($class->stringProperty); // string(3) "123" $class->iNeedThis = new DataType(); // Fails, not writable var_dump($class->collection); // ArrayObject { } var_dump($class->iNeedThis === $iNeedThis); // true

The Versions

20/12 2017

dev-master

9999999-dev https://www.spaark.io/

Composite Object Model Utilities

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

framework php library spaark

21/03 2017

dev-feature/generics

dev-feature/generics https://www.spaark.io/

Composite Object Model Utilities

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

framework php library spaark

11/03 2017

v1.1.0

1.1.0.0 https://www.spaark.io/

Composite Object Model Utilities

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

framework php library spaark

18/02 2017

dev-feature/set-collection

dev-feature/set-collection https://www.spaark.io/

Composite Object Model Utilities

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

framework php library spaark

14/02 2017

v1.0.1

1.0.1.0 https://www.spaark.io/

Composite Object Model Utilities

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

framework php library spaark

13/02 2017

v1.0.0

1.0.0.0 https://www.spaark.io/

Composite Object Model Utilities

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

framework php library spaark