dragon-code/simple-data-transfer-object
dragon-code/simple-data-transfer-object offers a lightweight way to define typed data transfer objects in Laravel and PHP applications, helping you move structured data between layers with less boilerplate and clearer, more predictable code. Ideal for request payloads, API responses, and service bou...
This is a tiny, framework-agnostic DTO helper rather than a full Laravel data layer. Treat it as a typed property bag for moving data across boundaries: request → DTO → action/service, job payload, or API response.
composer show dragon-code/simple-data-transfer-object
vendor/dragon-code/simple-data-transfer-object/src/DataTransferObject.phpfromArray(), from(), toArray(), nested DTO behavior, and property visibility expectations.use DragonCode\SimpleDataTransferObject\DataTransferObject;
final class CreateOrderData extends DataTransferObject
{
public int $customerId;
/** @var array<int, array{sku: string, qty: int}> */
public array $lines;
public ?string $coupon = null;
public static function fromValidated(array $input): self
{
$dto = new self();
$dto->customerId = (int) $input['customer_id'];
$dto->lines = array_map(
fn (array $line): array => [
'sku' => (string) $line['sku'],
'qty' => (int) $line['qty'],
],
$input['lines'] ?? []
);
$dto->coupon = $input['coupon'] ?? null;
return $dto;
}
}
$request->validated() into the DTO, and pass the DTO to an action or service. The action should not receive the request object or raw validated array.fromValidated(), fromModel(), or a dedicated mapper close to the DTO. Centralizing casts, defaults, and key renaming is more valuable than saving a few lines with generic hydration.public readonly properties on PHP 8.1+ where possible, avoid setters, and create a new DTO for updates. Public mutable properties are fine for tiny internal payloads, but they invite accidental changes.array_map() plus a named constructor. Do not assume the package auto-hydrates nested objects, dates, enums, or collections unless you have verified it in the installed source.$dto->toArray() before logging, queueing, or JSON responses. Convert dates to ISO strings and backed enums to scalar values in your mapper/serializer rather than relying on json_encode($dto) unless the base class implements JsonSerializable.fromValidated() and toArray() for required fields, nullables, nested arrays, and type casts. A small DTO is easy to test; use that to compensate for the package’s low adoption.Error. Initialize nullable/default properties and test every required key."1" to an int property fails under strict typing. Cast in the mapper ((int), (string), (bool)) instead of trusting request input.toArray() visibility: many simple DTO implementations serialize only public properties. Keep DTO state public or write a custom serializer; private/protected properties may disappear.array_key_exists() when the distinction matters. $input['x'] ?? null collapses missing and null.Carbon, enum instances, or floats with currency semantics leak into serialized payloads.$dto->toArray() and get_object_vars($dto); check for uninitialized properties and visibility. If hydration fails, compare the installed base class factory behavior with the tests.BaseData extends DataTransferObject, add mapping traits or named constructors there, and keep package usage isolated. Never edit vendor code.How can I help you explore Laravel packages today?