A simple and flexible Data Transfer Object (DTO) package for PHP, allowing clean data encapsulation and validation.
- Type-safe properties - Define properties with specific types to ensure consistent data handling.
- Automatic property assignment - Easily map input data to DTO properties.
Install the package via Composer:
composer require mom/data
Define a property class and extend AbstractString
, AbstractInteger
, AbstractFloat
, AbstractCollection
, AbstractDate
, AbstractDataValue
or AbstractBoolean
based on the property type:
<?php
namespace App\User\Properties;
use Mom\Data\AbstractString;
class Uuid extends AbstractString
{
public static function getName(): string
{
return 'uuid';
}
}
Next, create DTO class and extend AbstractData
class:
<?php
namespace App\User;
use Mom\Data\AbstractData;
use App\User\Properties\Uuid;
class User extends AbstractData
{
public function __construct(
private Uuid $uuid,
) {}
public function getUuid(): Uuid
{
return $this->uuid;
}
public function setUuid(Uuid $uuid): User
{
$this->uuid = $uuid;
return $this;
}
}
Once created, you can access the DTO properties as usual:
use App\User\User;
$data = [
'uuid' => '123e4567-e89b-12d3-a456-426614174000',
];
$user = User::fromArray($data);
echo $user->getUuid()->toString(); // Outputs: 123e4567-e89b-12d3-a456-426614174000
Contributions are welcome! Please submit issues or pull requests.
This package is open-sourced software licensed under MIT license