mrkrstphr/hydrotron

Hydrotron 帮助实现 PHP 对象的 hydration

2.1.0 2017-04-19 12:56 UTC

This package is auto-updated.

Last update: 2024-09-11 04:57:15 UTC


README

Hydrotron 帮助实现 PHP 对象的 hydration

安装

composer install mrkrstphr/hydrotron

使用

当数组键存在时,调用一个或多个回调函数

$hydro = new Hydrotron(['foo' => 'bar']);
$hydro->when('foo', $callback, [$object, 'method']);

当数组键存在时,实例化一个对象,并使用该对象调用一系列回调函数

$hydro = new Hydrotron(['foo' => 'bar', 'bizz' => 'buzz']);
$hydro->instantiateWhen('foo', MyClass::class, $callback);

instantiateWhen() 使用 Instantiator,它会分析类的构造函数参数,并将 Hydrotron 数组中匹配这些参数名称的键的值传递过去。

所以如果 MyClass 看起来像这样

class MyClass {
    public function __construct($foo, $bizz) {}
}

MyClass 将会使用 $foo = 'bar'$bizz = 'buzz' 来实例化。如果数组中没有找到参数名称,将传递 null

致谢

Hydrotron 受 Keyper 的启发。