Links

Available Types

Array

For JSON strings, it will convert into an array, for other types, it will wrap them in an array.
protected function casts(): array
{
return [
'property' => new ArrayCast(),
];
}
If you want to cast all the elements inside the array, you can pass a Castable to the ArrayCast constructor. Let's say that you want to convert all the items inside the array into integers:
protected function casts(): array
{
return [
'property' => new ArrayCast(new IntegerCast()),
];
}
This works with all Castable, including DTOCast and ModelCast for nested data.

Boolean

For string values, this uses the filter_var function with the FILTER_VALIDATE_BOOLEAN flag.
protected function casts(): array
{
return [
'property' => new BooleanCast(),
];
}

Carbon

This accepts any value accepted by the Carbon constructor. If an invalid value is found it will throw a \WendellAdriel\ValidatedDTO\Exceptions\CastException exception.
protected function casts(): array
{
return [
'property' => new CarbonCast(),
];
}
You can also pass a timezone when defining the cast if you need that will be used when casting the value.
protected function casts(): array
{
return [
'property' => new CarbonCast('Europe/Lisbon'),
];
}
You can also pass a format when defining the cast to be used to cast the value. If the property has a different format than the specified it will throw a \WendellAdriel\ValidatedDTO\Exceptions\CastException exception.
protected function casts(): array
{
return [
'property' => new CarbonCast('Europe/Lisbon', 'Y-m-d'),
];
}

CarbonImmutable

This accepts any value accepted by the CarbonImmutable constructor. If an invalid value is found it will throw a \WendellAdriel\ValidatedDTO\Exceptions\CastException exception.
protected function casts(): array
{
return [
'property' => new CarbonImmutableCast(),
];
}
You can also pass a timezone when defining the cast if you need that will be used when casting the value.
protected function casts(): array
{
return [
'property' => new CarbonImmutableCast('Europe/Lisbon'),
];
}
You can also pass a format when defining the cast to be used to cast the value. If the property has a different format than the specified it will throw a \WendellAdriel\ValidatedDTO\Exceptions\CastException exception.
protected function casts(): array
{
return [
'property' => new CarbonImmutableCast('Europe/Lisbon', 'Y-m-d'),
];
}

Collection

For JSON strings, it will convert into an array first, before wrapping it into a Collection object.
protected function casts(): array
{
return [
'property' => new CollectionCast(),
];
}
If you want to cast all the elements inside the Collection, you can pass a Castable to the CollectionCast constructor. Let's say that you want to convert all the items inside the Collection into integers:
protected function casts(): array
{
return [
'property' => new CollectionCast(new IntegerCast()),
];
}
This works with all Castable, including DTOCast and ModelCast for nested data.

DTO

This works with arrays and JSON strings. This will validate the data and also cast the data for the given DTO.
This will throw a Illuminate\Validation\ValidationException exception if the data is not valid for the DTO.
This will throw a WendellAdriel\ValidatedDTO\Exceptions\CastException exception if the property is not a valid array or valid JSON string.
This will throw a WendellAdriel\ValidatedDTO\Exceptions\CastTargetException exception if the class passed to the DTOCast constructor is not a ValidatedDTO instance.
protected function casts(): array
{
return [
'property' => new DTOCast(UserDTO::class),
];
}

Enum

This will try to convert the value to the given Enum class. It works with UnitEnum and BackedEnum.
This will throw a WendellAdriel\ValidatedDTO\Exceptions\CastException exception if the property is not a valid enum value.
This will throw a WendellAdriel\ValidatedDTO\Exceptions\CastTargetException exception if the class passed to the EnumCast constructor is not a Enum instance.
protected function casts(): array
{
return [
'property' => new EnumCast(MyEnum::class),
];
}

Float

If a not numeric value is found, it will throw a WendellAdriel\ValidatedDTO\Exceptions\CastException exception.
protected function casts(): array
{
return [
'property' => new FloatCast(),
];
}

Integer

If a not numeric value is found, it will throw a WendellAdriel\ValidatedDTO\Exceptions\CastException exception.
protected function casts(): array
{
return [
'property' => new IntegerCast(),
];
}

Model

This works with arrays and JSON strings.
This will throw a WendellAdriel\ValidatedDTO\Exceptions\CastException exception if the property is not a valid array or valid JSON string.
This will throw a WendellAdriel\ValidatedDTO\Exceptions\CastTargetException exception if the class passed to the ModelCast constructor is not a Model instance.
protected function casts(): array
{
return [
'property' => new ModelCast(User::class),
];
}

Object

This works with arrays and JSON strings.
This will throw a WendellAdriel\ValidatedDTO\Exceptions\CastException exception if the property is not a valid array or valid JSON string.
protected function casts(): array
{
return [
'property' => new ObjectCast(),
];
}

String

If the data can't be converted into a string, this will throw a WendellAdriel\ValidatedDTO\Exceptions\CastException exception.
protected function casts(): array
{
return [
'property' => new StringCast(),
];
}