bernard/normalt
Composer 安装命令:
composer require bernard/normalt
包简介
Normalt is a extension to Symfony Serializer that only implements the Normalization part
README 文档
README
Normalt contains additional normalizers for use with the serializer component found in Symfony. It also implements a normalizer delegator that will look at the data you want normalized and/or denormalized and call the normalizer which supports it.
In the context of Normalt normalization is the act of converting an object into an array. Denormalization is the opposite direction (converting array into an object). This is to my knowledge the same concept Symfony serializer uses.
Table of Contents
Getting Started
Getting started is as easy as requiring the library with composer.
$ composer require bernard/normalt
Normalizers
Theese normalizers can be used with the serializer component directly or through the AggregateNormalizer.
AggregateNormalizer
AggregateNormalizer is a delegator and aggregator as it aggregates multiple normalizers and denormalizers
which it will delegate the process to.
It have a list of normalizers and denormalizers. It will ask each of theese if they support the data/object and use the first found.
It implements a subset of the full serializer and its only focus is normalizing to arrays and
denormalize arrays into objects. This lets you focus on normalization instead of converting
into a specific format such as xml, json etc.
Usage
You need to instantiate the normalizer and the list of normalizer/denormalizers you want to use.
For this example we use GetSetMethodNormalizer which is distributed with the symfony package.
This is the class we are going to use. GetSetMethodNormalizer uses getters and setters to do
its job.
class User { protected $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } }
Lets normalize and denormalize it again.
use Normalt\Normalizer\AggregateNormalizer; use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; $aggregate = new AggregateNormalizer([new GetSetMethodNormalizer]); $user = new User; $user->setName('henrik'); $array = $aggregate->normalize($user); // $array now contains ['name' => 'henrik'] $user = $aggregate->denormalize($array, 'User'); echo $user->getName(); // outputs Henrik
In contrast to the other normalizers in this package, it does not make sense to use this with the serializer as the Serializer already does most of the functionality already.
DoctrineNormalizer
DoctrineNormalizer normalizes mapped objects (Entities, Documents etc.) into arrays and back again.
It usage is very simple. The following example assume a mapped object of $user and that you are
using the doctrine orm (other doctrine projects work aswell!).
use Doctrine\ORM\EntityManager; use Normalt\Normalizer\DoctrineNormalizer; // create $entityManager $normalizer = new DoctrineNormalizer($entityManager); // assuming $user is a mapped object and have the identifier value of 10. the following will return // array('MyModel\User', 10) $array = $normalizer->normalize($user); // using the same structure you can convert it back into a user $user = $normalizer->denormalize($array, null);
RecursiveReflectionNormalizer
This normalizes also delegates like the AggregateNormalizer, but delegates for each property in the object
you are normalizing to normalize. It does this with recursion, so if a normalizer does not support a given
property and is an array it will loop through that array and look for more objects.
The same thing happens when denormalizing, except it will try and find a supporting denormalizer for the property structure before looping.
Using is simple as the other, the example utilises DoctrineNormalizer and assumes we have a $profile object
that contains a reference to a user with $profile->user.
use Normalt\Normalizer\RecursiveReflectionNormalizer; use Normalt\Normalizer\DoctrineNormalizer; $normalizer = new RecursiveReflectionNormalizer([new DoctrineNormalizer($entityManager)]); // following will return assuming User is mapped and has the identifier of 10 //['user' => ['MyModel\User', 10]] $array = $normalizer->normalize($profile); // converting it back into the object. // $profile->user is now an instance of MyModel\User $profile = $normalize->denormalize($array, 'MyModel\Profile');
License
Please refer to the included LICENSE file.
bernard/normalt 适用场景与选型建议
bernard/normalt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.51M 次下载、GitHub Stars 达 22, 最近一次更新时间为 2014 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「normalization」 「denormalization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bernard/normalt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bernard/normalt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bernard/normalt 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
De/normalize business objects without tightly coupling them to your normalization format
Simple minimal but useful set of utils (properties, strings, datetimes, etc.)
A library to extend, manipulate and normalize string.
A PHP 8.5+ DTO and Data library with immutable DTOs and mutable Data objects
An enhanced facade to existing unicode-normalization implementations.
A Laravel package to add follow/unfollow functionality to Eloquent models.
统计信息
- 总下载量: 1.51M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 22
- 点击次数: 36
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-03-02