Download the PHP package alexanevsky/output-normalizer-bundle without Composer
On this page you can find all versions of the php package alexanevsky/output-normalizer-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alexanevsky/output-normalizer-bundle
More information about alexanevsky/output-normalizer-bundle
Files in alexanevsky/output-normalizer-bundle
Package output-normalizer-bundle
Short Description Provides functions which allows to flexibly customize the output of objects and entities to an array or JSON by defining normalization rules in classes.
License MIT
Informations about the package output-normalizer-bundle
Output Normalizer
This library allows you to flexibly customize the output of your objects and entities to an array or JSON by defining normalization rules in classes that implements OutputInterface.
Table of Contents
- Basic Example
- How the Output Normalizer Works
- Using Getters
- Using Setters
- Object Normalization
- Global Description of Object Normalization
- Entity Identifier Output
- Using Output Modifiers
Basic Example
Let's imagine that we have a model or Doctrine entity like this:
We want to give only two properties to the output: id and userName. Let's describe our class:
Add the OutputNormalizer to the constructor of controller or service:
And then we call the normalize method, passing in User entity and the UserOutput class name:
As a result, in $output we will get this:
Note: Please note that property names are translated in snake case.
This is the basic functionality of the normalizer. We will explore its capabilities in more detail below.
How the Output Normalizer Works
- The normalizer takes the public properties and getters of your model (entity) and maps them to the public properties and setters of the output object.
- The normalizer takes the public properties and getters of the output object and converts them into an array, changing the keys to snake case.
- The normalizer invokes the call modifier.
First, the normalizer goes through the public properties, then through the getters (or setters, depending on the action).
Note: If a public property has a getter (or setter), it will take precedence, i.e. the value of the getter will be taken (passed to the setter) rather than taken from the property (rather than assigned to the property). You can see more about how getters and setters are used in the library alexanevsky/getter-setter-accessor.
Using Getters
Let's imagine this model:
To just output the phone, we just need to add this property to our Output class:
And our result will be like this:
However, if we add a phone getter to our output that will perform some modification, it will be called during normalization:
The result will be:
Using Setters
Instead of using public properties in our output, we can use setters and getters as we normally do elsewhere in our project. Everything will work as it should:
Object Normalization
Imagine that for the phone we use not a string, but a class:
And in the user model:
In the output, we must also use this model:
The object will be normalized by its public properties and getters, as Symphony Normalizer usually does:
We can also use PhoneOutput instead of Phone, which also implements OutputInterface, in which we define only the properties we need:
As a result, we will get only the properties we need:
However, remember that we can use a getter, as in the example above, to output a modified result:
The result will be:
Global Description of Object Normalization
We can globally describe how we want to normalize some objects. To do this, we need to create a class that inherits from ObjectNormalizerInterface and put it anywhere in our project:
Now we can just specify Phone in our output and it will be normalized according to the rule above:
We will get this result:
Entity Identifier Output
Imagine that our entity has a property with another entity:
In order not to normalize all properties of the City, but only its identifier, we must add the EntityToId attribute:
And in the result we get this:
If we have a many-to-one relationship in our model, we can display all identifiers in the same way:
The output will have s appended to the key, since we are outputting a set:
If the entity identifier is different from id, it must be passed as the first parameter of EntityToId:
Our result will be:
If we want to override the suffix added to the output array key, we must pass it as the second parameter to EntityToId:
Our result will be:
If we don't want to add a suffix at all, we must pass false as the second parameter to EntityToId:
Our result will be:
Using Output Modifiers
If we want to somehow change the data we normalize into an array, we must use OutputModifierInterface. It modifies our OutputInterface data.
Let's imagine the following entity:
Let's say we want to remove the ROLE_USER role from the output and print all roles except for it. We can use OutputModifierInterface for this. Just put it anywhere in your project:
The normalizer itself invokes modifiers and uses those that return true in the supports method for the model and output class passed to the normalizer.
Our output will be like this:
Good luck!
All versions of output-normalizer-bundle with dependencies
alexanevsky/getter-setter-accessor-bundle Version ^1.0
doctrine/orm Version ^2.6
symfony/config Version ^5.4|^6.0
symfony/dependency-injection Version ^5.4|^6.0
symfony/http-kernel Version ^5.4|^6.0
symfony/string Version ^5.4|^6.0