jn-jairo / laravel-cast
Cast for Laravel.
Requires
- php: ^7.2|^8.0
- illuminate/encryption: ^5.8|^6.0|^7.0|^8.0|^9.0
- illuminate/support: ^5.8|^6.0|^7.0|^8.0|^9.0
- ramsey/uuid: ^3.8|^4.0
- thecodingmachine/safe: ^1.1|^2.1
Requires (Dev)
- moontoast/math: ^1.1
- orchestra/testbench: ^3.8|^4.0|^5.0|^6.0|^7.0
- php-decimal/php-decimal: ^1.1
- phpunit/phpunit: ^8.3|^9.0
- squizlabs/php_codesniffer: ^3.6
Suggests
- moontoast/math: Required to use ordered UUIDs (^1.1).
- php-decimal/php-decimal: Required to use decimal type (^1.1).
README
Cast for Laravel
This package provide cast for Laravel.
Requirements
- Laravel Framework >= 5.8
Installation
You can install the package via composer:
composer require jn-jairo/laravel-cast
The CastServiceProvider
will be automatically registered for you.
Usage
Both contract \JnJairo\Laravel\Cast\Contracts\Cast
and facade \JnJairo\Laravel\Cast\Facades\Cast
are available.
There are three methods Cast::cast()
, Cast::castDb()
and Cast::castJson()
.
Cast::cast()
casts toPHP
typesCast::castDb()
casts todatabase
typesCast::castJson()
casts tojson
types
All methods accept three parameters mixed $value
, string $type
and optionally string $format
.
Examples
print_r(Cast::cast('{"foo":"bar"}', 'array')); /* Array ( [foo] => bar ) */ print_r(Cast::castDb(1234.555, 'decimal', '10:2')); // 1234.56 print_r(Cast::castDb(['foo' => 'bar'], 'json')); // {"foo":"bar"} print_r(Cast::castJson(new DateTime('01 jan 2000'), 'date')); // 2000-01-01
Types available
int
,integer
float
,real
,double
decimal
bool
,boolean
date
datetime
timestamp
json
array
object
collection
string
,text
uuid
encrypted
enum
Format parameter
- decimal -
precision:places|(up|down|ceiling|floor|half_up|half_down|half_even|half_odd|truncate)
. Example:10:2|half_up
,10:2
,2
,half_up
. Default:28:2|half_up
. The decimal type uses the https://php-decimal.io extension, to use this type runcomposer require php-decimal/php-decimal:^1.1
and install the decimal extension. - date - Example:
Y-m-d
. Default:Y-m-d
. - datetime, timestamp - Example:
Y-m-d H:i:s
. Default:Y-m-d H:i:s
. - uuid -
(uuid1|uuid4|ordered)
. Example:uuid1
. Default:uuid4
. Empty string value will return a new UUID. To use ordered UUID format runcomposer require moontoast/math:^1.1
. - encrypted -
type:format
. Example:date:Y-m-d
. Default:
$decrypted = Cast::cast($value, 'encrypted'); $encrypted = Cast::castDb($value, 'encrypted'); $decrypted = Cast::castJson($value, 'encrypted');
- enum - Example:
MyEnum::class
. Default:
enum MyEnum : int { case foo = 1; case bar = 2; } Cast::cast(1, 'enum', MyEnum::class); // MyEnum::foo Cast::castDb(MyEnum::foo, 'enum', MyEnum::class); // 1 Cast::castJson(MyEnum::foo, 'enum', MyEnum::class); // 1
It can be a instance of \Illuminate\Contracts\Support\Arrayable
or \Illuminate\Contracts\Support\Jsonable
.
use Illuminate\Contracts\Support\Arrayable; enum MyEnum : string implements Arrayable { case foo = 1; case bar = 2; public function description() : string { return match ($this) { self::foo => 'foo description', self::bar => 'bar description', }; } public function toArray() { return [ 'name' => $this->name, 'value' => $this->value, 'description' => $this->description(), ]; } } Cast::cast(1, 'enum', MyEnum::class); // MyEnum::foo Cast::castDb(MyEnum::foo, 'enum', MyEnum::class); // 1 Cast::castJson(MyEnum::foo, 'enum', MyEnum::class); // [ // 'name' => 'foo', // 'value' => 1, // 'description' => 'foo description' // ]
Custom types
To create a custom type just implements the contract \JnJairo\Laravel\Cast\Contracts\Type
.
class CustomType implements \JnJairo\Laravel\Cast\Contracts\Type { // ... }
Publish the configuration to config/cast.php
.
php artisan vendor:publish --provider=JnJairo\\Laravel\\Cast\\CastServiceProvider
Set the new type in the configuration.
// config/cast.php return [ 'types' => [ 'custom_type' => CustomType::class, ], ];
And the custom type will be available.
Cast::cast('foo', 'custom_type');
License
The MIT License (MIT). Please see License File for more information.