|
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\WebApi; |
|
15
|
|
|
|
|
16
|
|
|
use FiveLab\Component\Resource\Resource\ResourceInterface; |
|
17
|
|
|
use FiveLab\Component\Resource\Serializer\Context\ResourceSerializationContext; |
|
18
|
|
|
use FiveLab\Component\Resource\Serializer\ResourceSerializerInterface; |
|
19
|
|
|
use FiveLab\Component\Resource\Serializer\SerializerInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Default Web API serializer. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Vitaliy Zhuk <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class WebApiSerializer implements ResourceSerializerInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var SerializerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $serializer; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $format; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param SerializerInterface $serializer |
|
42
|
|
|
* @param string $format |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function __construct(SerializerInterface $serializer, string $format) |
|
45
|
|
|
{ |
|
46
|
2 |
|
$this->serializer = $serializer; |
|
47
|
2 |
|
$this->format = $format; |
|
48
|
2 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
1 |
View Code Duplication |
public function serialize(ResourceInterface $resource, ResourceSerializationContext $context): string |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$innerContext = [ |
|
56
|
|
|
'after_normalization' => function (array $data) { |
|
57
|
1 |
|
return $this->removeRelations($data); |
|
58
|
1 |
|
}, |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
1 |
|
return $this->serializer->serialize($resource, $this->format, $innerContext); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function deserialize(string $data, string $resourceClass, ResourceSerializationContext $context): ResourceInterface |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->serializer->deserialize($data, $resourceClass, $this->format); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Remove resources from data |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $data |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
1 |
|
protected function removeRelations(array $data): array |
|
80
|
|
|
{ |
|
81
|
1 |
|
unset($data['relations'], $data['actions']); |
|
82
|
|
|
|
|
83
|
1 |
|
return $data; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
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.