PHP code example of paulo / data-transfer-object

1. Go to this page and download the library: Download paulo/data-transfer-object library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

paulo / data-transfer-object example snippets


#[PropertyMapFrom('source.from.key.innerKey')]
public mixed $property;

#[PropertyMapTo('destination.to.qwe')]
public mixed $property;

#[PropertyIgnoreParse]
public mixed $property;

#[PropertyIgnoreSerialize]
public mixed $property;

#[PropertyInternal]
public mixed $property;

#[PropertyParse(DateTime::class)]
public DateTime $property;

#[PropertyParseArray(InternalClass::class, 'wrap', true)]
public array $property;

#[PropertySerialize(InternalClass2::class, 'serialize', true, 'array')]
public InternalClass2 $property;

#[PropertySerializeArray(InternalClass2::class, 'serialize', true, InternalClass2::class)]
public array $property;

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

 */