Skip to content

Commit

Permalink
Update README.md to add more documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickescobedo committed Jul 24, 2024
1 parent 245cd70 commit 1b54ea1
Showing 1 changed file with 75 additions and 1 deletion.
76 changes: 75 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,78 @@ class Cast extends FormRequest
}
}
```
`$request->castedInput('toBoolean')` will return a boolean value.
`$request->castedInput('toBoolean')` will return a boolean value.

## Available Casts
- int
- integer
- float
- double
- decimal
- string
- boolean
- object
- array
- json
- collection
- date
- datetime
- immutable_date
- immutable_datetime
- timestamp


## Custom Casts
Custom casts allow for more complex casting logic. Enums and custom classes can be used. Custom cast classes must implement `NickEscobedo\Cambia\CastsRequestAttributes`.

### Enum
```php
enum Status: string
{
case Pending = 'pending';
}
```
```php
public function casts(): array
{
return [
'status' => Status::class,
];
}
```

### Custom Cast Class
The class must implement `NickEscobedo\Cambia\CastsRequestAttributes`.
```php
use Illuminate\Http\Request;
use NickEscobedo\Cambia\CastsRequestAttributes;

class JsonCast implements CastsRequestAttributes
{

public function get(Request $request, string $key, mixed $value, array $attributes)
{
return json_decode($value, true);
}
}
```
```php
class Cast extends FormRequest
{
use CastRequestAttributes;

public function rules(): array
{
return [
'toBoolean' => 'string', // Fields not present in validation will not cast
];
}

public function casts(): array
{
return [
'toBoolean' => JsonCast::class,
];
}
}
```

0 comments on commit 1b54ea1

Please sign in to comment.