1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the FiveLab Resource package |
7
|
|
|
* |
8
|
|
|
* (c) FiveLab |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace FiveLab\Component\Resource\Serializers\Hateoas\Normalizer; |
15
|
|
|
|
16
|
|
|
use FiveLab\Component\Resource\Resource\ResourceCollection; |
17
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
18
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
19
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The normalizer for normalize resource collection. |
23
|
|
|
* |
24
|
|
|
* @author Vitaliy Zhuk <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ResourceCollectionObjectNormalizer implements NormalizerInterface, NormalizerAwareInterface |
27
|
|
|
{ |
28
|
|
|
use NormalizerAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
* |
33
|
|
|
* @param ResourceCollection $object |
34
|
|
|
* |
35
|
|
|
* @throws \InvalidArgumentException |
36
|
|
|
*/ |
37
|
3 |
|
public function normalize($object, $format = null, array $context = []) |
38
|
|
|
{ |
39
|
3 |
View Code Duplication |
if (!$object instanceof ResourceCollection) { |
|
|
|
|
40
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
41
|
1 |
|
'The normalizer support only "%s" but "%s" given.', |
42
|
1 |
|
ResourceCollection::class, |
43
|
1 |
|
is_object($object) ? get_class($object) : gettype($object) |
44
|
|
|
)); |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
$data = []; |
48
|
2 |
|
$links = []; |
49
|
|
|
|
50
|
2 |
|
if (count($object->getRelations())) { |
51
|
1 |
|
$links = array_merge($links, $this->normalizer->normalize($object->getRelations(), $format, $context)); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
if (count($object->getActions())) { |
55
|
1 |
|
$links = array_merge($links, $this->normalizer->normalize($object->getActions(), $format, $context)); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
foreach ($object as $resource) { |
59
|
2 |
|
$data[] = $this->normalizer->normalize($resource, $format, $context); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
if (count($links)) { |
63
|
|
|
return [ |
64
|
1 |
|
'_links' => $links, |
65
|
|
|
'_embedded' => [ |
66
|
1 |
|
'items' => $data, |
67
|
|
|
], |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $data; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
2 |
|
public function supportsNormalization($data, $format = null): bool |
78
|
|
|
{ |
79
|
2 |
|
return is_object($data) && $data instanceof ResourceCollection; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.