PHP code example of codebot / phpdto

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

    

codebot / phpdto example snippets




namespace App\Dto;

use PhpDto\Dto;
use PhpDto\DtoSerialize;
use PhpDto\ToArray;

class ItemDto extends Dto
{
	use DtoSerialize, ToArray;

	private int $_id;
	private ?int $_count;
	private string $_name;
	private ?string $_description;
	private bool $_isActive;

	public function __construct( array $item )
	{
	    $this->_id = $item['id'];
	    $this->_count = $item['count'];
	    $this->_name = $item['name'];
	    $this->_description = $item['description'];
	    $this->_isActive = $item['is_active'];
	}

	public function getId(): int
	{
	    return $this->_id;
	}

	public function getCount(): ?int
	{
	    return $this->_count;
	}

	public function getName(): string
	{
	    return $this->_name;
	}

	public function getDescription(): ?string
	{
	    return $this->_description;
	}

	public function getIsActive(): bool
	{
	    return $this->_isActive;
	}
}

$itemData = [
    'id' => 1,
    'name' => 'Dummy Item',
    'description' => 'Some dummy description.',
    'count' => 10,
    'is_active' => true,
    'meta' => [
        'meta_title' => 'Dummy meta title',
        'meta_description' => 'Dummy meta description',
    ],
    'tags' => 'TagOne, TagTwo, TagThree'
];

$item = ItemDto::mapSingle( $itemData );

// Now you are able to access DTO properties via getters

$item->getId();
$item->Name();
$item->getDescription();
$item->getCount();
$item->getIsActive();

$itemData = [
    [
        'id' => 1,
            'name' => 'Dummy Item',
            'description' => 'Some dummy description.',
            'count' => 10,
            'is_available' => true, 
            'meta' => [
                'meta_title' => 'Dummy meta title',
                'meta_description' => 'Dummy meta description',
            ],
            'tags' => 'TagOne, TagTwo, TagThree'
    ],
    // more items
];

$items = ItemDto::mapArray( $itemData ); // array of instance of ItemDto class

foreach( $items as $item )
{
    $item->getId();
    // ... 
}

$fakeData = DtoFaker::fakeSingle( ItemDto::class ); // array that contains fake data for ItemDto
$item = ItemDto::mapSingle( $fakeData );

$fakeData = DtoFaker::fakeArray( ItemDto::class, 10 );
$items = ItemDto::mapArray( $fakeData );



namespace App\Dto;

use PhpDto\Dto;
use PhpDto\ToArray;

class MockDto extends Dto
{
    use ToArray;
    
    private ?string $_name;
    private int $_count;
    private bool $_isTrue;
    
    public function __construct( array $data )
    {
        $this->_name   = $data['name'];
        $this->_count  = $data['count'];
        $this->_isTrue = $data['is_true'];
    }
    
    public function getName(): ?string
    {
        return $this->_name;
    }
    
    public function getCount(): int
    {
        return $this->_count;
    }

    public function getIsTrue(): bool
    {
        return $this->_isTrue;
    }
}

$mockData = [
    'name' => 'Mock name',
	'count'   => 4,
	'is_true' => true
];

$dto = new MockDto($mockData);

$arr = $dto->toArray();

var_dump($arr);