Casts API
Complete API reference for cast classes.
Primitive Casts
Section titled “Primitive Casts”StringCast
Section titled “StringCast”#[Cast(StringCast::class)]public readonly string $name;IntCast
Section titled “IntCast”#[Cast(IntCast::class)]public readonly int $age;FloatCast
Section titled “FloatCast”#[Cast(FloatCast::class)]public readonly float $price;BoolCast
Section titled “BoolCast”#[Cast(BoolCast::class)]public readonly bool $active;ArrayCast
Section titled “ArrayCast”#[Cast(ArrayCast::class)]public readonly array $tags;Date & Time Casts
Section titled “Date & Time Casts”DateTimeCast
Section titled “DateTimeCast”#[Cast(DateTimeCast::class)]public readonly Carbon $createdAt;
#[Cast(DateTimeCast::class, format: 'Y-m-d H:i:s')]public readonly Carbon $updatedAt;DateCast
Section titled “DateCast”#[Cast(DateCast::class)]public readonly Carbon $birthDate;TimeCast
Section titled “TimeCast”#[Cast(TimeCast::class)]public readonly Carbon $startTime;TimestampCast
Section titled “TimestampCast”#[Cast(TimestampCast::class)]public readonly Carbon $timestamp;Enum Casts
Section titled “Enum Casts”EnumCast
Section titled “EnumCast”#[Cast(EnumCast::class)]public readonly Status $status;BackedEnumCast
Section titled “BackedEnumCast”#[Cast(BackedEnumCast::class)]public readonly Role $role;Collection Casts
Section titled “Collection Casts”CollectionCast
Section titled “CollectionCast”#[Cast(CollectionCast::class)]public readonly Collection $items;DataCollectionCast
Section titled “DataCollectionCast”#[Cast(DataCollectionCast::class, itemClass: UserDTO::class)]public readonly DataCollection $users;Object Casts
Section titled “Object Casts”ObjectCast
Section titled “ObjectCast”#[Cast(ObjectCast::class, class: Address::class)]public readonly Address $address;DTOCast
Section titled “DTOCast”#[Cast(DTOCast::class, class: UserDTO::class)]public readonly UserDTO $user;Security Casts
Section titled “Security Casts”EncryptedCast
Section titled “EncryptedCast”#[Cast(EncryptedCast::class)]public readonly string $ssn;HashedCast
Section titled “HashedCast”#[Cast(HashedCast::class)]public readonly string $password;Special Casts
Section titled “Special Casts”JsonCast
Section titled “JsonCast”#[Cast(JsonCast::class)]public readonly array $metadata;DecimalCast
Section titled “DecimalCast”#[Cast(DecimalCast::class, precision: 2)]public readonly string $price;UuidCast
Section titled “UuidCast”#[Cast(UuidCast::class)]public readonly string $uuid;IpAddressCast
Section titled “IpAddressCast”#[Cast(IpAddressCast::class)]public readonly string $ipAddress;UrlCast
Section titled “UrlCast”#[Cast(UrlCast::class)]public readonly string $website;Custom Casts
Section titled “Custom Casts”Creating a Custom Cast
Section titled “Creating a Custom Cast”use event4u\DataHelpers\SimpleDTO\Contracts\Cast;
class UpperCaseCast implements Cast{ public function cast(mixed $value): string { return strtoupper((string) $value); }
public function uncast(mixed $value): string { return strtolower((string) $value); }}
// Usage#[Cast(UpperCaseCast::class)]public readonly string $name;See Also
Section titled “See Also”- Type Casting Guide - Complete guide
- Custom Casts - Create custom casts
- Casting Attributes - Cast attribute reference