Property AttributeHydration Stage
CastWith
CastWith allows you to automatically cast values during DTO hydration. You can cast to DTOs, or use built-in casters for primitives, enums, and dates.
CastWith accepts any of the following:
- A DTO class-string (e.g.
User::class) to auto-cast arrays/collections to that DTO - A caster class-string (e.g.
PrimitiveCaster::class) - A caster instance (e.g.
new PrimitiveCaster('int'))
Casting to DTOs
Automatically cast a property into a specific DTO (or array of DTOs):
php
use Alamellama\Carapace\Attributes\CastWith;
class Account extends Data
{
public function __construct(
public string $name,
#[CastWith(User::class)]
/** @var User[] */
public array $users,
) {}
}php
$account = Account::from([
'name' => 'Me, Myself and I',
'users' => [
['name' => 'John', 'email' => 'john@example.com'],
['name' => 'Jane', 'email' => 'jane@example.com'],
],
]);TIP
The @var annotation helps IDEs understand the type of the users property. Carapace will automatically cast each array item to the specified DTO type via CastWith.
Built-in casters
- Date and time: DateTime Caster
- Enums: Enum Caster
- Primitive types: Primitive Caster
Custom Casters
For information on creating your own custom casters, see the Custom Casters guide in the Advanced section.
