spaceonfire/laminas-hydrator-bridge

此包已被废弃且不再维护。作者建议使用getwarp/laminas-hydrator-bridge包代替。

为Laminas Hydrator与一些spaceonfire库提供集成

2.5.2 2021-09-25 09:48 UTC

This package is auto-updated.

Last update: 2022-06-12 16:28:21 UTC


README

Latest Version on Packagist Software License Total Downloads Code Coverage

Laminas Hydrator与一些spaceonfire库提供集成。

安装

通过Composer

$ composer require spaceonfire/laminas-hydrator-bridge

使用方法

use spaceonfire\LaminasHydratorBridge\StdClassHydrator;
use spaceonfire\LaminasHydratorBridge\NamingStrategy\AliasNamingStrategy;
use spaceonfire\LaminasHydratorBridge\Strategy\BooleanStrategy;
use spaceonfire\LaminasHydratorBridge\Strategy\ScalarStrategy;
use spaceonfire\LaminasHydratorBridge\Strategy\NullableStrategy;
use spaceonfire\Type\BuiltinType;

$hydrator = new StdClassHydrator();

$hydrator->setNamingStrategy(new AliasNamingStrategy([
    'firstName' => ['first_name', 'firstname'],
    'lastName' => ['last_name', 'lastname'],
    'rulesAccepted' => ['rules_accepted'],
]));

$hydrator->addStrategy('age', new NullableStrategy(new ScalarStrategy(BuiltinType::INT)));
$hydrator->addStrategy('rulesAccepted', new BooleanStrategy(['Y', 'y', 1], 'N', false));

$john = $hydrator->hydrate([
    'first_name' => 'John',
    'last_name' => 'Doe',
    'rules_accepted' => 'y',
    'age' => '25',
], new stdClass());

// $john->firstName === 'John';
// $john->lastName === 'Doe';
// $john->rulesAccepted === true;
// $john->age === 25;

$jane = $hydrator->hydrate([
    'firstname' => 'Jane',
    'lastname' => 'Doe',
    'rules_accepted' => '',
    'age' => null,
], new stdClass());

// $jane->firstName === 'John';
// $jane->lastName === 'Doe';
// $jane->rulesAccepted === false;
// $jane->age === null;

变更日志

请参阅CHANGELOG以获取最近变更的更多信息。

贡献

请在main spaceonfire仓库报告问题发送Pull Requests。请参阅CONTRIBUTINGCODE_OF_CONDUCT以获取详细信息。

致谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。