Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: bernardphp/normalt
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.1.0
Choose a base ref
...
head repository: bernardphp/normalt
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref

Commits on Mar 3, 2014

  1. Redo readme

    henrikbjorn committed Mar 3, 2014
    Copy the full SHA
    50cb2f2 View commit details
  2. Update README.md

    henrikbjorn committed Mar 3, 2014
    Copy the full SHA
    0449a7b View commit details

Commits on Mar 7, 2014

  1. Copy the full SHA
    1e28e5f View commit details
  2. Copy the full SHA
    223aa48 View commit details
  3. Copy the full SHA
    d2712f1 View commit details
  4. Update README.md

    henrikbjorn committed Mar 7, 2014
    Copy the full SHA
    b9b51d3 View commit details

Commits on Mar 25, 2014

  1. change DoctrinNormalizer hasMetadataFor()

    Richard Shank committed Mar 25, 2014
    Copy the full SHA
    4f68390 View commit details
  2. Merge pull request #3 from iampersistent/doctrine-normalizer

    change DoctrinNormalizer hasMetadataFor()
    henrikbjorn committed Mar 25, 2014
    Copy the full SHA
    b844162 View commit details

Commits on Aug 25, 2014

  1. Fixup composer file

    henrikbjorn committed Aug 25, 2014
    Copy the full SHA
    92a3ee4 View commit details

Commits on Mar 23, 2016

  1. Allow symfony/serializer 3.0

    ruudk committed Mar 23, 2016
    Copy the full SHA
    bf278a8 View commit details

Commits on Apr 11, 2016

  1. Merge pull request #4 from ruudk/patch-1

    Allow symfony/serializer 3.0
    henrikbjorn committed Apr 11, 2016
    Copy the full SHA
    4d7352e View commit details

Commits on May 2, 2016

  1. Copy the full SHA
    62fc35e View commit details

Commits on May 3, 2016

  1. Merge pull request #5 from sagikazarmark/serializer_aware

    Add serializer aware capabilities
    henrikbjorn committed May 3, 2016
    Copy the full SHA
    bbf3e49 View commit details
  2. Improve package

    Install phpspec as a dev dependency
    
    Improve composer config
    
    Improve travis config
    
    Add editorconfig
    
    Add git attributes file
    
    Improve gitignore
    
    Add phpspec config, add coverage generation
    
    Update LICENSE
    
    Improve README
    
    Add change log
    
    Use an older version of code coverage to support older PHP
    
    Tweak travis config
    
    Change phpspec config name
    
    Update changelog
    
    Update LICENSE
    
    Remove coverage
    sagikazarmark committed May 3, 2016
    Copy the full SHA
    fede2d3 View commit details
  3. Merge pull request #6 from sagikazarmark/improvements

    Improvements
    henrikbjorn committed May 3, 2016
    Copy the full SHA
    1cde16b View commit details

Commits on Mar 3, 2017

  1. Allow denormalize for null

    `null` is not a scalar, so on denormalize I get :
    
    ```
    Warning: ReflectionObject::__construct() expects parameter 1 to be object, null given in vendor/bernard/normalt/src/Normalizer/RecursiveReflectionNormalizer.php line 31
    ```
    stephanedelprat authored Mar 3, 2017
    Copy the full SHA
    4965c54 View commit details
  2. Merge pull request #7 from stephanedelprat/patch-1

    Allow denormalize for null
    henrikbjorn authored Mar 3, 2017
    Copy the full SHA
    29a9c87 View commit details

Commits on Dec 15, 2017

  1. Allow symfony 4

    acrobat committed Dec 15, 2017
    Copy the full SHA
    cdec46f View commit details

Commits on Jan 13, 2018

  1. Merge pull request #9 from acrobat/sf4-support

    Allow symfony 4
    acrobat authored Jan 13, 2018
    Copy the full SHA
    9ed9b6b View commit details

Commits on Jul 8, 2021

  1. Allow PHP 8.0

    holtkamp authored Jul 8, 2021
    Copy the full SHA
    a60aab4 View commit details

Commits on Apr 21, 2022

  1. Merge pull request #10 from holtkamp/patch-1

    Allow PHP 8.0
    sagikazarmark authored Apr 21, 2022
    Copy the full SHA
    b8815ab View commit details
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
spec/ export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
phpspec.yml.dist export-ignore
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
vendor
build/
vendor/
composer.lock
phpspec.yml
32 changes: 25 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
language: php

sudo: false

cache:
directories:
- $HOME/.composer/cache

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- hhvm

before_script:
- "composer require --dev phpspec/phpspec:~2.0.0-RC4 --no-update"
- "composer install --no-progress --no-plugins"
env:
global:
- TEST_COMMAND="composer test"

matrix:
fast_finish: true
include:
- php: 5.6
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"

before_install:
- travis_retry composer self-update

install:
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction

script:
- php vendor/bin/phpspec run
- $TEST_COMMAND
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Change Log


## Unreleased

### Changed

- AggregateNormalizer now implements `Symfony\Component\Serializer\SerializerAwareInterface`


## 1.0.0 - 2014-03-25

- Initial release
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Bernard
Copyright (c) 2014-2016 Bernard Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
164 changes: 126 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -3,67 +3,155 @@ Normalt

[![Build Status](https://travis-ci.org/bernardphp/normalt.png?branch=master)](https://travis-ci.org/bernardphp/normalt)

Normalt is a extension to Symfony Serializer than implements only the Normalization part. It comes with several
different Normalizers that can be used to normalize from object to array and denormalize from array to object.
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.

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.


Table of Contents
-----------------

* [Getting Started](#getting-started)
* [Normalizers](#normalizers)
* [AggregateNormalizer](#aggregatenormalizer)
* [DoctrineNormalizer](#doctrinenormalizer)
* [RecursiveReflectionNormalizer](#recursivereflectionnormalizer)
* [License](#license)

The main interaction is the Marshaller. This is a implementation of `DenormalizerInterface` and `NormalizerInterface`.

Getting Started
---------------

Each normalizer can be used on its own, but you can also use a `NormalizerSet` to use many different dependent on
the type you are normalizing, just like when using the serializer.
Getting started is as easy as requiring the library with composer.

``` bash
$ composer require bernard/normalt
```


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
use Normalt\Marshaller;
class User
{
protected $name;

public function setName($name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}
```

Lets normalize and denormalize it again.

``` php
use Normalt\Normalizer\AggregateNormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;

$set = new Marshaller(array(
new GetSetMethodNormalizer,
new CustomNormalizer,
));
$aggregate = new AggregateNormalizer([new GetSetMethodNormalizer]);

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

$array = $aggregate->normalize($user);

$normalized = $set->normalize(new MyObject);
$object = $set->denormalize($normalized);
// $array now contains ['name' => 'henrik']

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

Any normalizer that is used through the `Marshaller` will have an instance of it set
if they implement `Normalt\MarshallerAware`. Same as if you have a Normalizer that implements
`SerializerAwareInterface` and use the Serializer.
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.

RecursiveReflectionNormalizer
-----------------------------

It is a special normalizer that uses a list of normalizers, but instead of applying on an per object basis it
traverses the properties and applies normalization to each. If a property contains an array this is also traversed.
### DoctrineNormalizer

This can be used together with `DoctrineNormalizer` to automatically convert from Entity to array and back again.
`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.

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

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

``` php
use Normalt\Normalizer\RecursiveReflectionNormalizer;
use Normalt\Normalizer\DoctrineNormalizer;

$normalizer = new RecursiveReflectionNormalizer(array(
new DoctrineNormalizer($objectManager),
));
$normalizer = new RecursiveReflectionNormalizer([new DoctrineNormalizer($entityManager)]);

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

// Assuming this wraps an entity called MyModel.
// we would get the following array when normalized (assuming its identifier is 1
// array(
// 'model' => array('className' => 'MyModel', 1),
// )
class MyModelWrapper {
protected $model;
// converting it back into the object.
// $profile->user is now an instance of MyModel\User
$profile = $normalize->denormalize($array, 'MyModel\Profile');
```

public function __construct()
{
$this->model = new MyModel;
}
}

$normalizer->normalize(new MyModelWrapper);
```
License
-------

Please refer to the included `LICENSE` file.
34 changes: 23 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
{
"name" : "bernard/normalt",
"license" : "MIT",

"autoload" : {
"psr-4" : { "Normalt\\" : "src" }
"name": "bernard/normalt",
"description": "Normalt is a extension to Symfony Serializer that only implements the Normalization part",
"keywords": ["normalization", "denormalization"],
"license": "MIT",
"require": {
"php": "^5.6 || ^7.0 || ^8.0",
"symfony/serializer": "^2.3 || ^3.0 || ^4.0"
},

"require" : {
"symfony/serializer" : ">=2.3,<=3.0"
"require-dev": {
"doctrine/common": "^2.1",
"phpspec/phpspec": "^2.5"
},

"require-dev" : {
"doctrine/common" : ">=2.1,<3.0"
"autoload": {
"psr-4": { "Normalt\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Fixtures\\": "spec/Fixtures/" }
},
"scripts": {
"test": "vendor/bin/phpspec run"
},
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
}
}
}
4 changes: 4 additions & 0 deletions phpspec.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
suites:
normalt_suite:
namespace: Normalt
formatter.name: pretty
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace spec\Normalt;
namespace spec\Normalt\Normalizer;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class MarshallerSpec extends ObjectBehavior
class AggregateNormalizerSpec extends ObjectBehavior
{
/**
* @param Symfony\Component\Serializer\Normalizer\NormalizerInterface $unsupported
@@ -67,7 +67,7 @@ function it_denormalizes_with_supported_normalizer($unsupported, $supported)
}

/**
* @param Normalt\MarshallerAware $normalizer
* @param Normalt\Normalizer\AggregateNormalizerAware $normalizer
*/
function it_sets_itself_as_marshaller_if_marshaller_aware($normalizer)
{
@@ -79,12 +79,36 @@ function it_sets_itself_as_marshaller_if_marshaller_aware($normalizer)
$normalizer->supportsNormalization('data', null)->willReturn(true);
$normalizer->supportsDenormalization('data', 'string', null)->willReturn(true);

$normalizer->setMarshaller($this)->shouldBeCalledTimes(2);
$normalizer->setAggregateNormalizer($this)->shouldBeCalledTimes(2);

$normalizer->normalize('data', null, array())->willReturn();
$normalizer->denormalize('data', 'string', null, array())->willReturn();

$this->normalize('data');
$this->denormalize('data', 'string');
}

function it_is_serializer_aware()
{
$this->shouldImplement('Symfony\Component\Serializer\SerializerAwareInterface');
}

/**
* @param Symfony\Component\Serializer\Normalizer\NormalizerInterface $normalizer
* @param Symfony\Component\Serializer\Normalizer\NormalizerInterface $denormaNormalizer
* @param Symfony\Component\Serializer\SerializerInterface $serializer
*/
function it_sets_the_serializer_if_one_of_the_normalizers_is_serializer_aware($normalizer, $denormaNormalizer, $serializer)
{
$normalizer->implement('Symfony\Component\Serializer\SerializerAwareInterface');
$denormaNormalizer->implement('Symfony\Component\Serializer\SerializerAwareInterface');
$denormaNormalizer->implement('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');

$normalizer->setSerializer($serializer)->shouldBeCalled();
$denormaNormalizer->setSerializer($serializer)->shouldBeCalledTimes(2);

$this->beConstructedWith(array($normalizer, $denormaNormalizer));

$this->setSerializer($serializer);
}
}
Loading