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\VndError\Normalizer; |
15
|
|
|
|
16
|
|
|
use FiveLab\Component\Resource\Resource\Error\ErrorCollection; |
17
|
|
|
use FiveLab\Component\Resource\Resource\Error\ErrorResource; |
18
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
19
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
20
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Normalizer for normalize error collection for Vnd.Error format. |
24
|
|
|
* |
25
|
|
|
* @author Vitaliy Zhuk <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ErrorCollectionObjectNormalizer implements NormalizerInterface, NormalizerAwareInterface |
28
|
|
|
{ |
29
|
|
|
use NormalizerAwareTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
2 |
|
public function supportsNormalization($data, $format = null): bool |
35
|
|
|
{ |
36
|
2 |
|
return \is_object($data) && $data instanceof ErrorCollection; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
* |
42
|
|
|
* @param ErrorCollection $object |
43
|
|
|
*/ |
44
|
2 |
|
public function normalize($object, $format = null, array $context = []): array |
45
|
|
|
{ |
46
|
2 |
|
$nested = $object->getMessage() || $object->getReason(); |
47
|
|
|
|
48
|
2 |
|
$errors = []; |
49
|
|
|
|
50
|
2 |
|
foreach ($object as $item) { |
51
|
2 |
|
$errors[] = $this->normalizer->normalize($item, $format, $context); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
$links = []; |
55
|
|
|
|
56
|
2 |
View Code Duplication |
if (count($object->getRelations())) { |
|
|
|
|
57
|
2 |
|
$links = \array_merge($links, $this->normalizer->normalize($object->getRelations(), $format, $context)); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
View Code Duplication |
if (count($object->getActions())) { |
|
|
|
|
61
|
1 |
|
$links = \array_merge($links, $this->normalizer->normalize($object->getActions(), $format, $context)); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
if ($nested) { |
65
|
1 |
|
$innerError = new ErrorResource( |
66
|
1 |
|
$object->getMessage(), |
67
|
1 |
|
$object->getReason(), |
68
|
1 |
|
$object->getPath(), |
69
|
1 |
|
$object->getAttributes(), |
70
|
1 |
|
$object->getIdentifier() |
71
|
|
|
); |
72
|
|
|
|
73
|
1 |
|
$data = $this->normalizer->normalize($innerError, $format, $context); |
74
|
|
|
|
75
|
1 |
|
if (\count($links)) { |
76
|
1 |
|
$data['_links'] = $links; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
$data['_embedded'] = [ |
80
|
1 |
|
'errors' => $errors, |
81
|
|
|
]; |
82
|
|
|
|
83
|
1 |
|
return $data; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$data = [ |
87
|
1 |
|
'total' => \count($errors), |
88
|
|
|
'_embedded' => [ |
89
|
1 |
|
'errors' => $errors, |
90
|
|
|
], |
91
|
|
|
]; |
92
|
|
|
|
93
|
1 |
|
if (\count($links)) { |
94
|
1 |
|
$data['_links'] = $links; |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
return $data; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.