2017 © Pedro Peláez
 

library normalt

Normalt is a extension to Symfony Serializer that only implements the Normalization part

image

bernard/normalt

Normalt is a extension to Symfony Serializer that only implements the Normalization part

  • Saturday, January 13, 2018
  • by henrikbjorn
  • Repository
  • 4 Watchers
  • 14 Stars
  • 279,759 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 1 Open issues
  • 7 Versions
  • 12 % Grown

The README.md

Normalt

Build Status, (*1)

Normalt contains additional normalizers for use with the serializer component found in Symfony. It also implements a normalizer delegator that will look at the data you want normalized and/or denormalized and call the normalizer which supports it., (*2)

In the context of Normalt normalization is the act of converting an object into an array. Denormalization is the opposite direction (converting array into an object). This is to my knowledge the same concept Symfony serializer uses., (*3)

Table of Contents

Getting Started

Getting started is as easy as requiring the library with composer., (*4)

``` bash $ composer require bernard/normalt, (*5)



Normalizers ----------- Theese normalizers can be used with the serializer component directly or through the `AggregateNormalizer`. ### AggregateNormalizer `AggregateNormalizer` is a delegator and aggregator as it aggregates multiple normalizers and denormalizers which it will delegate the process to. It have a list of normalizers and denormalizers. It will ask each of theese if they support the data/object and use the first found. It implements a subset of the full serializer and its only focus is normalizing to arrays and denormalize arrays into objects. This lets you focus on normalization instead of converting into a specific format such as `xml`, `json` etc. #### Usage You need to instantiate the normalizer and the list of normalizer/denormalizers you want to use. For this example we use `GetSetMethodNormalizer` which is distributed with the symfony package. This is the class we are going to use. `GetSetMethodNormalizer` uses getters and setters to do its job. ``` php class User { protected $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } }

Lets normalize and denormalize it again., (*6)

``` php use Normalt\Normalizer\AggregateNormalizer; use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;, (*7)

$aggregate = new AggregateNormalizer([new GetSetMethodNormalizer]);, (*8)

$user = new User; $user->setName('henrik');, (*9)

$array = $aggregate->normalize($user);, (*10)

// $array now contains ['name' => 'henrik'], (*11)

$user = $aggregate->denormalize($array, 'User'); echo $user->getName(); // outputs Henrik, (*12)


In contrast to the other normalizers in this package, it does __not__ make sense to use this with the serializer as the Serializer already does most of the functionality already. ### DoctrineNormalizer `DoctrineNormalizer` normalizes mapped objects (Entities, Documents etc.) into arrays and back again. It usage is very simple. The following example assume a mapped object of `$user` and that you are using the doctrine orm (other doctrine projects work aswell!). ``` php use Doctrine\ORM\EntityManager; use Normalt\Normalizer\DoctrineNormalizer; // create $entityManager $normalizer = new DoctrineNormalizer($entityManager); // assuming $user is a mapped object and have the identifier value of 10. the following will return // array('MyModel\User', 10) $array = $normalizer->normalize($user); // using the same structure you can convert it back into a user $user = $normalizer->denormalize($array, null);

RecursiveReflectionNormalizer

This normalizes also delegates like the AggregateNormalizer, but delegates for each property in the object you are normalizing to normalize. It does this with recursion, so if a normalizer does not support a given property and is an array it will loop through that array and look for more objects., (*13)

The same thing happens when denormalizing, except it will try and find a supporting denormalizer for the property structure before looping., (*14)

Using is simple as the other, the example utilises DoctrineNormalizer and assumes we have a $profile object that contains a reference to a user with $profile->user., (*15)

``` php use Normalt\Normalizer\RecursiveReflectionNormalizer; use Normalt\Normalizer\DoctrineNormalizer;, (*16)

$normalizer = new RecursiveReflectionNormalizer([new DoctrineNormalizer($entityManager)]);, (*17)

// following will return assuming User is mapped and has the identifier of 10 //['user' => ['MyModel\User', 10]] $array = $normalizer->normalize($profile);, (*18)

// converting it back into the object. // $profile->user is now an instance of MyModel\User $profile = $normalize->denormalize($array, 'MyModel\Profile'); ```, (*19)

License

Please refer to the included LICENSE file., (*20)

The Versions

13/01 2018

dev-master

9999999-dev

Normalt is a extension to Symfony Serializer that only implements the Normalization part

  Sources   Download

MIT

The Requires

 

The Development Requires

normalization denormalization

13/01 2018

v1.2.0

1.2.0.0

Normalt is a extension to Symfony Serializer that only implements the Normalization part

  Sources   Download

MIT

The Requires

 

The Development Requires

normalization denormalization

03/05 2016

1.1.0

1.1.0.0

Normalt is a extension to Symfony Serializer that only implements the Normalization part

  Sources   Download

MIT

The Requires

 

The Development Requires

normalization denormalization

11/04 2016

1.0.1

1.0.1.0

  Sources   Download

MIT

The Requires

 

The Development Requires

25/03 2014

1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

07/03 2014

0.2.0

0.2.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

01/03 2014

0.1.0

0.1.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires