Download the PHP package yzen.dev/plain-to-class without Composer

On this page you can find all versions of the php package yzen.dev/plain-to-class. 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 plain-to-class

ClassTransformer

Packagist Version GitHub Workflow Status (with branch) Coverage License readthedocs Packagist Downloads Packagist Downloads

Mutation testing badge type-coverage psalm-level

Alas, I do not speak English, and the documentation was compiled through google translator :( I will be glad if you can help me describe the documentation more correctly :)

This library will allow you to easily convert any data set into the object you need. You are not required to change the structure of classes, inherit them from external modules, etc. No dancing with tambourines - just data and the right class.

It is considered good practice to write code independent of third-party packages and frameworks. The code is divided into services, domain zones, various layers, etc.

To transfer data between layers, the DataTransfer Object (DTO) template is usually used. A DTO is an object that is used to encapsulate data and send it from one application subsystem to another.

Thus, services/methods work with a specific object and the data necessary for it. At the same time, it does not matter where this data was obtained from, it can be an http request, a database, a file, etc.

Accordingly, each time the service is called, we need to initialize this DTO. But it is not effective to compare data manually each time, and it affects the readability of the code, especially if the object is complex.

This is where this package comes to the rescue, which takes care of all the work with mapping and initialization of the necessary DTO.

Documentation

Installation

The package can be installed via composer:

Note: The current version of the package supports only PHP 8.0 +.

For PHP version 7.4, you can read the documentation in version v0.*.

Usage

Common use case:

Base

Result:

Also for php 8 you can pass named arguments:

If the property is not of a scalar type, but a class of another DTO is allowed, it will also be automatically converted.

Output:

Collection

If you have an array of objects of a certain class, then you must specify the ConvertArray attribute for it, passing it to which class you need to bring the elements.

You can also specify a class in PHP DOC, but then you need to write the full path to this class array <\DTO\ProductDTO>. This is done in order to know exactly which instance you need to create. Since Reflection does not provide out-of-the-box functions for getting the use * file. Besides use *, you can specify an alias, and it will be more difficult to trace it. Example:

Anonymous array

In case you need to convert an array of data into an array of class objects, you can implement this using the transformCollection method.

As a result of this execution, you will get an array of ProductDTO objects

You may also need a piecemeal transformation of the array. In this case, you can pass an array of classes, which can then be easily unpacked.

Result:

Writing style

A constant problem with the style of writing, for example, in the database it is snake_case, and in the camelCase code. And they constantly need to be transformed somehow. The package takes care of this, you just need to specify the WritingStyle attribute on the property:

Alias

Various possible aliases can be set for the property, which will also be searched in the data source. This can be useful if the DTO is generated from different data sources.

Custom setter

If a field requires additional processing during its initialization, you can mutator. To define a mutator, define a set{Attribute}Attribute method on your class where {Attribute} is cased name of the property you wish to access. This mutator will be automatically called when we attempt to set the value of the real_address attribute on the model:

After Transform

Inside the class, you can create the afterTransform method, which will be called immediately after the conversion is completed. In it, we can describe our additional verification or transformation logic by already working with the state of the object.

Custom transform

If you need to completely transform yourself, then you can create a transform method in the class. In this case, no library processing is called, all the responsibility of the conversion passes to your class.

Cache

The package supports a class caching mechanism to avoid the cost of reflection. This functionality is recommended to be used only if you have very voluminous classes, or there is a cyclic transformation of multiple entities. On ordinary lightweight DTO, there will be only 5-10%, and this will be unnecessary access in the file system.

You can enable caching by passing the config to the hydrator constructor:

Comparison

I also made a comparison with current analogues and here are the main disadvantages

Below is an example of my benchmark comparison

https://github.com/yzen-dev/php-dto-transform-benchmark image


All versions of plain-to-class with dependencies

PHP Build Version
Package Version
Requires php Version ^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 yzen.dev/plain-to-class contains the following files

Loading the files please wait ....