rahimi-ali/php-dto

PHP 数据传输对象

dev-master 2023-11-07 12:16 UTC

This package is auto-updated.

Last update: 2024-09-30 01:43:46 UTC


README

PHP 的 DTO!

DTO 可以用于将传入的 Psr7 服务器请求、json 和随机数组转换为具有内置验证的强类型类。

不使用反射,使用传统的类和方法处理一切!

安装

composer require rahimi-ali/php-dto

类型

  • int(bool $strict = false): IntType
  • float(bool $strict = false): FloatType
  • string(bool $strict = false): StringType
  • bool(bool $strict = false): BoolType
  • datetime(string $format = DateTimeInterface::ATOM, string|null $timezone = null, bool $immutable = true): DateTimeType
  • embedded(string $class): EmbeddedType
  • dynamicEmbedded(string|Closure $discriminator, array $types = []): DynamicEmbeddedType
  • collection(TypeInterface $type): CollectionType

规则

  • int(bool $strict = false): IntRule 声明 IntType 字段时自动添加
  • float(bool $strict = false): FloatRule 声明 FloatType 字段时自动添加
  • string(bool $strict = false): StringRule 声明 StringType 字段时自动添加
  • bool(bool $strict = false): BoolRule 声明 BoolType 字段时自动添加
  • array(): ArrayRule 应该是一个具有连续 int 键的数组
  • object(): ObjectRule 应该是一个对象或具有字符串键或非连续 int 键的数组
  • min(int $min, bool $strict = false): MinRule
  • max(int $max, bool $strict = false): MaxRule
  • minLength(int $length, bool $strict = false): MinLengthRule
  • maxLength(int $length, bool $strict = false): MaxLengthRule
  • in(array $values, bool $strict = false): InRule
  • notIn(array $values, bool $strict = false): NotInRule
  • equals(mixed $value, bool $strict = false): EqualsRule
  • notEqual(mixed $value, bool $strict = false): NotEqual

示例

class AddressDto extends Dto
{
    private string $city;
    private string $street;
    private int $number;
    
    public static function fields(array $data): array
    {
        return [
            'city' => new Field('city', Type::string(true), rules: [Rule::in(['London', 'Paris', 'New York'])]),
            'street' => new Field('street', Type::string(true), rules: [Rule::min(5)]),
            'number' => new Field('number', Type::int(true), rules: [Rule::min(1), Rule::max(100)]),
        ];
    }
    
    public function getCity(): string
    {
        return $this->city;
    }
    
    public function getStreet(): string
    {
        return $this->street;
    }
    
    public function getNumber(): int
    {
        return $this->number;
    }
}

class UserDto extends Dto
{
    private string $name;
    private int $age;
    private AddressDto $address;
    
    public static function fields(array $data): array
    {
        return [
            'name' => new Field('name', Type::string(true), rules: [Rule::min(5)]),
            'age' => new Field('age', Type::int(true), rules: [Rule::min(18)]),
            'address' => new Field('address', Type::embedded(AddressDto::class)),
        ];
    }
    
    public function getName(): string
    {
        return $this->name;
    }
    
    public function getAge(): int
    {
        return $this->age;
    }
    
    public function getAddress(): AddressDto
    {
        return $this->address;
    }
}

说明

  • 由于没有使用反射和魔术方法,私有和只读属性不能使用,因为父类默认无法访问它们。如果您真的需要私有和只读属性,请覆盖您 DTO 中的 setProperty 方法。
protected function setProperty(string $property, mixed $value): void
{
    $this->{$property} = $value;
}

标准

  • 代码覆盖率:100%
  • PHPStan 级别:9
  • Infection MSI:94%

许可证

MIT