|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of AppBundle the package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Ruslan Muriev <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace RonteLtd\JsonApiBundle\Serializer\Normalizer; |
|
12
|
|
|
|
|
13
|
|
|
use RonteLtd\JsonApiBundle\Serializer\Exception\LogicException; |
|
14
|
|
|
use RonteLtd\JsonApiBundle\Serializer\Mapping\Factory\MetadataFactoryInterface; |
|
15
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
|
16
|
|
|
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
|
17
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
|
18
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class CollectionNormalizer |
|
22
|
|
|
* |
|
23
|
|
|
* @package RonteLtd\JsonApiBundle\Serializer\Normalizer |
|
24
|
|
|
* @author Ruslan Muriev <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class CollectionNormalizer extends AbstractNormalizer |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* ObjectNormalizer constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param MetadataFactoryInterface $metadataFactory |
|
32
|
|
|
* @param ClassMetadataFactoryInterface|null $classMetadataFactory |
|
33
|
|
|
* @param NameConverterInterface|null $nameConverter |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(MetadataFactoryInterface $metadataFactory, ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null) |
|
36
|
|
|
{ |
|
37
|
|
|
parent::__construct($classMetadataFactory, $nameConverter); |
|
38
|
|
|
|
|
39
|
|
|
$this->metadataFactory = $metadataFactory; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function supportsNormalization($data, $format = null) |
|
46
|
|
|
{ |
|
47
|
|
|
return ($data instanceof Collection); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function normalize($collection, $format = null, array $context = array()) |
|
54
|
|
|
{ |
|
55
|
|
|
if (!$collection instanceof Collection) { |
|
56
|
|
|
return $collection; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (!empty($collection->getJsonapi())) { |
|
60
|
|
|
$jsonApiData['jsonapi'] = $collection->getJsonapi(); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (!empty($collection->getLinks())) { |
|
64
|
|
|
$jsonApiData['links'] = $collection->getLinks(); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (!empty($collection->getMeta())) { |
|
68
|
|
|
$jsonApiData['meta'] = $collection->getMeta(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// $jsonApiData['included'] = []; TODO implement |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
foreach ($collection as $item) { |
|
74
|
|
|
if ($this->serializer instanceof NormalizerInterface) { |
|
|
|
|
|
|
75
|
|
|
$jsonApiData['data'][] = $this->serializer->normalize($item); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $jsonApiData; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
|
86
|
|
|
{ |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function denormalize($data, $class, $format = null, array $context = array()) |
|
94
|
|
|
{ |
|
95
|
|
|
throw new LogicException(sprintf('Cannot denormalize with "%s".', \JsonSerializable::class)); |
|
96
|
|
|
} |
|
97
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.