aaronbullard/firehose
Composer 安装命令:
composer require aaronbullard/firehose
包简介
README 文档
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
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-21