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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

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.

Total Downloads Latest Stable Version Latest Unstable Version

Contents

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

PHP Build Version
Package Version
Requires php Version ^7.4|^8.0
ext-pdo Version *
ext-json Version *
illuminate/database Version ^7.0|^8.0
illuminate/support Version ^7.0|^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package isakzhanov-r/laravel-value-object contains the following files

Loading the files please wait ....