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.

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 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

  1. Basic Example
  2. How the Output Normalizer Works
  3. Using Getters
  4. Using Setters
  5. Object Normalization
  6. Global Description of Object Normalization
  7. Entity Identifier Output
  8. 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

  1. 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.
  2. The normalizer takes the public properties and getters of the output object and converts them into an array, changing the keys to snake case.
  3. 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

PHP Build Version
Package Version
Requires php Version >=8.1
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
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 alexanevsky/output-normalizer-bundle contains the following files

Loading the files please wait ...