1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Glue\GlueApplication\Rest\JsonApi; |
||
9 | |||
10 | use ArrayObject; |
||
11 | use Spryker\Shared\Kernel\Transfer\AbstractTransfer; |
||
12 | |||
13 | /** |
||
14 | * @deprecated Will be removed without replacement. |
||
15 | */ |
||
16 | class RestResource implements RestResourceInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var string|null |
||
20 | */ |
||
21 | protected $id; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $type; |
||
27 | |||
28 | /** |
||
29 | * @var array<\Spryker\Glue\GlueApplication\Rest\JsonApi\RestLinkInterface> |
||
30 | */ |
||
31 | protected $links = []; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $relationships = []; |
||
37 | |||
38 | /** |
||
39 | * @var \Spryker\Shared\Kernel\Transfer\AbstractTransfer|null |
||
40 | */ |
||
41 | protected $attributes; |
||
42 | |||
43 | /** |
||
44 | * @var \Spryker\Shared\Kernel\Transfer\AbstractTransfer|null |
||
45 | */ |
||
46 | protected $payload; |
||
47 | |||
48 | /** |
||
49 | * @param string $type |
||
50 | * @param string|null $id |
||
51 | * @param \Spryker\Shared\Kernel\Transfer\AbstractTransfer|null $attributes |
||
52 | */ |
||
53 | public function __construct(string $type, ?string $id = null, ?AbstractTransfer $attributes = null) |
||
54 | { |
||
55 | $this->type = $type; |
||
56 | $this->id = $id; |
||
57 | $this->attributes = $attributes; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface $restResource |
||
62 | * |
||
63 | * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface |
||
64 | */ |
||
65 | public function addRelationship(RestResourceInterface $restResource): RestResourceInterface |
||
66 | { |
||
67 | if ($restResource->getId()) { |
||
68 | $this->relationships[$restResource->getType()][$restResource->getId()] = $restResource; |
||
69 | |||
70 | return $this; |
||
71 | } |
||
72 | |||
73 | $this->relationships[$restResource->getType()][] = $restResource; |
||
74 | |||
75 | return $this; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param string $type |
||
80 | * |
||
81 | * @return array<\Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface> |
||
82 | */ |
||
83 | public function getRelationshipByType(string $type): array |
||
84 | { |
||
85 | return $this->relationships[$type]; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return array |
||
90 | */ |
||
91 | public function getRelationships(): array |
||
92 | { |
||
93 | return $this->relationships; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param string $name |
||
98 | * @param string $resourceUri |
||
99 | * @param array $meta |
||
100 | * |
||
101 | * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface |
||
102 | */ |
||
103 | public function addLink(string $name, string $resourceUri, array $meta = []): RestResourceInterface |
||
104 | { |
||
105 | $this->links[] = new RestLink($name, $resourceUri, $meta); |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
106 | |||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param string $name |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | public function hasLink(string $name): bool |
||
116 | { |
||
117 | foreach ($this->links as $link) { |
||
118 | if ($name === $link->getName()) { |
||
119 | return true; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | return false; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @return string|null |
||
128 | */ |
||
129 | public function getId(): ?string |
||
130 | { |
||
131 | return $this->id; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @return string |
||
136 | */ |
||
137 | public function getType(): string |
||
138 | { |
||
139 | return $this->type; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return \Spryker\Shared\Kernel\Transfer\AbstractTransfer|null |
||
144 | */ |
||
145 | public function getAttributes(): ?AbstractTransfer |
||
146 | { |
||
147 | return $this->attributes; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @return array |
||
152 | */ |
||
153 | public function getLinks(): array |
||
154 | { |
||
155 | return $this->links; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param bool $includeRelations |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | public function toArray($includeRelations = true): array |
||
164 | { |
||
165 | $response = [ |
||
166 | RestResourceInterface::RESOURCE_TYPE => $this->type, |
||
167 | RestResourceInterface::RESOURCE_ID => $this->id, |
||
168 | ]; |
||
169 | |||
170 | if ($this->attributes) { |
||
171 | $response[RestResourceInterface::RESOURCE_ATTRIBUTES] = $this->transformTransferToArray($this->attributes); |
||
172 | } |
||
173 | |||
174 | $response = $this->addLinksDataToResponse($response); |
||
175 | |||
176 | if (!$includeRelations) { |
||
177 | return $response; |
||
178 | } |
||
179 | |||
180 | $relationships = $this->toArrayRelationships(); |
||
181 | if ($relationships) { |
||
182 | $response[RestResourceInterface::RESOURCE_RELATIONSHIPS] = $relationships; |
||
183 | } |
||
184 | |||
185 | return $response; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @param \Spryker\Shared\Kernel\Transfer\AbstractTransfer|null $payload |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function setPayload(?AbstractTransfer $payload) |
||
194 | { |
||
195 | $this->payload = $payload; |
||
196 | |||
197 | return $this; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @return \Spryker\Shared\Kernel\Transfer\AbstractTransfer|null |
||
202 | */ |
||
203 | public function getPayload(): ?AbstractTransfer |
||
204 | { |
||
205 | return $this->payload; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param array $response |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | protected function addLinksDataToResponse(array $response): array |
||
214 | { |
||
215 | if ($this->links) { |
||
216 | $response[RestResourceInterface::RESOURCE_LINKS] = []; |
||
217 | foreach ($this->links as $link) { |
||
218 | $response[RestResourceInterface::RESOURCE_LINKS] += $link->toArray(); |
||
219 | } |
||
220 | } |
||
221 | |||
222 | return $response; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @return array |
||
227 | */ |
||
228 | protected function toArrayRelationships(): array |
||
229 | { |
||
230 | $relationships = []; |
||
231 | foreach ($this->relationships as $type => $typeRelationships) { |
||
232 | if (!isset($relationships[$type])) { |
||
233 | $relationships[$type][RestResourceInterface::RESOURCE_DATA] = []; |
||
234 | } |
||
235 | |||
236 | foreach ($typeRelationships as $relationship) { |
||
237 | $relationships[$type][RestResourceInterface::RESOURCE_DATA][] = [ |
||
238 | RestResourceInterface::RESOURCE_TYPE => $type, |
||
239 | RestResourceInterface::RESOURCE_ID => $relationship->getId(), |
||
240 | ]; |
||
241 | } |
||
242 | } |
||
243 | |||
244 | return $relationships; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Used for preventing have empty object instead of empty arrays in the response. |
||
249 | * Converts transfer object to array. |
||
250 | * Replaces empty ArrayObjects to empty arrays. |
||
251 | * |
||
252 | * @param \Spryker\Shared\Kernel\Transfer\AbstractTransfer $transfer |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | private function transformTransferToArray(AbstractTransfer $transfer): array |
||
257 | { |
||
258 | $transferData = $transfer->toArray(true, true); |
||
259 | |||
260 | return $this->transformEmptyArrayObjectToArray($transferData); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * @param array $transferData |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | private function transformEmptyArrayObjectToArray(array $transferData): array |
||
269 | { |
||
270 | foreach ($transferData as $key => $item) { |
||
271 | if (is_array($item)) { |
||
272 | $transferData[$key] = $this->transformEmptyArrayObjectToArray($item); |
||
273 | } |
||
274 | if (($item instanceof ArrayObject) && count($item) === 0) { |
||
275 | $transferData[$key] = []; |
||
276 | } |
||
277 | } |
||
278 | |||
279 | return $transferData; |
||
280 | } |
||
281 | } |
||
282 |