Download the PHP package jn-jairo/laravel-cast without Composer
On this page you can find all versions of the php package jn-jairo/laravel-cast. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jn-jairo/laravel-cast
More information about jn-jairo/laravel-cast
Files in jn-jairo/laravel-cast
Package laravel-cast
Short Description Cast for Laravel.
License MIT
Homepage https://github.com/jn-jairo/laravel-cast
Informations about the package laravel-cast
Cast for Laravel
This package provide cast for Laravel.
Version Compatibility
Laravel | Eloquent Cast |
---|---|
5.8.x | 1.x |
6.x | 1.x |
7.x | 1.x |
8.x | 2.x |
9.x | 2.x |
10.x | 2.x |
11.x | 2.x |
12.x | 2.x |
Installation
You can install the package via composer:
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
Types available
int
,integer
float
,real
,double
decimal
bool
,boolean
date
datetime
timestamp
json
array
object
collection
string
,text
uuid
encrypted
compressed
base64
pipe
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 -
(one|all),base64:key,cipher
. Empty key and cipher uses the laravel configurationapp.key
andapp.cipher
. Example:base64:N38XBrdzg505959nDEqefo6fNpeTmGy0wTBHRSxrpcQ=,aes-256-cbc
. Default:one
.one
- allow double encryption, decrypts only one time.all
- prevents double encryption, decrypts recursively all encryptions.
-
compressed -
(always|smaller),(one|all),level,(raw|deflate|gzip)
. Example:smaller,all,9,gzip
. Default:always,one,-1,raw
.always
- always uses the result of compression even if the result is bigger than the original value.smaller
- only uses the result of compression if the result is smaller than the original value.one
- allow double compression, decompresses only one time.all
- prevents double compression, decompresses recursively all compressions.- level - level of compression, from
0
(no compression) to9
(maximum compression). If-1
is used, the default compression of the zlib library is used. raw
- uses the encodingZLIB_ENCODING_RAW
withgzdeflate
andgzinflate
.deflate
- uses the encodingZLIB_ENCODING_DEFLATE
withgzcompress
andgzuncompress
.gzip
- uses the encodingZLIB_ENCODING_GZIP
withgzencode
andgzdecode
.
-
base64 -
(one|all),prefix
. Example:base64:
. Default:one
.one
- allow double encoding, decodes only one time.all
- prevents double encoding, decodes recursively all encodings (requires prefix).
-
pipe -
|type:format|type:format|...|direction
. Example:|encrypted|compressed|array|
. Default:||php:>,db:<,json:>
.- The direction format is
>|<|php:(>|<),db:(>|<),json:(>|<)
,>
follows from left to right and<
follows from right to left. - The first character is used as the type separator, for the other separators in case of conflict with the type separator the
|
separator is used. - Examples of valid parameters:
|encrypted|array|
|encrypted|array|<
|encrypted|array|>,db:<
|encrypted|decimal:2|php:>,db:<,json:>
,encrypted,decimal:2,php:>|db:<|json:>
:encrypted:decimal|2:php|>,db|<,json|>
- The direction format is
- enum - Example:
MyEnum::class
. Default:
It can be a instance of \Illuminate\Contracts\Support\Arrayable
or \Illuminate\Contracts\Support\Jsonable
.
Custom types
To create a custom type just implements the contract \JnJairo\Laravel\Cast\Contracts\Type
.
Or extends the \JnJairo\Laravel\Cast\Types\Type
abstract class.
Publish the configuration to config/cast.php
.
Set the new type in the configuration.
And the custom type will be available.
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-cast with dependencies
illuminate/encryption Version ^8.83|^9.0|^10.0|^11.0|^12.0
illuminate/support Version ^8.83|^9.0|^10.0|^11.0|^12.0
nesbot/carbon Version ^2.66|^3.0
ramsey/uuid Version ^4.7
thecodingmachine/safe Version ^2.4|^3.0