|
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; |
|
15
|
|
|
|
|
16
|
|
|
use FiveLab\Component\Resource\Resource\ResourceInterface; |
|
17
|
|
|
use FiveLab\Component\Resource\Serializer\Context\ResourceSerializationContext; |
|
18
|
|
|
use FiveLab\Component\Resource\Serializer\Exception\DeserializationNotSupportException; |
|
19
|
|
|
use FiveLab\Component\Resource\Serializer\ResourceSerializerInterface; |
|
20
|
|
|
use FiveLab\Component\Resource\Serializer\SerializerInterface; |
|
21
|
|
|
use FiveLab\Component\Resource\Serializers\Hateoas\Normalizer\RelationCollectionObjectNormalizer; |
|
22
|
|
|
use FiveLab\Component\Resource\Serializers\Hateoas\Normalizer\RelationObjectNormalizer; |
|
23
|
|
|
use FiveLab\Component\Resource\Serializers\VndError\Normalizer\ErrorCollectionObjectNormalizer; |
|
24
|
|
|
use FiveLab\Component\Resource\Serializers\VndError\Normalizer\ErrorResourceObjectNormalizer; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The serializer for serialize errors to Vnd.Error format. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Vitaliy Zhuk <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
class VndErrorSerializer implements ResourceSerializerInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var SerializerInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $serializer; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $format; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
private $normalizers; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Constructor. |
|
50
|
|
|
* |
|
51
|
|
|
* @param SerializerInterface $serializer |
|
52
|
|
|
* @param array $normalizers |
|
53
|
|
|
* @param string $format |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function __construct(SerializerInterface $serializer, array $normalizers, string $format) |
|
56
|
|
|
{ |
|
57
|
2 |
|
$this->serializer = $serializer; |
|
58
|
2 |
|
$this->format = $format; |
|
59
|
2 |
|
$this->normalizers = $normalizers; |
|
60
|
2 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
1 |
View Code Duplication |
public function serialize(ResourceInterface $resource, ResourceSerializationContext $context): string |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
$innerContext = [ |
|
68
|
1 |
|
'normalizers' => $this->normalizers, |
|
69
|
|
|
]; |
|
70
|
|
|
|
|
71
|
1 |
|
return $this->serializer->serialize($resource, $this->format, $innerContext); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
* |
|
77
|
|
|
* @throws DeserializationNotSupportException |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function deserialize(string $data, string $resourceClass, ResourceSerializationContext $context): ResourceInterface |
|
80
|
|
|
{ |
|
81
|
1 |
|
throw new DeserializationNotSupportException('The Vnd.Error not support deserialization.'); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
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.