paolo/data-transfer-object

该包的最新版本(1.0.0)没有提供许可证信息。

简单的DTO包装器

1.0.0 2024-02-15 05:24 UTC

This package is auto-updated.

Last update: 2024-10-02 14:44:29 UTC


README

可用的属性

  1. PropertyIgnoreParse - 在解析数组时忽略对象属性
  2. PropertyIgnoreSerialize - 在序列化对象时忽略属性

示例

class T extends DataTransferObject
{
    #[PropertyMapTo('test.job.0')]
    #[PropertyMapFrom('tt')]
    #[PropertyParse(T::class, 'parse', true)]
    #[PropertySerialize(T::class, 'serString', true, 'string')]
    #[PropertySerialize(T::class, 'serInt', true, PhpType::Integer)]
    #[PropertySerialize(T::class, 'serNull', true, PhpType::NULL)]
    public mixed $t;

    public static function parse(mixed $v)
    {
        return 'Hello world!';
    }

    public static function serString(string $v)
    {
        return substr($v, 0, 5);
    }

    public static function serInt(int $v)
    {
        return $v * 2;
    }

    public static function serNull(mixed $v)
    {
        return 'undefined';
    }
}

$t = T::wrap(['tt' => null]);

var_dump($t->toArray());
/*
 array(1) {
  ["test"]=>
  array(1) {
    ["job"]=>
    array(1) {
      [0]=>
      string(5) "Hello"
    }
  }
}

 */