symplify/easy-hydrator

此包已被废弃,不再维护。未建议替代包。

使用PHP 7.4和构造函数注入轻松将数组转换为对象

安装数: 81,388

依赖项: 1

建议者: 0

安全: 0

星星: 31

关注者: 4

分支: 0

类型:symfony-bundle

This package is auto-updated.

Last update: 2021-10-11 12:54:29 UTC


README

Downloads total

  • 简单!
  • PHP 7.4支持
  • 构造函数注入支持
  • 自动解析DateTimeInterface字符串值
  • 根据参数类型声明自动重类型
  • 支持嵌套对象
  • 可自定义对象创建
  • 缓存

安装

composer require symplify/easy-hydrator

添加到config/bundles.php

return [
    Symplify\EasyHydrator\EasyHydratorBundle::class => [
        'all' => true,
    ],
    Symplify\SimplePhpDocParser\Bundle\SimplePhpDocParserBundle::class => [
        'all' => true,
    ],
];

用法

具有构造函数注入的值对象

namespace App\ValueObject;

use DateTimeInterface;

final class Person
{
    private string $name;

    private int $age;

    private DateTimeInterface $metAt;

    public function __construct(string $name, int $age, DateTimeInterface $metAt)
    {
        $this->name = $name;
        $this->age = $age;
        $this->metAt = $metAt;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getAge(): int
    {
        return $this->age;
    }

    public function getMetAt(): DateTimeInterface
    {
        return $this->metAt;
    }
}

使用如下数组形式的hydrator

namespace App\Repository;

use App\ValueObject\Person;
use Symplify\EasyHydrator\ArrayToValueObjectHydrator;

final class HumanRepository
{
    /**
     * @var ArrayToValueObjectHydrator
     */
    private $arrayToValueObjectHydrator;

    public function __construct(ArrayToValueObjectHydrator $arrayToValueObjectHydrator)
    {
        $this->arrayToValueObjectHydrator = $arrayToValueObjectHydrator;
    }

    public function getPerson(): Person
    {
        return $this->arrayToValueObjectHydrator->hydrateArray([
            'name' => 'Tom',
            // will be retyped to int
            'age' => '30',
            // will be retyped to DateTimeInterface
            'metAt' => '2020-02-02',
        ], Person::class);

        // ...
    }
}

多个值对象?

这是如何填充一个项的

$singlePersonAsArray = [
    'name' => 'Tom',
    // will be retyped to int
    'age' => '30',
    // will be retyped to DateTimeInterface
    'metAt' => '2020-02-02',
]);

/** @var Person $person */
$person = $this->arrayToValueObjectHydrator->hydrateArray($singlePersonAsArray, Person::class);

但如何填充多个项呢?

$manyPersonsAsArray = [];
$manyPersonsAsArray[] = [
    'name' => 'Tom',
    // will be retyped to int
    'age' => '30',
    // will be retyped to DateTimeInterface
    'metAt' => '2020-02-02',
];

$manyPersonsAsArray[] = [
    'name' => 'John',
    // will be retyped to int
    'age' => '25',
    // will be retyped to DateTimeInterface
    'metAt' => '2019-12-31',
];

/** @var Person[] $persons */
$persons = $this->arrayToValueObjectHydrator->hydrateArrays($manyPersonsAsArray, Person::class);

可选值

如果对象具有可选参数,并且数据中没有提供其一些值,则在填充对象中会使用默认值。

class MyObject
{
    private string $foo;

    private string $bar;

    public function __construct(string $foo, string $bar = 'bar')
    {
        $this->foo = $foo;
        $this->bar = $bar;
    }

    public function getFoo(): string
    {
        return $this->foo;
    }

    public function getBar(): string
    {
        return $this->bar;
    }
}

$data = [
    'foo' => 'foo',
];

$object = $this->arrayToValueObjectHydrator->hydrateArray($data, MyObject::class);
// bar
$object->getBar();

缺少构造函数数据

当未提供必需构造函数参数的数据时,将抛出Symplify\EasyHydrator\Exception\MissingDataException异常。


报告问题

如果您遇到错误或希望请求新功能,请访问Symplify monorepo问题跟踪器

贡献

此包的源代码包含在Symplify monorepo中。我们欢迎在symplify/symplify上为此包做出贡献。