Flexible DTO Documentation

Creating a DTO

To create a DTO, create a class and extend the DataTransferObject class.

use Mass6\FlexibleDTO\DataTransferObject;

class UserDTO extends DataTransferObject
{   
}

Instantiating a DTO

To instantiate a DTO, pass an associative array into the class constructor. Alternatively, you can use the static make constructor method.


# Using an associative array $userDTO = new UserDTO([ 'first_name' => 'John', 'last_name' => 'Doe', ]); # Using the static make method $userDTO = UserDTO::make([ 'first_name' => 'John', 'last_name' => 'Doe', ]);

If you define the list of allowed properties via the allowedProperties() method in your DTO, you can also construct a DTO by passing in values in the same order as the properties are defined.

class UserDTO extends DataTransferObject
{
    protected function allowedProperties(): array
    {
        return [
            'first_name',
            'last_name',
        ];
    }
}

# Pass in values in the same order as the properties are defined.
$userDTO = new UserDTO('John', 'Doe');