paulo/data-transfer-object

There is no license information available for the latest version (1.0.0) of this package.

Simple DTO wrapper

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 - игнорирует свойство при сериализации объекта

Example

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"
    }
  }
}

 */