PHP code example of amorphine / data-transfer-object

1. Go to this page and download the library: Download amorphine/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/ */

    

amorphine / data-transfer-object example snippets


$post = $api->get('posts', 1); 

[
    'title' => '…',
    'body' => '…',
    'author' => '["id" => 1, ...]',
]

class PostData extends DataTransferObject
{
    /** @var string */
    public $title;
    
    /** @var string */
    public $body;
    
    /** @var AnotherDataTransferObjectImplementation */
    public $author;
}

$postData = new PostData([
    'title' => '…',
    'body' => '…',
    'author' => '…',
]);

$postData->title;
$postData->body;
$postData->author;

use SomeNameSpace\Author;

class PostData extends DataTransferObject
{
    /**
     * Built in types: 
     *
     * @var string 
     */
    public $property;

    /**
     * You can override property data source (array key)
     *
     * @source keyWithSomeAnotherName
     */
    public $property;
    
    /**
     * Classes with their FQCN: 
     *
     * @var Author
     */
    public $property;
    
    /**
     * Lists of types: 
     *
     * @var Author[]
     */
    public $property;
    
    /**
     * Iterator of types: 
     *
     * @var iterator<Author>
     */
    public $property;
    
    /**
     * Union types: 
     *
     * @var string|int
     */
    public $property;
    
    /**
     * Nullable types: 
     *
     * @var string|null
     */
    public $property;
    
    /**
     * Mixed types: 
     *
     * @var mixed|null
     */
    public $property;
    
    /**
     * Any iterator : 
     *
     * @var iterator
     */
    public $property;
    
    /**
     * No type, which allows everything
     */
    public $property;
    
    /** 
     * PHP types declaration supported
     */
    public ?int $property;

}

class PostData extends DataTransferObject
{
    /** 
     * @var AuthorData
     * @source authorData
     */
    public $author;
}

class PostData extends DataTransferObject
{
    /** @var AuthorData */
    public $author;
}

$postData = new PostData([
    'author' => [
        'name' => 'Foo',
    ],
]);

class TagData extends DataTransferObject
{
    /** @var string */
   public $name;
}

class PostData extends DataTransferObject
{
    /** @var TagData[] */
   public $tags;
}

$postData = new PostData([
    'tags' => [
        ['name' => 'foo'],
        ['name' => 'bar']
    ]
]);