1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the vseth-semesterly-reports project. |
5
|
|
|
* |
6
|
|
|
* (c) Florian Moser <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace App\Normalizer; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use Symfony\Component\Serializer\Exception\CircularReferenceException; |
16
|
|
|
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
17
|
|
|
use Symfony\Component\Serializer\Exception\LogicException; |
18
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
19
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
20
|
|
|
|
21
|
|
|
class ArrayCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Normalizes an object into a set of arrays/scalars. |
25
|
|
|
* |
26
|
|
|
* @param array $object Object to normalize |
27
|
|
|
* @param string $format Format the normalization result will be encoded as |
28
|
|
|
* @param array $context Context options for the normalizer |
29
|
|
|
* |
30
|
|
|
* @throws InvalidArgumentException Occurs when the object given is not an attempted type for the normalizer |
31
|
|
|
* @throws CircularReferenceException Occurs when the normalizer detects a circular reference when no circular |
32
|
|
|
* reference handler can fix it |
33
|
|
|
* @throws LogicException Occurs when the normalizer is not called in an expected context |
34
|
|
|
* |
35
|
|
|
* @return array|string|int|float|bool |
36
|
|
|
*/ |
37
|
|
|
public function normalize($object, $format = null, array $context = []) |
38
|
|
|
{ |
39
|
|
|
$normalized = []; |
40
|
|
|
|
41
|
|
|
foreach ($object as $key => $val) { |
42
|
|
|
$normalized[] = $this->normalizer->normalize($val, $format, $context); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $normalized; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Checks whether the given class is supported for normalization by this normalizer. |
50
|
|
|
* |
51
|
|
|
* @param mixed $data Data to normalize |
52
|
|
|
* @param string $format The format being (de-)serialized from or into |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function supportsNormalization($data, $format = null) |
57
|
|
|
{ |
58
|
|
|
return $data instanceof ArrayCollection; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var NormalizerInterface |
63
|
|
|
*/ |
64
|
|
|
private $normalizer; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets the owning Normalizer object. |
68
|
|
|
*/ |
69
|
|
|
public function setNormalizer(NormalizerInterface $normalizer) |
70
|
|
|
{ |
71
|
|
|
$this->normalizer = $normalizer; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|