Download the PHP package morebec/orkestra-normalization without Composer
On this page you can find all versions of the php package morebec/orkestra-normalization. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download morebec/orkestra-normalization
More information about morebec/orkestra-normalization
Files in morebec/orkestra-normalization
Package orkestra-normalization
Short Description Orkestra Component allowing to easily normalize complex object graphs to arrays of primitives for persistence purposes
License Apache-2.0
Informations about the package orkestra-normalization
Normalization
The normalization component allows to easily normalize complex object graphs to arrays of primitives for persistence purposes.
Essentially, it makes transforming POPOs into human-readable, platform independent serializable arrays of primitives a breeze.
It can be used to:
- Serialize in any format that supports PHP arrays and primitives.
- Dump objects to an SQL database without the need for an ORM.
- Dump complex object graphs to a document store.
- Load an object in memory from a serialized representation.
- Convert HTTP requests to Typed Objects.
Installation
Usage
Let's take an example of an object graph:
Here's how one can normalize an object:
This last representation can easily be serialized to json, xml, yaml or any other format.
Denormalization
Here's how to convert a normalized object back to an instance of its class:
The process of denormalization works using reflection inspecting an object's structure. In order to know what into what type to denormalize a value to, the normalizer checks for
@var
annotations on the object for PHP < 7.4, or the declared type for PHP >= 7.4. Therefore, it is important to correctly declare the @var annotations if using an older version of PHP.
Custom normalizers/denormalizer.
Depending on the structure of your objects you might want to personalize the way they are (de)normalized. A common example is with value objects wrapping primitives:
Such object out of the box would be normalized as follows:
For such object, and especially when they are part of an object graph, you might want to normalize them to the primitive they wrap for example.
To perform this you need to specify a custom normalizer/denormalizer pair and add it to your normalizer:
The FluentNormalizer
and FluentDenormalizer
are the most convenient way to define (De)Normalizers.
If you want to have full control over them you can implement the NormalizerInterface
and DenormalizerInterface
.
All versions of orkestra-normalization with dependencies
doctrine/instantiator Version 1.*
php-di/phpdoc-reader Version 2.*
ext-json Version *
morebec/orkestra-datetime Version ^2.5.6