|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CCT\Component\Rest\Transformer\Response; |
|
4
|
|
|
|
|
5
|
|
|
use CCT\Component\Rest\Http\ResponseInterface; |
|
6
|
|
|
use CCT\Component\Rest\Serializer\ContextInterface; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
8
|
|
|
|
|
9
|
|
|
class ObjectCollectionTransformer extends AbstractSerializerResponseTransformer |
|
10
|
|
|
{ |
|
11
|
|
|
protected $mappingKeys = null; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @param ResponseInterface|Response $response |
|
15
|
|
|
* |
|
16
|
|
|
* {@inheritdoc} |
|
17
|
|
|
*/ |
|
18
|
1 |
|
public function transform(ResponseInterface $response, ContextInterface $context = null) |
|
19
|
|
|
{ |
|
20
|
1 |
|
$data = $this->map($response->getData()); |
|
21
|
1 |
|
foreach ($data as $k => $object) { |
|
22
|
1 |
|
$data[$k] = $this->serializer->deserialize( |
|
23
|
1 |
|
json_encode($object), |
|
24
|
1 |
|
$this->class, |
|
25
|
1 |
|
'json', |
|
26
|
1 |
|
$context ?? $this->context |
|
27
|
|
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
$response->setData($data); |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param ResponseInterface|Response $response |
|
35
|
|
|
* |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
2 |
|
public function supports(ResponseInterface $response): bool |
|
39
|
|
|
{ |
|
40
|
2 |
|
$data = $response->getData(); |
|
41
|
|
|
|
|
42
|
|
|
return ( |
|
43
|
2 |
|
$response->isSuccessful() |
|
44
|
2 |
|
&& $this->isArrayAndNotEmpty($data) |
|
45
|
2 |
|
&& $this->mappingKeysExist($data) |
|
46
|
2 |
|
&& $this->isSequential($this->map($data)) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Check if $data is an array and not empty |
|
52
|
|
|
* |
|
53
|
|
|
* @param mixed $data |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
2 |
|
protected function isArrayAndNotEmpty($data) |
|
58
|
|
|
{ |
|
59
|
2 |
|
return is_array($data) && !empty($data); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Extract collection from response array based on mapping key. |
|
64
|
|
|
* Supports single level only |
|
65
|
|
|
* If no mapping key returns full data |
|
66
|
|
|
* |
|
67
|
|
|
* @param $data |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
1 |
|
protected function map(array $data): array |
|
72
|
|
|
{ |
|
73
|
1 |
|
if (null === $this->mappingKeys) { |
|
74
|
1 |
|
return $data; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
foreach ($this->mappingKeys as $key) { |
|
78
|
|
|
if (key_exists($key, $data)) { |
|
79
|
|
|
return $data; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
return []; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function mappingKeysExist(array $data): bool |
|
86
|
|
|
{ |
|
87
|
|
|
if (null === $this->mappingKeys) { |
|
88
|
|
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return (bool)count(array_intersect($this->mappingKeys, array_flip($data))) > 0; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return array|null |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getMappingKeys(): ?array |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->mappingKeys; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param null|array $mappingKeys |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setMappingKeys(array $mappingKeys = null) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->mappingKeys = $mappingKeys; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function isSequential($data) |
|
111
|
|
|
{ |
|
112
|
|
|
return array_keys($data) === range(0, count($data) - 1); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|