om / from-array
fromArray 特性允许创建带有初始数据数组的对象实例
v1.0.0
2024-08-01 07:25 UTC
Requires
- php: >=8.0
Requires (Dev)
- nette/tester: *
README
安装
composer require om/from-array
fromArray 特性允许创建带有初始数据数组的对象实例
class Example { use \DataLoader\FromArray; public ?string $a = null; public ?string $b = null; public ?string $c = null; } $example = Example::fromArray( [ 'a' => 'value of A', 'b' => 'value of B', 'c' => 'value of C' ] ); echo json_encode($example, JSON_PRETTY_PRINT);
这将得到以下结果...
{
"a": "value of A",
"b": "value of B",
"c": "value of C"
}
SCHEME 和嵌套
默认对象方案由 SCHEME 常量定义。您可以使用 可调用 函数
<?php require_once __DIR__ . '/../src/FromArray.php'; class SchemeExample { use \DataLoader\FromArray; const SCHEME = [ 'id' => 'intval', 'date' => DateTime::class ]; public ?int $id = null; public ?DateTime $date = null; public bool $alwaysFalse = true; } $example = SchemeExample::fromArray( data: ['id' => '12345', 'alwaysFalse' => true, 'date' => '2020-01-01'], scheme: ['alwaysFalse' => fn() => false] ); var_dump($example->id); // will return integer 12345 var_dump($example->alwaysFalse); // will return false var_dump($example->date->format('c')); // will return date
或者您可以使用 类名
class NestedData { public array $data = []; public function __construct($data) { $this->data = $data; } } class NestedExample { use \DataLoader\FromArray; const SCHEME = ['nested' => NestedData::class]; public ?NestedData $nested = null; } $example = NestedExample::fromArray(['nested' => ['some', 'data', 'here']]); var_dump($example->nested); // will return instance of Nested class
如果您使用的是使用相同特性 object::fromArray() 的类,则将调用 fromArray 函数(具有相同的 $filter),而不是类构造函数。这允许您创建嵌套结构并加载数据
class One { use \DataLoader\FromArray; public ?string $value = null; } class Two { use \DataLoader\FromArray; public ?string $value = null; } class Multiple { use \DataLoader\FromArray; const SCHEME = ['one' => One::class, 'two' => Two::class]; public ?One $one = null; public ?Two $two = null; } $nested = Multiple::fromArray( [ 'one' => ['value' => 'set value for one'], 'two' => ['value' => 'set value for two'] ] );
您也可以这样更改方案
$scheme = Nested::fromArray(data: $data, filter: ['a' => function($data) { return $data; }]);
在这种情况下,$a 中的 $data 将保持不变...
映射
class Example { use \DataLoader\FromArray; const MAPPING = ['anotherId'=>'id']; public ?int $id = null; } $example = Example::fromArray(['anotherId' => 1234]); var_dump($example->id); // will return 1234
值过滤器
class Filter { public ?DateTime $date = null; public string $notDate = ''; } $data = ['date'=> '2017-11-01', 'notDate'=> '2017-11-01']; $example = Filter::fromArray($data, function ($value, $property) { return ($property === 'date') ? new DateTime($value) : $value; }); echo $example->notDate; // will return '2017-11-01' string var_dump($example->date); // will return DateTime object
过滤器在例如从 MongoDb 加载数据时非常有用
function ($value, $property) { if ($property === '_id') return new \MongoId((string)$value); if ($value instanceof \MongoDate) return new \DateTime('@' . $value->sec); return $value; }
测试
composer install composer test # will run Nette Tester
资源
- https://github.com/zendframework/zend-hydrator - Zend Hydrator 类
- https://github.com/makasim/yadm - Mongo ODM
- https://github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md - Doctrine Hydrator