morebec/orkestra-normalization 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

morebec/orkestra-normalization

Composer 安装命令:

composer require morebec/orkestra-normalization

包简介

Orkestra Component allowing to easily normalize complex object graphs to arrays of primitives for persistence purposes

README 文档

README

The normalization component allows to easily normalize complex object graphs to arrays of primitives for persistence purposes.

Essentially, it makes transforming POPOs into human-readable, platform independent serializable arrays of primitives a breeze.

It can be used to:

  • Serialize in any format that supports PHP arrays and primitives.
  • Dump objects to an SQL database without the need for an ORM.
  • Dump complex object graphs to a document store.
  • Load an object in memory from a serialized representation.
  • Convert HTTP requests to Typed Objects.

Installation

Usage

Let's take an example of an object graph:

class Project 
{
    /** @var string */
    private $id;
    
    /** @var string */
    private $title;
    
    /** @var string */
    private $description;
    
    /** @var Task[] */
    private $tasks;
    
    // ...
}

class Task 
{
    /** @var string */
    private $id;
    
    /** @var string */
    private $name;
    
    /** @var DateTime */
    private $dueDate;
    
    /** @var bool */
    private $completed;
    
    // ...
}

Here's how one can normalize an object:

use Morebec\Orkestra\Normalization\ObjectNormalizer;$project = new Project('prj123456789', 'A new Project', 'This is our latest project');
$project->addTask(new Task('tsk123456789', 'Deploy to production', $dueDate));

// Normalize
$normalizer = new ObjectNormalizer();
$data = $normalizer->normalize($project);

print_r($data);
// Would print:
[
    'id' => 'prj123456789',
    'title' => 'A new Project',
    'description' => 'This is our latest project',
    'tasks' => [
        [
            'id' => 'tsk123456789',
            'name' => 'Deploy to production',
            'dueDate' => '2021-01-01T10:25:55+00:00',
            'completed' => false
        ]       
    ]
];

This last representation can easily be serialized to json, xml, yaml or any other format.

Denormalization

Here's how to convert a normalized object back to an instance of its class:

// Would print:
use Morebec\Orkestra\Normalization\ObjectNormalizer;$data = [
    'id' => 'prj123456789',
    'title' => 'A new Project',
    'description' => 'This is our latest project',
    'tasks' => [
        [
            'id' => 'tsk123456789',
            'name' => 'Deploy to production',
            'dueDate' => '2021-01-01T10:25:55+00:00',
            'completed' => false
        ]       
    ]
];
$normalizer = new ObjectNormalizer();
$project = $normalizer->denormalize($data, Project::class);

The process of denormalization works using reflection inspecting an object's structure. In order to know what into what type to denormalize a value to, the normalizer checks for @var annotations on the object for PHP < 7.4, or the declared type for PHP >= 7.4. Therefore, it is important to correctly declare the @var annotations if using an older version of PHP.

Custom normalizers/denormalizer.

Depending on the structure of your objects you might want to personalize the way they are (de)normalized. A common example is with value objects wrapping primitives:

class Username {

    /** @var string */    
    private $value;

    public function __construct(string $value) {
        $this->value = $value;
    }
    
    public function __toString()
    {
        return $this->value;
    }
}

Such object out of the box would be normalized as follows:

[
    'value' => 'the_username'
];

For such object, and especially when they are part of an object graph, you might want to normalize them to the primitive they wrap for example.

To perform this you need to specify a custom normalizer/denormalizer pair and add it to your normalizer:

use Morebec\Orkestra\Normalization\Denormalizer\DenormalizationContextInterface;
use Morebec\Orkestra\Normalization\ObjectNormalizer;
use Morebec\Orkestra\Normalization\Normalizer\ObjectNormalizer\FluentNormalizer;
use Morebec\Orkestra\Normalization\Denormalizer\ObjectDenormalizer\FluentDenormalizer;

$normalizer = new ObjectNormalizer();

$normalizer->addNormalizer(FluentNormalizer::for(Username::class)->asString());
$normalizer->addDenormalizer(FluentDenormalizer::for(Username::class)->as(static function (DenormalizationContextInterface $context) {
    return new Username($context->getValue());
}));

The FluentNormalizer and FluentDenormalizer are the most convenient way to define (De)Normalizers. If you want to have full control over them you can implement the NormalizerInterface and DenormalizerInterface.

morebec/orkestra-normalization 适用场景与选型建议

morebec/orkestra-normalization 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 552 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 morebec/orkestra-normalization 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 552
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 12
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2021-04-21