-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAggregateNormalizer.php
More file actions
112 lines (91 loc) · 3.47 KB
/
AggregateNormalizer.php
File metadata and controls
112 lines (91 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace Normalt\Normalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerInterface;
use UnexpectedValueException;
/**
* Functionality extracted from Symfony\Component\Serializer\Serializer in order to
* limit it to denormalization and normalization while still giving the user lot of
* flexibility when needed.
*
* @package Normalt
*/
class AggregateNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
{
protected $normalizers = array();
protected $denormalizers = array();
public function __construct($normalizers = array())
{
array_map(array($this, 'add'), $normalizers);
}
public function normalize($object, $format = null, array $context = array())
{
if ($normalizer = $this->getNormalizer($object, $format)) {
return $normalizer->normalize($object, $format, $context);
}
throw new UnexpectedValueException('No supported normalizer found for "' . get_class($object) . '".');
}
public function denormalize($data, $class, $format = null, array $context = array())
{
if ($denormalizer = $this->getDenormalizer($data, $class, $format)) {
return $denormalizer->denormalize($data, $class, $format, $context);
}
throw new UnexpectedValueException('No supported denormalizer found for "' . $class . '".');
}
public function supportsNormalization($data, $format = null)
{
return (boolean) $this->getNormalizer($data, $format);
}
public function supportsDenormalization($data, $type, $format = null)
{
return (boolean) $this->getDenormalizer($data, $type, $format);
}
public function setSerializer(SerializerInterface $serializer)
{
foreach ($this->normalizers as $normalizer) {
if ($normalizer instanceof SerializerAwareInterface) {
$normalizer->setSerializer($serializer);
}
}
foreach ($this->denormalizers as $normalizer) {
if ($normalizer instanceof SerializerAwareInterface) {
$normalizer->setSerializer($serializer);
}
}
}
protected function getNormalizer($data, $format = null)
{
foreach ($this->normalizers as $normalizer) {
if (false == $normalizer->supportsNormalization($data, $format)) {
continue;
}
if ($normalizer instanceof AggregateNormalizerAware) {
$normalizer->setAggregateNormalizer($this);
}
return $normalizer;
}
}
protected function getDenormalizer($data, $type, $format = null)
{
foreach ($this->denormalizers as $denormalizer) {
if (false == $denormalizer->supportsDenormalization($data, $type, $format)) {
continue;
}
if ($denormalizer instanceof AggregateNormalizerAware) {
$denormalizer->setAggregateNormalizer($this);
}
return $denormalizer;
}
}
protected function add($normalizer)
{
if ($normalizer instanceof NormalizerInterface) {
$this->normalizers[] = $normalizer;
}
if ($normalizer instanceof DenormalizerInterface) {
$this->denormalizers[] = $normalizer;
}
}
}