aaronbullard / firehose
v1.0.0
2018-11-21 19:10 UTC
Requires
- php: ^7.1
Requires (Dev)
- phpunit/phpunit: ^7.3
This package is auto-updated.
Last update: 2024-10-22 09:17:23 UTC
README
PHP Reflection Class helper to Instantiate, Mutate, and Extract data directly within a class.
Installation
Library
git clone git@github.com:aaronbullard/firehose.git
Composer
composer require aaronbullard/firehose
Testing
composer test
Usage
Firehose/Hydrator uses PHP reflection to bypass protected and private attribute constraints.
This can be useful when trying to instantiate objects from a database query using a mapper pattern. This method also allows you to bypass any validation constraints within the constructor.
When trying to persist the object, anemic getters are unnecessary. Instance properties can be exctracted as key/value pairs for database persistence.
Examples
Given Foo
class Foo { private $bar; protected $baz; private $qux; public function __construct($bar, $baz = null, $qux = null) { $this->bar = $bar; $this->baz = $baz; $this->qux = $qux; } public function bar(){ return $this->bar; } public function baz(){ return $this->baz; } }
Create a new instance
use Firehose\Hydrator; $foo = Hydrator::newInstance(Foo::class, [ 'bar' => 'bar', 'baz' => 'baz' ]); $this->assertInstanceOf(Foo::class, $foo); $this->assertEquals('bar', $foo->bar()); $this->assertEquals('baz', $foo->baz());
Mutating a live instance
$foo = new Foo('bar', 'baz'); Hydrator::mutate($foo, [ 'bar' => 'newBar' ]); $this->assertEquals('newBar', $foo->bar());
Extracting data
$foo = new Foo('bar', 'baz', 'qux'); $data = Hydrator::extract($foo, ['bar', 'baz', 'qux']); $this->assertEquals('bar', $data['bar']); $this->assertEquals('baz', $data['baz']); $this->assertEquals('qux', $data['qux']);
For more examples, see the tests: tests\HydratorTest.php