PHP code example of eve / dto

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

    

eve / dto example snippets


// UserService.php
public function createUser(array $attributes): User
{
    return User::create($attributes);
}

// UserController.php
public function store(CreateUserRequest $request)
{
    $this->userService->create($request->toArray());
}

// UserCreationData.php
class UserCreationData
{
    public string $email;
    public string $password;
    public ?int $age;
}

// UserService.php
public function createUser(UserCreationData $data): User
{
    return User::create($data->toArray());
}

// UserController.php
public function store(CreateUserRequest $request)
{
    $this->userService->create(UserCreationData::fromRequest($request));
}

class UserCreationData extends \Eve\DTO\DataTransferObject
{
    public string $email;
    public string $password;
    public ?int $age;
}

$data = UserCreationData::make([
   'email' => '[email protected]',
   'password' => 'SoSecureWow',
   'age' => 30,
]);

$data = UserCreationData::make();
$data->email = '[email protected]';
$data->password = 'SoSecureWow';
$data->age = 30;

$data = UserCreationData::make()
    ->set('email', '[email protected]')
    ->set([
       'password' => 'SoSecureWow',
       'age' => 30,
    ]);

UserCreationData::make(['nope' => 'bar']); // throws "Public property $nope does not exist in class UserCreationData"

$arr = $data->toArray(); // ['email' => '[email protected]', 'password' => 'SoSecureWow', 'age' => 30]

$data = UserCreationData::make();

// Only setting email now
$data->email = '[email protected]';

$arr = $data->toArray(); // ['email' => '[email protected]']

class UserCreationData extends \Eve\DTO\DataTransferObject
{
    public string $email;
    public string $password;
    public UserInformationData $information;
}

class UserInformationData extends \Eve\DTO\DataTransferObject
{
    public int $age;
}

$data = UserCreationData::make([
   'email' => '[email protected]',
   'password' => 'SoSecureWow',
   'information' => UserInformationData::make(['age' => 30]),
]);

$data->toArray(); // ['email' => '[email protected]', 'password' => 'SoSecureWow', ['information' => ['age' => 30]]

  $data = UserCreationData::make([
     'email' => '[email protected]',
     'password' => 'SoSecureWow',
     'age' => 30,
  ]);
  
  $data->only('email', 'password')->toArray(); // ['email' => '[email protected]', 'password' => 'SoSecureWow']
  

  $data = UserCreationData::make([
     'email' => '[email protected]',
     'password' => 'SoSecureWow',
     'age' => 30,
  ]);
  
  $data->except('email', 'password')->toArray(); // ['age' => 30]
  

  $data = UserCreationData::make([
     'email' => '[email protected]',
     'password' => 'SoSecureWow',
     'age' => null,
  ]);
  
  $data->compact()->toArray(); // ['email' => '[email protected]', 'password' => 'SoSecureWow']
  

  $data = UserCreationData::make([
     'email' => '[email protected]',
     'password' => 'SoSecureWow',
  ]);

  $data->get('email'); // '[email protected]'
  $data->password; // 'SoSecureWow'
  
  $data->age; // throws "UserCreationData::$age must not be accessed before initialization."
  $data->get('age', 30); // 30

  $data->get('nope'); // throws "Public property $nope does not exist in class UserCreationData."
  $data->nope; // throws "Public property $nope does not exist in class UserCreationData."