makinacorpus/goat-hydrator

此软件包已被废弃,不再维护。作者建议使用 makinacorpus/generated-hydrator-bundle 软件包。

高级可配置的平面和分层对象水化器

1.0.1 2020-03-04 09:50 UTC

README

基于https://github.com/Ocramius/GeneratedHydrator的高级对象水化器,支持嵌套对象水化。

可选地,可以使用https://github.com/makinacorpus/generated-hydrator无缝替换Ocramius/GeneratedHydrator以支持PHP 5.6。

此软件包提供与Symfony 3.4和4.x兼容的捆绑包。

状态

时间已经过去,PHP 5.6不再被任何地方支持,因此 此软件包不再受支持

请使用makinacorpus/generated-hydrator-bundle替代。

如果需要,此软件包可能会接收一些更新,用于修复错误和安全问题。

入门

为了有效地水化对象,可能需要通过一个离线准备阶段来检查目标对象类,以便稍后进行水化。使用Symfony,这个准备阶段在编译容器时完成。

测试用例场景

在本文档中,我们将处理这个场景,我们定义了以下三个类

class A
{
    /**
     * @var B
     */
    private $b;

    public function getB(): ?B
    {
        return $this->b;
    }
}

class B
{
    private $bar;

    /**
     * @var C
     */
    private $c;

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

    public function getC(): ?C
    {
        return $this->c;
    }
}

class C
{
    private $foo;

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

独立

配置

简单的配置示例,更多文档将在以后提供

$hydratorMap = new HydratorMap(\sys_get_temp_dir().'/hydrator/cache');

// Register the A class
$hydratorMap->addClassConfiguration(new ClassConfiguration(
    A::class,
    [
        'b' => B::class,
    ],
    [],
    HydratorInterface::CONSTRUCTOR_SKIP
));

// Register the B class
$hydratorMap->addClassConfiguration(new ClassConfiguration(
    C::class,
    [
        'c' => C::class,
    ],
    [],
    HydratorInterface::CONSTRUCTOR_SKIP
));

用法

用法很简单


$separator = '__';

$values = [
    'b__bar' => "Hello, "
    'b__c__foo' => "World !",
];

$hydrator = $hydratorMap->get(A::class);
$a = $hydrator->createAndHydrateInstance($values);

现在,调用print $a->getB()->getBar() . $a->getB()->getC()->getFoo();应显示Hello, World !;

Symfony

配置

将捆绑包添加到您的bundles.php文件中(目前尚无flex配方)

return [
    // ...
    Goat\Hydrator\Bridge\Symfony\GoatHydratorBundle::class => ['all' => true],
];

使用goat_hydrator配置密钥

goat_hydrator:

    # Blacklist prevents some classes from being registered as property to
    # hydrate within automatically found properties. These arethe properties
    # target types.
    blacklist:
        - DateTime
        - DateTimeImmutable
        - DateTimeInterface

    classes:

        # Class that are not registered here will not be hierachically
        # hydrated, you MUST register every class you'll need to hydrate.
        App\Domain\Foo:

            # Contructor type, can be "none", "normal" or "late"
            constructor: none

            # Each property defined here will be forcefully hydrated to the
            # given value, which should be an existing class.
            properties:
                role: App\Domain\Role

                # If you encounter issues due to property info component
                # finding invalid or too greedy types, you can disable a
                # property hydration by setting its value to null or false.
                some_other_property: false

用法

用法与上述相同,但为了方便,您可以将HydratorMap类注入到您的控制器或服务中。

行为

每次调用HydratorMap::get()时,将生成一个新实例:如果您正在迭代,例如,SQL结果集,请始终使用相同的实例:每个实例都进行缓存,使其更快。