digital-craftsman/self-aware-normalizers 问题修复 & 功能扩展

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

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

digital-craftsman/self-aware-normalizers

Composer 安装命令:

composer require digital-craftsman/self-aware-normalizers

包简介

Adds interfaces and normalizers that allow value objects to normalize and denormalize themselves.

README 文档

README

A Symfony bundle to enable value objects and DTOs to normalize and denormalize themselves through implementing simple interfaces that normalize to scalar values and denormalize themselves from scalar values (string, int, float, bool and array). Adding this kind of logic to the classes themselves might be considered a bad practice, but depending on the use case it will actually be better due to the fact that the data structure and the normalization need to be changed together.

The name implies that the value objects and DTOs are self-aware in the sense that they know how to normalize and denormalize themselves and that they are self-aware enough to do so 🙂

As it's a central part of an application, it's tested thoroughly (including mutation testing).

Latest Stable Version PHP Version Require codecov Packagist Downloads Packagist License

Installation and configuration

Install package through composer:

composer require digital-craftsman/self-aware-normalizers

Optionally, you can add a self-aware-normalizers.php file to your config/packages. The options are described below.

Usage

Normalizers

To make the normalization process easier, there are the following normalizers included:

  • StringNormalizableNormalizer
  • IntNormalizableNormalizer
  • FloatNormalizableNormalizer
  • BoolNormalizableNormalizer
  • ArrayNormalizableNormalizer

Additionally, there is an interface for each of the normalizers. Every class that implements one of the interfaces, will be automatically normalized to the respected type. This means putting the logic of how serialization of a class works within the class. That's not really seen as a good practice. In my experience, the data structure and the normalization need to be changed together. So, I like it better to have both in one place. I've used this approach in multiple large scale projects for years and haven't had a single issue with it yet. But your mileage may vary.

With this you can have nested denormalization that looks like this:

/**
 * @psalm-type NormalizedSearch = array{
 *     searchTerm: string,
 *     limit: int,
 * }
 */
final readonly class Search implements ArrayNormalizable
{
    public function __construct(
        public SearchTerm $searchTerm,
        public Limit $limit,
    ) {
    }
    
    /**
    * @param NormalizedSearch $data
    */
    public static function denormalize(array $data): self
    {
        return new self(
            searchTerm: SearchTerm::denormalize($data['searchTerm']),
            limit: Limit::denormalize($data['limit']),
        );
    }
    
    /**
    * @return NormalizedSearch
    */
    public function normalize(): array
    {
        return [
            'searchTerm' => $this->searchTerm->normalize(),
            'limit' => $this->limit->normalize(),
        ];
    }
}

Denormalized for null handling

When handling null you can use the Nullable*Denormalizable interfaces with the related Nullable*DenormalizableTrait to handle switches between null and the class like the following:

/**
 * @psalm-type NormalizedSearchWithOptionalLimit = array{
 *     searchTerm: string,
 *     limit: int | null,
 * }
 */
final readonly class SearchWithOptionalLimit implements ArrayNormalizable
{
    public function __construct(
        public SearchTerm $searchTerm,
        public ?Limit $limit,
    ) {
    }
    
    /**
    * @param NormalizedSearchWithOptionalLimit $data
    */
    public static function denormalize(array $data): self
    {
        return new self(
            searchTerm: SearchTerm::denormalize($data['searchTerm']),
            limit: Limit::denormalizeWhenNotNull($data['limit']),
        );
    }
    
    /**
    * @return NormalizedSearchWithOptionalLimit
    */
    public function normalize(): array
    {
        return [
            'searchTerm' => $this->searchTerm->normalize(),
            'limit' => $this->limit?->normalize(),
        ];
    }
}

Internally the value object simply has to implement the relevant interface and use the related trait like the following:

final readonly class Limit implements IntNormalizable, NullableIntDenormalizable
{
    use NullableIntDenormalizableTrait;

    public function __construct(
        public int $limit,
        ...

Doctrine types

When using the normalizers, you can also use the same logic for doctrine types.

Automatic doctrine types

You don't even need to define custom doctrine types. All you need to do is to add the directory your implementing classes are into the config/packages/self-aware-normalizers.php configuration:

<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\App;

return App::config([
    'self_aware_normalizers' => [
        'implementation_directories' => [
            '%kernel.project_dir%/src/ValueObject',
        ],
    ],
]);

Now you can use the implementing classes themselves directly in your entity like the following:

<?php

declare(strict_types=1);

namespace App\Entity;

use App\ValueObject\UserName;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
class User
{
    ...
    
    #[ORM\Column(name: 'name', type: UserName::class)]
    public UserName $name;
    
    ...
}

There are 3 interfaces that can be used for customization how the doctrine type behaves when implementing them in you classes:

NormalizableTypeWithSQLDeclaration

Adds a custom SQL declaration

StringNormalizableTypeAsTypeText

Uses type TEXT instead of the default VARCHAR(255) for strings.

StringNormalizableTypeWithMaxLength

Defines the max length for strings. For example: VARCHAR(50).

Custom doctrine types

If you do have custom doctrine types, you can also add them to the second configuration option for them to be registered automatically. They just need to contain the public static function getTypeName(): string method.

<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\App;

return App::config([
    'self_aware_normalizers' => [
        'doctrine_type_directories' => [
            '%kernel.project_dir%/src/Doctrine',
        ],
    ],
]);

As an added bonus, this makes sure, that the structure is always the same no matter if you're using Doctrine to read from the data or a normalizer.

Additional documentation

digital-craftsman/self-aware-normalizers 适用场景与选型建议

digital-craftsman/self-aware-normalizers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.84k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 digital-craftsman/self-aware-normalizers 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-03