DTOs can be used to transform Incoming Psr7 server requests, json and random arrays to typed classes with validation built in.
Does not use reflection and handles everything with good old classes and methods!
composer require rahimi-ali/php-dtoint(bool $strict = false): IntTypefloat(bool $strict = false): FloatTypestring(bool $strict = false): StringTypebool(bool $strict = false): BoolTypedatetime(string $format = DateTimeInterface::ATOM, string|null $timezone = null, bool $immutable = true): DateTimeTypeembedded(string $class): EmbeddedTypedynamicEmbedded(string|Closure $discriminator, array $types = []): DynamicEmbeddedTypecollection(TypeInterface $type): CollectionType
int(bool $strict = false): IntRuleAutomatically added when declaring a field with IntTypefloat(bool $strict = false): FloatRuleAutomatically added when declaring a field with FloatTypestring(bool $strict = false): StringRuleAutomatically added when declaring a field with StringTypebool(bool $strict = false): BoolRuleAutomatically added when declaring a field with BoolTypearray(): ArrayRuleShould be an array with sequential int keysobject(): ObjectRuleShould be an object or an array with string keys or non-sequential int keysmin(int $min, bool $strict = false): MinRulemax(int $max, bool $strict = false): MaxRuleminLength(int $length, bool $strict = false): MinLengthRulemaxLength(int $length, bool $strict = false): MaxLengthRulein(array $values, bool $strict = false): InRulenotIn(array $values, bool $strict = false): NotInRuleequals(mixed $value, bool $strict = false): EqualsRulenotEqual(mixed $value, bool $strict = false): NotEqual
class AddressDto extends Dto
{
private string $city;
private string $street;
private int $number;
public static function fields(array $data): array
{
return [
'city' => new Field('city', Type::string(true), rules: [Rule::in(['London', 'Paris', 'New York'])]),
'street' => new Field('street', Type::string(true), rules: [Rule::min(5)]),
'number' => new Field('number', Type::int(true), rules: [Rule::min(1), Rule::max(100)]),
];
}
public function getCity(): string
{
return $this->city;
}
public function getStreet(): string
{
return $this->street;
}
public function getNumber(): int
{
return $this->number;
}
}
class UserDto extends Dto
{
private string $name;
private int $age;
private AddressDto $address;
public static function fields(array $data): array
{
return [
'name' => new Field('name', Type::string(true), rules: [Rule::min(5)]),
'age' => new Field('age', Type::int(true), rules: [Rule::min(18)]),
'address' => new Field('address', Type::embedded(AddressDto::class)),
];
}
public function getName(): string
{
return $this->name;
}
public function getAge(): int
{
return $this->age;
}
public function getAddress(): AddressDto
{
return $this->address;
}
}- Because reflection and magic methods were not used private and readonly properties
cannot be used because the parent class cannot access them by default. If you really want private and readonly
properties, override the
setPropertymethod in you DTO:
protected function setProperty(string $property, mixed $value): void
{
$this->{$property} = $value;
}- Code Coverage: 100%
- PHPStan Level: 9
- Infection MSI: 94%
MIT