定制 scn/hydrator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

scn/hydrator

Composer 安装命令:

composer require scn/hydrator

包简介

A pragmatic hydrator and extractor library

README 文档

README

Latest Stable Version Monthly Downloads License

A pragmatic hydrator and extractor library

Installation

Via Composer:

$ composer require scn/hydrator

Usage

See the examples folder for usage instructions by code.

Extraction

First of all you'll need a class that implements \Scn\Hydrator\Config\ExtractorConfigInterface. The only method getExtractorProperties has to return an array with string keys and callables as values.

The string keys of this array are used to craft the result of the extraction.

The corresponding callables must have the signature callable(string $propertyName): mixed. The $propertyName parameter will be the corresponding string key. You will usually not need this information.

If this callable is an instance of \Closure, its' $this and static context are bound to the entity object to extract. As a result of this the closure will have access to private and protected properties and methods of the entity.

A short example:

<?php

require_once 'vendor/autoload.php';

class ExtractorConfig implements Configuration\ExtractorConfigInterface
{

    public function getExtractorProperties(): array
    {
        return [
            'property' => function (string $propertyName): string {
                return $this->privateProperty; // `$this` will be the entity to extract
            }
        ];
    }
}

class Entity
{
    private $privateProperty = 'private value';
}

$hydrator = new \Hydrator();

$result = $hydrator->extract(
    new ExtractorConfig(),
    new Entity()
);

var_dump(assert($result === ['property' => 'private value'])); // -> bool(true)

Hydration

For hydration you'll need a class implementing \Scn\Hydrator\Configuration\HydratorConfigInterface. The only method getHydratorProperties has to return an array with string keys and callables as values.

As an alternative you can use the \Scn\Hydrator\Configuration\GenericHydratorConfig.

The string keys have to correspond the keys in the data to hydrate your object with.

The corresponding callables must have the signature callable(mixed $value, string $propertyName): void. The $propertyName parameter will be the corresponding string key. You will usually not need this information.

If this callable is an instance of \Closure, its' $this and static context are bound to the entity object to hydrate. As a result of this the closure will have access to private and protected properties and methods of the entity.

A short example:

<?php

require_once __DIR__.'vendor/autoload.php';

class HydratorConfig implements Configuration\HydratorConfigInterface
{

    public function getHydratorProperties(): array
    {
        return [
            'property' => function (string $value, string $propertyName): void {
                $this->privateProperty = $value; // $this will be the entity to hydrate
            }
        ];
    }
}

class Entity
{
    private $privateProperty = 'private value';

    public function getPropertyValue(): string
    {
        return $this->privateProperty;
    }
}

$hydrator = new \Hydrator();
$data = [
    'property' => 'hydrated private value',
];

$entity = new Entity();

$hydrator->hydrate(
    new HydratorConfig(), 
    $entity, 
    $data
);

var_dump(assert('hydrated private value' === $entity->getPropertyValue())); // -> bool(true)

Hydration flags

The hydrate method has a fourth, optional bit flag parameter $flags.

Currently there are two options available

\Scn\Hydrator\Hydrator::NO_STRICT_KEYS (1)

If this bit is set in $flags, the hydrator will ignore keys in the data array that have no corresponding key in the array the getHydratorProperties method returns.

This bit is not set by default, additional keys in the data array will lead to an \InvalidArgumentException exception thrown by the hydrator.

\Scn\Hydrator\Hydrator::IGNORE_KEYS (2)

If this bit is set, the hydrator will ignore the keys of the data array but assume the entries in the data array are in the same order as in the hydrator configuration array.

Example:

<?php

require_once __DIR__.'vendor/autoload.php';

class HydratorConfig implements Configuration\HydratorConfigInterface
{

    public function getHydratorProperties(): array
    {
        return [
            'property_a' => function (string $value, string $propertyName): void {
                $this->privatePropertyA = $value;
            },
            'property_b' => function (string $value, string $propertyName): void {
                $this->privatePropertyB = $value;
            },
            'property_c' => function (string $value, string $propertyName): void {
                $this->privatePropertyC = $value;
            },

        ];
    }
}

class Entity
{
    private $privatePropertyA;
    private $privatePropertyB;
    private $privatePropertyC;

    public function getPropertyValues(): array
    {
        return [
            $this->privatePropertyA,
            $this->privatePropertyB,
            $this->privatePropertyC,
        ];
    }
}

$hydrator = new \Hydrator();
$data = ['value a', 'value_b', 'value_c'];

$entity = new Entity();

$hydrator->hydrate(
    new HydratorConfig(),
    $entity,
    $data,
    Hydrator::IGNORE_KEYS
);

var_dump(assert($data === $entity->getPropertyValues())); // -> bool(true)

scn/hydrator 适用场景与选型建议

scn/hydrator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 303.07k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 07 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 scn/hydrator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 scn/hydrator 我们能提供哪些服务?
定制开发 / 二次开发

基于 scn/hydrator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 303.07k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 19
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-23