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.

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.

You can also pass a timezone when defining the cast if you need that will be used when casting the value.

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.

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.

You can also pass a timezone when defining the cast if you need that will be used when casting the value.

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.

Collection

For JSON strings, it will convert into an array first, before wrapping it into a Collection object.

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:

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.

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.

Float

If a not numeric value is found, it will throw a WendellAdriel\ValidatedDTO\Exceptions\CastException exception.

Integer

If a not numeric value is found, it will throw a WendellAdriel\ValidatedDTO\Exceptions\CastException exception.

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.

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.

String

If the data can't be converted into a string, this will throw a WendellAdriel\ValidatedDTO\Exceptions\CastException exception.

Last updated

Was this helpful?