mbernet / dto-parser
A DTO Parser to convert array to object magically
v1.0.0
2020-11-26 09:54 UTC
Requires
- php: >=7.1
This package is auto-updated.
Last update: 2025-03-26 21:27:05 UTC
README
Parse simple array data to well defined PHP objects. Very usefull to strict type data before json encoding.
Just extend your DTO object with BaseDTO
Example
class SuccessDTO extends BaseDTO { public $message; public $id; /** * SuccessDTO constructor. * * @param $message * @param $id */ public function __construct(string $message, $id = null) { $this->message = $message; $this->id = $id; } }
Now, in the controller
$successObject = SuccessDTO::toDTO(['message' => 'success', 'id' => 23]); //Get instance of SuccessDTO class
Alternativelly you can use toDTOArray to parse multiple rows
$successes = SuccessDTO::toDTOArray([['message' => 'success', 'id' => 23],['message' => 'error', 'id' => null]]);