Download the PHP package isakzhanov-r/laravel-value-object without Composer
On this page you can find all versions of the php package isakzhanov-r/laravel-value-object. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download isakzhanov-r/laravel-value-object
More information about isakzhanov-r/laravel-value-object
Files in isakzhanov-r/laravel-value-object
Package laravel-value-object
Short Description Value Object for Eloquent
License MIT
Informations about the package laravel-value-object
Laravel Value Object
Allows you to create value objects in eloquent models, in the form of casts that are then stored in the database, or to represent the data as an object.
Contents
- Installation
- Usage
- Creating
- Validation
- Use in Model
- Transform data
- Serialize and unserialize
- License
Installation
To get the latest version of Laravel Value Object package, simply require the project using Composer:
Instead, you can, of course, manually update the dependency block require
in composer.json
and run composer update
if you want to:
Usage
Creating
To use Value Object
, you need to create a class that will inherit from the abstract ValueObject class
The ValueObject class has mandatory methods for implementation, transform
and rules
The ValueObject inheritor class has two methods for creating an object, via new FooValueObject($value, $key)
and via a static call to the create
function
if the key is not passed to the function argument, it is generated automatically from the class name
Validation
The data in ValueObject must be valid for this, the Illuminate\Validation\ValidatesWhenResolvedTrait
validation trait is used. The same trait is used
in FormRequest
. To define rules for the validator, use the rules
method
If ValueObject is array, then its value is validated in the same way:
For custom error messages, use the messages
method:
You can also declare an authorize
method that returns true
or false
.
Use in Model
To use ValueObject
in Eloquent models, you do not need to add anything, just specify it in $casts
.
Let's say you have an Eloquent Whether
model . You may want to apply transformations to this field or get a value in degrees Celsius, Fahrenheit or Kelvin. If
you use this type of field in multiple models, copying and pasting getAttribute functions can be difficult.
Such valuable items become very useful. Let's see how to do this. First, we created a weather model that will have a temperature field brought to the temperature value object.
We will add the following methods to the temperature object
Objects with a value are stored in the database as a prime number and can be used as follows:
To write to the model, you must use an instance of ValueObject:
You may also use Accessors and Mutators, just like in Eloquent Models.
Transform data
Data transformation is intended for minor manipulations with data, for example, clearing unnecessary characters. This method is performed before validation. The
transformInput
function is also executed in the Eloquent model with both get
and set
methods
Serialize and unserialize
Sometimes objects with a value may not be so simple and may require several fields instead of one. Let's say we have a User
model with an address field that
contains the country, city, street and house number, this field in the database is of the json
type. Then we could define the User
model as we did before:
Then we will be able to define the address value object as follows:
In order to write data, you need to convert an array to a json string and, accordingly, when reading this string from the database, you need to convert it back
to an array, for this we will need the serialize
and unserialize
static methods. These methods are executed only if they are declared.
License
This package is released under theMIT License.
All versions of laravel-value-object with dependencies
ext-pdo Version *
ext-json Version *
illuminate/database Version ^7.0|^8.0
illuminate/support Version ^7.0|^8.0