Library to build objects and extract data out of them

Library to build objects and extract data out of them

README

master develop
Scrutinizer Code Quality Code Coverage Build Status Scrutinizer Code Quality Code Coverage Build Status

SensioLabsInsight

Library to build objects and extract data out of them through an immutable API.

Inject data into an object

use InnmindReflectionReflectionObject;

$refl = (new ReflectionObject($myObject))
    ->withProperty('foo', 'bar')
    ->withProperty('bar', 'baz');
$refl->build();

This simple code will inject both foo and bar into your $myObject following this strategy:

  • look for the setter setFoo()
  • look for a method foo() that has at least one argument
  • use reflection to set value directly on the property

Important: all strategies using methods will use a camelized version of the property, ie: the property foo_bar will lead to setFooBar() and fooBar().

Build a new object

use InnmindReflectionReflectionClass;

class Foo
{
    private $foo;

    public function __construct(string $foo)
    {
        $this->foo = $foo;
    }
}

$foo = (new ReflectionClass(Foo::class))
    ->withProperty('foo', 'bar')
    ->build();

The ReflectionClass uses the ReflectionInstanciator to build the new instance of your class; it’s replaceable by any object implementing the InstanciatorInterface and giving it as the fourth argument of ReflectionClass.

In case the properties you define to be injected can’t be injected through the constructor, it will use internally ReflectionObject to do so.

Extracting data out of an object

use InnmindReflectionReflectionObject;

$properties = (new ReflectionObject($myObject))->extract('foo', 'bar', 'baz');

Here $properties is a collection containing the values of foo, bar and baz that are set in your $myObject.

To do so, it uses 3 strategies:

  • look for a getFoo()
  • look for a foo() (without required parameters)
  • look for a isFoo() (without required parameters)
  • look for a hasFoo() (without required parameters)
  • uses reflection to check if the property foo exists

Important: all strategies using methods will use a camelized version of the property, ie: the property foo_bar will lead to getFooBar() and fooBar().

Source

Add a Comment

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *