DTOs, or Data Transfer Objects, can be used for so much. Since PHP 8 was released, creating these fantastic classes in your projects has never been easier.
final class ChirpObject implements DataObjectContract { public function __construct( public readonly string $user, public readonly string $message, ) {} public function toArray(): array { return [ 'message' => $this->message, 'user_id' => $this->user, ]; }}
Creating a Data Object Factory will allow us to control how the Data Objects are created and allow us to transform the incoming request into something closer to how we want to work in our application.
I used to add static creation methods to my Data Objects - but it felt like I was mixing the purpose of the Data Object itself.