vjik/cycle-typecast

Cycle ORM 中类型转换的数据助手

2.2.0 2024-06-17 12:21 UTC

This package is auto-updated.

Last update: 2024-09-17 13:14:39 UTC


README

Latest Stable Version Total Downloads Build status Mutation testing badge static analysis psalm-level

该软件包提供

  • Typecaster 帮助在 Cycle ORM 中进行类型转换,并抽象化 TypecastHandler,该处理器使用它;
  • AttributeTypecastHandler 使用属性进行类型转换;
  • TypeInterface 必须由在 TypecasterAttributeTypecastHandler 中使用的类实现;
  • 用于 DateTimeImmutableUUIDArrayEnum 类型的类。

安装

可以使用 composer 安装此软件包

composer require vjik/cycle-typecast

一般用法

属性

use Vjik\CycleTypecast\AttributeTypecastHandler;
use Vjik\CycleTypecast\UuidString\UuidStringToBytesType;
use Vjik\CycleTypecast\DateTimeImmutable\DateTimeImmutableToIntegerType;

#[Entity(
    // ...
    typecast: AttributeTypecastHandler::class,
)]
final class User
{
    // ...

    #[Column(type: 'primary', primary: true)]
    #[UuidStringToBytesType]
    private string $id;

    #[Column(type: 'int')]
    #[DateTimeImmutableToIntegerType]
    private DateTimeImmutable $createDate;

自定义类型转换处理器

use Vjik\CycleTypecast\ArrayToStringType;
use Vjik\CycleTypecast\DateTimeImmutable\DateTimeImmutableToIntegerType;
use Vjik\CycleTypecast\TypecastHandler;
use Vjik\CycleTypecast\UuidString\UuidStringToBytesType;

final class UserTypecastHandler extends Vjik\CycleTypecast\TypecastHandler
{
    protected function getConfig(): array
    {
        return [
            'id' => new UuidStringToBytesType(),
            'createDate' => new DateTimeImmutableToIntegerType(),
            'modifyDate' => new DateTimeImmutableToIntegerType(),
            'tags' => new ArrayToStringType(','),
        ];
    }
}

自定义映射器

use Cycle\ORM\ORMInterface;
use Cycle\ORM\PromiseMapper\PromiseMapper;
use Vjik\CycleTypecast\Typecaster;
use Vjik\CycleTypecast\ArrayToStringType;
use Vjik\CycleTypecast\DateTimeImmutable\DateTimeImmutableToIntegerType;
use Vjik\CycleTypecast\UuidString\UuidStringToBytesType;

final class UserMapper extends PromiseMapper
{
    private Typecaster $typecaster;

    public function __construct(ORMInterface $orm, string $role)
    {
        // Typecast configuration
        $this->typecaster = new Typecaster([
            'id' => new UuidStringToBytesType(),
            'createDate' => new DateTimeImmutableToIntegerType(),
            'modifyDate' => new DateTimeImmutableToIntegerType(),
            'tags' => new ArrayToStringType(','),
        ]);
        
        parent::__construct($orm, $role);
    }

    public function extract($entity): array
    {
        $data = parent::extract($entity);
        
        // Typecast after extract from entity
        return $this->typecaster->prepareAfterExtract($data);
    }

    public function hydrate($entity, array $data)
    {
        // Typecast before hydrate entity
        $data = $this->typecaster->prepareBeforeHydrate($data);
        
        return parent::hydrate($entity, $data);
    }
}

类型

ArrayToStringType

new ArrayToStringType(',');

实体值:字符串数组。例如,['A', 'B', 'C']

数据库值:使用在构造函数中设置的分隔符连接的数组。例如,A,B,C

DateTimeImmutableToIntegerType

new DateTimeImmutableToIntegerType();

实体值:DateTimeImmutable

数据库值:字符串形式的时间戳(例如,1609658768)。

IntegerEnumType

new IntegerEnumType(IntegerEnum::class);

实体值:整数类型的枚举。

数据库值:整数类型的枚举值。

StringEnumType

new StringEnumType(StringEnum::class);

实体值:字符串类型的枚举。

数据库值:字符串类型的枚举值。

UuidStringToBytesType

new UuidStringToBytesType();

实体值:UUID 的标准字符串表示形式。例如,1f2d3897-a226-4eec-bd2c-d0145ef25df9

数据库值:UUID 的二进制字符串表示形式。

测试

单元测试

该软件包使用 PHPUnit 进行测试。要运行测试

./vendor/bin/phpunit

突变测试

该软件包的测试使用 Infection 突变框架和 Infection 静态分析插件 进行检查。要运行它

./vendor/bin/roave-infection-static-analysis-plugin

静态分析

代码使用 Psalm 进行静态分析。要运行静态分析

./vendor/bin/psalm

许可证

Cycle Typecast 是免费软件。它根据 BSD 许可证发布。有关更多信息,请参阅 LICENSE