PHP code example of romabeckman / dtophp

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

    

romabeckman / dtophp example snippets


class UserInDto extends InDto {
    /**
     * @rule *
     * @rule  * @rule  * @var array
     */
    private $foods;
    function getName(): string { return $this->name; }
    function getEmail(): string { return $this->email; }
    function getAddress(): AddressInDto { return $this->address; }
    function getAge(): int { return $this->age; }
    function getFoods(): array { return $this->foods; }
    function setName(string $name): void { $this->name = $name; }
    function setEmail(string $email): void { $this->email = $email; }
    function setAddress(AddressInDto $address): void { $this->address = $address; }
    function setAge(int $age): void { $this->age = $age; }
    function setFoods(array $foods): void { $this->foods = $foods; }
}

class AddressInDto extends InDto {
    /**
     * @rule 

//Configuring class to validation
DTOPHP\Configuration::setValidatorEngine('MyNameSpace\To\LaravelValidator');

class LaravelValidator implements ValidatorInterface {

    /**
     * @param OutputInterface $dto
     * @param array $rules
     * @return void
     */
    public function handlerDtoValidator(OutputInterface $dto, array $rules): void {
        $validator = Validator::make($dto->toArray(), $rules);

        if ($validator->fails()) {
            Response::make()
                    ->create($validator->errors()->all(), 400)
                    ->send();
            exit;
        }
    }

}

class UserOutDto extends OutDto {
    private $name;
    private $email;
    private $address;
    private $age;
    private $foods;
    function __construct(string $name, string $email, AddressOutDto $address, int $age, array $foods) {
        $this->name = $name;
        $this->email = $email;
        $this->address = $address;
        $this->age = $age;
        $this->foods = $foods;
    }
    function getName(): string { return $this->name; }
    function getEmail(): string { return $this->email; }
    function getAddress(): AddressOutDto { return $this->address; }
    function getAge(): int { return $this->age; }
    function getFoods(): array { return $this->foods; }
}

class AddressOutDto extends OutDto {
    private $address;
    private $country;
    private $state;
    function __construct(string $address, string $country, string $state) {
        $this->address = $address;
        $this->country = $country;
        $this->state = $state;
    }
    function getAddress(): string { return $this->address; }
    function getCountry(): string { return $this->country; }
    function getState(): string { return $this->state; }
}

$addressOutDto = new AddressOutDto('Av. Dr. Heitor Penteado', 'Brazil', 'São Paulo');
$userOutDto = new UserOutDto('My Name', '[email protected]', $addressOutDto, 30, ['Hamburger', 'Pizza']);

var_export($userOutDto->toArray());
//print array ( 'name' => 'My Name', 'email' => '[email protected]', 'address' => array ( 'address' => 'Av. Dr. Heitor Penteado', 'country' => 'Brazil', 'state' => 'São Paulo', ), 'age' => 30, 'foods' => array ( 0 => 'Hamburger', 1 => 'Pizza', ), )

var_export($userOutDto->toJson()); //print '{"name":"My Name","email":"[email protected]","address":{"address":"Av. Dr. Heitor Penteado","country":"Brazil","state":"S\\u00e3o Paulo"},"age":30,"foods":["Hamburger","Pizza"]}'