bpartner / dto
DTO with simple factory
Installs: 1 388
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- illuminate/support: ^8.0|^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0
- phpunit/phpunit: ^9.0
README
Simple create DTO from any array.
Installation
####Requirements:
- PHP 8.0
- Laravel 8 | 9
You can install the package via composer:
composer require bpartner/dto
Usage
Declare you DTO object. You can use any types declarations.
For array or collection you can use PHPDoc annotation with type of data inside. Supported only array and collection.
class DemoDto extends DtoAbstract { //Optional protected const CLASS_FORM_REQUEST = UpdateUserFormRequest::class; /** * DTO fields */ public string $name; public Carbon $date; public DtoOtherObject $otherObject; /** @var collection<\App\Dto\DtoOtherObject> */ public Collection $objectsCollection; /** @var array<\App\Dto\DtoOtherObject> */ public array $objectsArray; /** @var collection<\App\Dto\DtoOtherObject> */ public array $objectsArrayOfCollection; }
Create DTO from any array data (example: request()->all()) by facade Dto
//In any place of your code $data = request()->all(); //array data $dto = Dto::build(DemoDto::class, $data);
or from FormRequest
/** * In controller * * Create FormRequest and assign to CLASS_FORM_REQUEST const in DTO * */ public function store(DemoDto $dto) { //Use $dto made from UpdateUserFormRequest }
Now you can transfer your DTO to any Object as parameter and use:
$name = $dto->name; $objectCollection = $dto->objectsCollection;
You can transform input parameters to any string format by third parameter. (example: first_name -> firstName)
class DemoDto extends DtoAbstract { public string $firstName; } $inputData = [ 'first_name' => 'John Doe' ]; $dto = Dto::build(DemoDto::class, $inputData, DtoFactory::CAMEL_CASE);
Custom mapping
You can create DTO with custom mapping. Add static method withMap()
public static function withMap(array $data): DtoInterface { $mappedData = [ 'dto_param' => $data['some_param'], ]; return new static($mappedData); } //Client code $dto = Dto::build(DemoDto::class, request()->all());
Transform your DTO to array or flat array.
$array = $dto->toArray(); array:4 [ "name" => "Demo data" "date" => "06-11-2021" "phone" => array:2 [ "type" => "home" "number" => "500-123-123" ] "phones" => array:2 [ 0 => array:2 [ "type" => "work" "number" => "500-123-122" ] 1 => array:2 [ "type" => "private" "number" => "500-123-124" ] ] ] $flat = $dto->flatArray(); array:5 [ "name" => "Demo data" "date" => "06-11-2021" "type" => "home" "number" => "500-123-123" "phones" => array:2 [ 0 => array:2 [ "type" => "work" "number" => "500-123-122" ] 1 => array:2 [ "type" => "private" "number" => "500-123-124" ] ] ]
Important! Collections and nested arrays are not flip to flat array.
Credits
License
The MIT License (MIT). Please see License File for more information.