PHP code example of rasclatt / smart-dto

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

    

rasclatt / smart-dto example snippets



namespace MyNamespace\Dto;

use \SmartDto\Dto as SDto;

class Get extends SDto
{
    # Type these attribures as 


namespace MyNamespace;

use Dto\Get;

class MyClass
{
    # You can type your return so your IDE can easily
    # interpret what should be returned for quick reference
    public function get(): Get
    {
        $dataArray = [
            'id' => 123,
            'description' => 'Some kind of example description',
            'tile' => 'Tile Here!'
        ];
        # Return back your Dto
        return new Get($dataArray);
    }
}


$MyClass = new MyNamespace\MyClass();
# Any respectable IDE will be able to detect your params and make viewing available attributes easy
# If the data attribute "$dataArray" is missing the "id" key, for example, because of the Dto object
# mapping, this will not throw an undefined index error
# As is, this will write "123"
echo $MyClass->id;


namespace MyNamespace\Dto;

use \SmartDto\Dto as SDto;

class Get extends SDto
{
    public $id = 0;
    public $description = '';
    public $title = '';
    
    # If you create a same-named method as the parameter,
    # the Dto will then call this method post-process
    # Because it's protected, your IDE should not show it as available
    protected function id()
    {
        $this->id += 1;
    }
}



$MyClass = new MyNamespace\MyClass();
# This will result in the value "124" instead of "123"
echo $MyClass->id;


namespace MyNamespace\Dto;

use \SmartDto\Dto as SDto;

class Get extends SDto
{
    # Notice the Camel Case keys
    public $id = 0;
    public $theDescription = '';
    public $myTitle = '';
}



namespace MyNamespace;

use Dto\Get;

class MyClass
{
    public function get(): Get
    {
        # Notice these are not formatted the same as the DTO
        $dataArray = [
            'id' => 123,
            'the_description' => 'Some kind of example description',
            'my title' => 'Tile Here!'
        ];
        # Return back your Dto
        return new Get($dataArray, \SmartDto\Dto::CAMEL_CASE);
    }
}


echo $MyClass->theDescription;


use \SmartDto\Mapper;

$dataArray = [
    'id' => 123,
    'description' => 'Some kind of example description',
    'price' => 1.23
];

$Mapper = new Mapper($dataArray);
echo $Mapper->getAttributes();

# Original array with undesirable keys
$array = [
    'FIRST' => 'John',
    'LAST' => 'Doe'
];

# This is the array you want to create
$target = [
    'fullName' => '~FIRST~ ~LAST~',
    'firstName' => '~FIRST~',
    'lastName' => '~LAST~'
];

# Map the arrays
print_r(\SmartDto\Mapper::mapster($array, $target));