, (*1)
Laravel Castable
Laravel Castable package is a type converter, input filter or sanitizer. It is possible to do all of these operations. Supported POST
, RAW DATA
, GET
requests methods. We started by inspiring the Laravel Eloquent data cast., (*2)
Requirements
PHP 5.6, 7.0+
Laravel 5.3 (LTS) or Laravel 5.4 (Current), (*3)
Get Started
Firstly, we install package:, (*4)
$ composer require atayahmet/laravel-castable
and then we need add the service provider to the app.php, (*5)
Castable\CastableServiceProvider::class
OK, we done., (*6)
Let's see how to use the laravel-castable., (*7)
Castable types
Types |
string |
integer |
boolean |
float |
double |
real |
unset |
array |
object (stdClass) |
collection |
We created new artisan command that inspired make:request
from laravel built in command., (*8)
$ php artisan make:cast ContactRequest
New form of the form request class:, (*9)
<?php
namespace App\Http\Requests;
use Castable\Castable;
class ContactRequest extends Castable
{
protected $casts = [
'json' => [
//
],
'post' => [
'name' => 'string',
'age' => 'integer',
'student' => 'boolean',
'interests' => 'collection'
],
'query' => [
'save' => 'boolean'
]
];
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}
We added four inputs to casts property, status attibute added to query string parameters and age, student and interests attributes added to post parameters., (*10)
Raw and converted type of the attributes
Post:, (*11)
Name |
Value |
Type |
Cast Type |
name |
Ali |
string |
string |
age |
19 |
string |
integer |
student |
true |
string |
boolean |
interests |
books, computers |
array |
collection |
Query String:, (*12)
Name |
Value |
Type |
Cast Type |
save |
true |
string |
boolean |
To get the above result for:, (*13)
<?php
ContactController extends Controller {
public index(ContactRequest $contactRequest)
{
$contactRequest->cast()->input();
}
}
Get a input:, (*14)
$contactRequest->cast()->input('interests'); // collection
$contactRequest->cast()->input('student') // boolean (true)
$contactRequest->cast()->input('save') // boolean (true)
````
if request is post raw data:
```php
$contactRequest->cast()->json();
$contactRequest->cast()->json('age');
````
**Get original inputs:**
```php
$contactRequest->input();
Get original an input:, (*15)
$contactRequest->input('student'); // string (true)
Original raw data:, (*16)
$contactRequest->json();
You can add presenter to the all post, query and json inputs. This feature gives you a chance to filter inputs., (*17)
Add presenter for post parameters:, (*18)
```php, (*19)
public function PostNameAttribute($value, $original)
{
return ucfirst($value);
}, (*20)
Add presenter for query string parameters:
```php
public function QuerySaveAttribute($value, $original)
{
return $value === true ? 1 : 0;
}
Add presenter for json raw data parameters:, (*21)
```php, (*22)
public function JsonSaveAttribute($value, $original)
{
return ucfirst($name);
}
````, (*23)
License
This package is open-source software licensed under the MIT license., (*24)