martinezdelariva / hydrator
简化函数的注入和提取。理想适用于扁平或原始数据。
v1.0
2017-11-29 20:39 UTC
Requires
- php: >=7.1
Requires (Dev)
- phpunit/phpunit: ^6.4
This package is not auto-updated.
Last update: 2024-09-15 04:31:29 UTC
README
Hydrator
简化函数的注入和提取。理想适用于扁平对象或原始数据。
安装
使用 Composer 进行安装
composer require martinezdelariva/hydrator
用法
拥有以下类
class Person { private $name; private $age; private $hobbies = []; private $student = false; public function __construct(string $name, int $age, array $hobbies, bool $student) { $this->name = $name; $this->age = $age; $this->hobbies = $hobbies; $this->student = $student; } }
提取
use function Martinezdelariva\Hydrator\extract; $person = new Person("John", 29, ["soccer", "reading"], true); extract($person); // array ( // 'name' => 'John', // 'age' => 29, // 'hobbies' => array ( // 0 => 'soccer', // 1 => 'reading', // ), // 'student' => true, // )
注入
use function Martinezdelariva\Hydrator\hydrate; $values = [ "name" => "Maria", "age" => 30, "hobbies" => ["swimming", "coding"], ]; $person = hydrate(Person::class, $values); // object(Person)#48 (4) { // ["name":"Person":private] => string(5) "Maria" // ["age":"Person":private] => int(30) // ["hobbies":Person":private]=> // array(2) { // [0]=> // string(8) "swimming" // [1]=> // string(6) "coding" // } // ["student":"Person":private] => bool(false)