Skip to content

Commit

Permalink
Add callable casts docs
Browse files Browse the repository at this point in the history
  • Loading branch information
WendellAdriel committed May 30, 2023
1 parent 7173c61 commit 0645330
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,8 @@ protected function casts(): array

## Create Your Own Type Cast

### Castable classes

You can easily create new `Castable` types for your project by implementing the `WendellAdriel\ValidatedDTO\Casting\Castable`
interface. This interface has a single method that must be implemented:

Expand Down Expand Up @@ -911,6 +913,36 @@ class CustomDTO extends ValidatedDTO
}
```

### Callable Casts

You can also create new `Castable` types for your project by using a `callable/callback`:

```php
class CustomDTO extends ValidatedDTO
{
protected function rules(): array
{
return [
'url' => ['required', 'url'],
];
}

protected function defaults(): array
{
return [];
}

protected function casts(): array
{
return [
'url' => function (string $property, mixed $value) {
return new URLWrapper($value);
},
];
}
}
```

## Casting Eloquent Model properties to DTOs

You can easily cast any **Eloquent Model** properties to your **DTOs**:
Expand Down

0 comments on commit 0645330

Please sign in to comment.