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\ErrorResourceInterface; |
17
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
18
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
19
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Normalizer for normalize error resource for Vnd.Error format. |
23
|
|
|
* |
24
|
|
|
* @author Vitaliy Zhuk <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ErrorResourceObjectNormalizer implements NormalizerInterface, NormalizerAwareInterface |
27
|
|
|
{ |
28
|
|
|
use NormalizerAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
2 |
|
public function supportsNormalization($data, $format = null): bool |
34
|
|
|
{ |
35
|
2 |
|
return \is_object($data) && $data instanceof ErrorResourceInterface; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
* |
41
|
|
|
* @param ErrorResourceInterface $object |
42
|
|
|
*/ |
43
|
1 |
|
public function normalize($object, $format = null, array $context = []): array |
44
|
|
|
{ |
45
|
1 |
|
$links = []; |
46
|
|
|
|
47
|
1 |
View Code Duplication |
if (\count($object->getRelations())) { |
|
|
|
|
48
|
1 |
|
$links = \array_merge($links, $this->normalizer->normalize($object->getRelations(), $format, $context)); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
View Code Duplication |
if (\count($object->getActions())) { |
|
|
|
|
52
|
1 |
|
$links = \array_merge($links, $this->normalizer->normalize($object->getActions(), $format, $context)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$data = [ |
56
|
1 |
|
'message' => $object->getMessage(), |
57
|
|
|
]; |
58
|
|
|
|
59
|
1 |
|
if ($object->getPath()) { |
60
|
1 |
|
$data['path'] = $object->getPath(); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
if ($object->getIdentifier()) { |
64
|
1 |
|
$data['logref'] = $object->getIdentifier(); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
if (\count($links)) { |
68
|
1 |
|
$data['_links'] = $links; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $data; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
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.