1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MovingImage\Client\VMPro\ApiClient; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use MovingImage\Client\VMPro\Collection\ChannelCollection; |
7
|
|
|
use MovingImage\Client\VMPro\Collection\VideoCollection; |
8
|
|
|
use MovingImage\Client\VMPro\Entity\Attachment; |
9
|
|
|
use MovingImage\Client\VMPro\Entity\Channel; |
10
|
|
|
use MovingImage\Client\VMPro\Entity\ChannelsRequestParameters; |
11
|
|
|
use MovingImage\Client\VMPro\Entity\EmbedCode; |
12
|
|
|
use MovingImage\Client\VMPro\Entity\Keyword; |
13
|
|
|
use MovingImage\Client\VMPro\Entity\UserInfo; |
14
|
|
|
use MovingImage\Client\VMPro\Entity\Video; |
15
|
|
|
use MovingImage\Client\VMPro\Entity\VideoDownloadUrl; |
16
|
|
|
use MovingImage\Client\VMPro\Entity\VideoManager; |
17
|
|
|
use MovingImage\Client\VMPro\Entity\VideoRequestParameters; |
18
|
|
|
use MovingImage\Client\VMPro\Entity\VideosRequestParameters; |
19
|
|
|
use MovingImage\Client\VMPro\Entity\Thumbnail; |
20
|
|
|
use MovingImage\Client\VMPro\Interfaces\ApiClientInterface; |
21
|
|
|
use MovingImage\Client\VMPro\Util\ChannelTrait; |
22
|
|
|
use MovingImage\Client\VMPro\Util\Logging\Traits\LoggerAwareTrait; |
23
|
|
|
use MovingImage\Client\VMPro\Util\SearchEndpointTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class AbstractApiClient. |
27
|
|
|
* |
28
|
|
|
* @author Ruben Knol <[email protected]> |
29
|
|
|
* @author Omid Rad <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
abstract class AbstractApiClient extends AbstractCoreApiClient implements ApiClientInterface |
32
|
|
|
{ |
33
|
|
|
use LoggerAwareTrait; |
34
|
|
|
use SearchEndpointTrait; |
35
|
|
|
use ChannelTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function getChannels($videoManagerId) |
41
|
|
|
{ |
42
|
|
|
$response = $this->makeRequest('GET', 'channels', [ |
43
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
$rootChannel = $this->deserialize($response->getBody(), Channel::class); |
47
|
|
|
$rootChannel->setChildren($this->sortChannels($rootChannel->getChildren())); |
48
|
|
|
|
49
|
|
|
return $rootChannel; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Since the VMPro API doesn't sort any more the returned channels, we have to do it on our side. |
54
|
|
|
* |
55
|
|
|
* @param ArrayCollection $channels |
56
|
|
|
* |
57
|
|
|
* @return ArrayCollection |
58
|
|
|
*/ |
59
|
|
|
protected function sortChannels(ArrayCollection $channels) |
60
|
|
|
{ |
61
|
|
|
$channels->map(function ($channel) { |
62
|
|
|
$channel->setChildren($this->sortChannels($channel->getChildren())); |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
$iterator = $channels->getIterator(); |
66
|
|
|
$iterator->uasort(function ($a, $b) { |
67
|
|
|
return $a->getName() > $b->getName(); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
return new ArrayCollection(iterator_to_array($iterator)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function createVideo( |
77
|
|
|
$videoManagerId, |
78
|
|
|
$fileName, |
79
|
|
|
$title = '', |
80
|
|
|
$description = '', |
81
|
|
|
$channel = null, |
82
|
|
|
$group = null, |
83
|
|
|
array $keywords = [], |
84
|
|
|
$autoPublish = null |
85
|
|
|
) { |
86
|
|
|
$response = $this->makeRequest('POST', 'videos', [ |
87
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
88
|
|
|
'json' => $this->buildJsonParameters( |
89
|
|
|
compact('fileName'), // Required parameters |
90
|
|
|
compact('title', 'description', 'channel', 'group', 'keywords', 'autoPublish') // Optional parameters |
91
|
|
|
), |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
// Guzzle 5+6 co-compatibility - Guzzle 6 for some reason |
95
|
|
|
// wraps headers in arrays. |
96
|
|
|
$videoLocation = is_array($response->getHeader('location')) |
97
|
|
|
? $response->getHeader('location')[0] |
98
|
|
|
: $response->getHeader('location'); |
99
|
|
|
|
100
|
|
|
$pieces = explode('/', $videoLocation); |
101
|
|
|
|
102
|
|
|
return end($pieces); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function getVideos($videoManagerId, VideosRequestParameters $parameters = null) |
109
|
|
|
{ |
110
|
|
|
$options = [ |
111
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
View Code Duplication |
if ($parameters) { |
|
|
|
|
115
|
|
|
$query = http_build_query($parameters->getContainer(), null, '&', PHP_QUERY_RFC3986); |
116
|
|
|
$options['query'] = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$response = $this->makeRequest('GET', 'videos', $options); |
120
|
|
|
$response = json_encode(json_decode($response->getBody()->getContents(), true)['videos']); |
121
|
|
|
|
122
|
|
|
return $this->deserialize($response, 'ArrayCollection<'.Video::class.'>'); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function getCount($videoManagerId, VideosRequestParameters $parameters = null) |
129
|
|
|
{ |
130
|
|
|
$options = [ |
131
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
132
|
|
|
]; |
133
|
|
|
|
134
|
|
View Code Duplication |
if ($parameters) { |
|
|
|
|
135
|
|
|
$query = http_build_query($parameters->getContainer(), null, '&', PHP_QUERY_RFC3986); |
136
|
|
|
$options['query'] = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$response = $this->makeRequest('GET', 'videos', $options); |
140
|
|
|
|
141
|
|
|
return json_decode($response->getBody()->getContents(), true)['total']; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function getVideoUploadUrl($videoManagerId, $videoId) |
148
|
|
|
{ |
149
|
|
|
$response = $this->makeRequest('GET', sprintf('videos/%s/url', $videoId), [ |
150
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
151
|
|
|
]); |
152
|
|
|
|
153
|
|
|
// Guzzle 5+6 co-compatibility - Guzzle 6 for some reason |
154
|
|
|
// wraps headers in arrays. |
155
|
|
|
return is_array($response->getHeader('location')) |
156
|
|
|
? $response->getHeader('location')[0] |
157
|
|
|
: $response->getHeader('location'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
|
|
public function updateVideo($videoManagerId, $videoId, $title, $description, $autoPublish = null) |
164
|
|
|
{ |
165
|
|
|
$this->makeRequest('PATCH', sprintf('videos/%s', $videoId), [ |
166
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
167
|
|
|
'json' => $this->buildJsonParameters([], compact('title', 'description', 'autoPublish')), |
168
|
|
|
]); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
|
|
public function addVideoToChannel($videoManagerId, $videoId, $channelId) |
175
|
|
|
{ |
176
|
|
|
$this->makeRequest('POST', sprintf('channels/%s/videos/%s', $channelId, $videoId), [ |
177
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
178
|
|
|
]); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
|
|
public function removeVideoFromChannel($videoManagerId, $videoId, $channelId) |
185
|
|
|
{ |
186
|
|
|
$this->makeRequest('DELETE', sprintf('channels/%s/videos/%s', $channelId, $videoId), [ |
187
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
188
|
|
|
]); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function setCustomMetaData($videoManagerId, $videoId, $metadata) |
195
|
|
|
{ |
196
|
|
|
$this->makeRequest('PATCH', sprintf('videos/%s/metadata', $videoId), [ |
197
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
198
|
|
|
'json' => $metadata, |
199
|
|
|
]); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* {@inheritdoc} |
204
|
|
|
*/ |
205
|
|
|
public function getEmbedCode($videoManagerId, $videoId, $playerDefinitionId, $embedType = 'html') |
206
|
|
|
{ |
207
|
|
|
$url = sprintf( |
208
|
|
|
'videos/%s/embed-codes?player_definition_id=%s&embed_type=%s', |
209
|
|
|
$videoId, |
210
|
|
|
$playerDefinitionId, |
211
|
|
|
$embedType |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
if ($this->cacheTtl) { |
215
|
|
|
$url = sprintf('%s&token_lifetime_in_seconds=%s', $url, $this->cacheTtl); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$response = $this->makeRequest('GET', $url, [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]); |
219
|
|
|
|
220
|
|
|
$data = \json_decode($response->getBody(), true); |
221
|
|
|
$embedCode = new EmbedCode(); |
222
|
|
|
$embedCode->setCode($data['embedCode']); |
223
|
|
|
|
224
|
|
|
return $embedCode; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
|
|
public function deleteVideo($videoManagerId, $videoId) |
231
|
|
|
{ |
232
|
|
|
$this->makeRequest('DELETE', sprintf('videos/%s', $videoId), [ |
233
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
234
|
|
|
]); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* {@inheritdoc} |
239
|
|
|
*/ |
240
|
|
|
public function getVideo($videoManagerId, $videoId, VideoRequestParameters $parameters = null) |
241
|
|
|
{ |
242
|
|
|
$options = [ |
243
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
244
|
|
|
]; |
245
|
|
|
|
246
|
|
|
if ($parameters) { |
247
|
|
|
$options['query'] = $parameters->getContainer(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$response = $this->makeRequest( |
251
|
|
|
'GET', |
252
|
|
|
sprintf('videos/%s', $videoId), |
253
|
|
|
$options |
254
|
|
|
); |
255
|
|
|
|
256
|
|
|
return $this->deserialize($response->getBody()->getContents(), Video::class); |
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* {@inheritdoc} |
261
|
|
|
*/ |
262
|
|
View Code Duplication |
public function getAttachments($videoManagerId, $videoId) |
|
|
|
|
263
|
|
|
{ |
264
|
|
|
$response = $this->makeRequest( |
265
|
|
|
'GET', |
266
|
|
|
sprintf('videos/%s/attachments', $videoId), |
267
|
|
|
[self::OPT_VIDEO_MANAGER_ID => $videoManagerId] |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
return $this->deserialize($response->getBody()->getContents(), 'ArrayCollection<'.Attachment::class.'>'); |
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* {@inheritdoc} |
275
|
|
|
*/ |
276
|
|
View Code Duplication |
public function getChannelAttachments($videoManagerId, $channelId) |
|
|
|
|
277
|
|
|
{ |
278
|
|
|
$response = $this->makeRequest( |
279
|
|
|
'GET', |
280
|
|
|
sprintf('channels/%s/attachments', $channelId), |
281
|
|
|
[self::OPT_VIDEO_MANAGER_ID => $videoManagerId] |
282
|
|
|
); |
283
|
|
|
|
284
|
|
|
return $this->deserialize($response->getBody()->getContents(), 'ArrayCollection<'.Attachment::class.'>'); |
|
|
|
|
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* {@inheritdoc} |
289
|
|
|
*/ |
290
|
|
View Code Duplication |
public function getKeywords($videoManagerId, $videoId) |
|
|
|
|
291
|
|
|
{ |
292
|
|
|
$uri = is_null($videoId) |
293
|
|
|
? 'keyword/find' |
294
|
|
|
: sprintf('videos/%s/keywords', $videoId); |
295
|
|
|
|
296
|
|
|
$response = $this->makeRequest( |
297
|
|
|
'GET', |
298
|
|
|
$uri, |
299
|
|
|
[self::OPT_VIDEO_MANAGER_ID => $videoManagerId] |
300
|
|
|
); |
301
|
|
|
|
302
|
|
|
return $this->deserialize($response->getBody()->getContents(), 'ArrayCollection<'.Keyword::class.'>'); |
|
|
|
|
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* {@inheritdoc} |
307
|
|
|
*/ |
308
|
|
|
public function updateKeywords($videoManagerId, $videoId, $keywords) |
309
|
|
|
{ |
310
|
|
|
//remove all keywords |
311
|
|
|
foreach ($this->getKeywords($videoManagerId, $videoId) as $keyword) { |
|
|
|
|
312
|
|
|
$this->deleteKeyword($videoManagerId, $videoId, $keyword->getId()); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
//add new |
316
|
|
|
foreach ($keywords as $keyword) { |
317
|
|
|
$this->makeRequest('POST', sprintf('videos/%s/keywords', $videoId), [ |
318
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
319
|
|
|
'json' => ['text' => $keyword], |
320
|
|
|
]); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* {@inheritdoc} |
326
|
|
|
*/ |
327
|
|
|
public function deleteKeyword($videoManagerId, $videoId, $keywordId) |
328
|
|
|
{ |
329
|
|
|
$this->makeRequest('DELETE', sprintf('videos/%s/keywords/%s', $videoId, $keywordId), [ |
330
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
331
|
|
|
]); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* {@inheritdoc} |
336
|
|
|
*/ |
337
|
|
|
public function searchVideos($videoManagerId, VideosRequestParameters $parameters = null, $searchQuery = null) |
338
|
|
|
{ |
339
|
|
|
$options = $this->getRequestOptionsForSearchVideosEndpoint($videoManagerId, $parameters); |
340
|
|
|
if ($searchQuery) { |
|
|
|
|
341
|
|
|
$options['query'] = sprintf('(%s) AND (%s)', $options['query'], $searchQuery); |
342
|
|
|
} |
343
|
|
|
$response = $this->makeRequest('POST', 'search', ['json' => $options]); |
344
|
|
|
$response = $this->normalizeSearchVideosResponse($response->getBody()->getContents()); |
345
|
|
|
|
346
|
|
|
$collection = $this->deserialize($response, VideoCollection::class); |
347
|
|
|
|
348
|
|
|
return $collection; |
|
|
|
|
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* {@inheritdoc} |
353
|
|
|
*/ |
354
|
|
|
public function searchChannels($videoManagerId, ChannelsRequestParameters $parameters = null) |
355
|
|
|
{ |
356
|
|
|
$options = $this->getRequestOptionsForSearchChannelsEndpoint($videoManagerId, $parameters); |
357
|
|
|
$response = $this->makeRequest('POST', 'search', ['json' => $options]); |
358
|
|
|
$response = $this->normalizeSearchChannelsResponse($response->getBody()->getContents()); |
359
|
|
|
/** @var ChannelCollection $collection */ |
360
|
|
|
$collection = $this->deserialize($response, ChannelCollection::class); |
361
|
|
|
|
362
|
|
|
//builds parent/children relations on all channels |
363
|
|
|
$channels = $this->setChannelRelations($collection->getChannels()); |
|
|
|
|
364
|
|
|
$collection->setChannels($channels); |
365
|
|
|
|
366
|
|
|
return $collection; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* {@inheritdoc} |
371
|
|
|
*/ |
372
|
|
|
public function getVideoManagers() |
373
|
|
|
{ |
374
|
|
|
$response = $this->makeRequest('GET', '', []); |
375
|
|
|
|
376
|
|
|
return $this->deserialize($response->getBody()->getContents(), 'ArrayCollection<'.VideoManager::class.'>'); |
|
|
|
|
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* {@inheritdoc} |
381
|
|
|
*/ |
382
|
|
View Code Duplication |
public function getVideoDownloadUrls($videoManagerId, $videoId) |
|
|
|
|
383
|
|
|
{ |
384
|
|
|
$options = [ |
385
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
386
|
|
|
]; |
387
|
|
|
|
388
|
|
|
$response = $this->makeRequest( |
389
|
|
|
'GET', |
390
|
|
|
sprintf('videos/%s/download-urls', $videoId), |
391
|
|
|
$options |
392
|
|
|
); |
393
|
|
|
|
394
|
|
|
$response = $response->getBody()->getContents(); |
395
|
|
|
|
396
|
|
|
return $this->deserialize($response, 'ArrayCollection<'.VideoDownloadUrl::class.'>'); |
|
|
|
|
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* {@inheritdoc} |
401
|
|
|
*/ |
402
|
|
|
public function createThumbnailByTimestamp($videoManagerId, $videoId, $timestamp) |
403
|
|
|
{ |
404
|
|
|
$options = [ |
405
|
|
|
self::OPT_VIDEO_MANAGER_ID => intval($videoManagerId), |
406
|
|
|
]; |
407
|
|
|
|
408
|
|
|
$response = $this->makeRequest( |
409
|
|
|
'POST', |
410
|
|
|
'videos/'.$videoId.'/thumbnails?timestamp='.intval($timestamp), |
411
|
|
|
$options |
412
|
|
|
); |
413
|
|
|
|
414
|
|
|
if (preg_match('/\/thumbnails\/([0-9]*)/', $response->getHeader('Location')[0], $match)) { |
415
|
|
|
return (new Thumbnail())->setId(intval($match[1])); |
416
|
|
|
} |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* {@inheritdoc} |
421
|
|
|
*/ |
422
|
|
|
public function getThumbnail($videoManagerId, $videoId, $thumbnailId) |
423
|
|
|
{ |
424
|
|
|
$options = [ |
425
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
426
|
|
|
]; |
427
|
|
|
|
428
|
|
|
$response = $this->makeRequest( |
429
|
|
|
'GET', |
430
|
|
|
'videos/'.$videoId.'/thumbnails/'.$thumbnailId.'/url', |
431
|
|
|
$options |
432
|
|
|
); |
433
|
|
|
|
434
|
|
|
$result = \json_decode($response->getBody()->getContents(), true); |
435
|
|
|
|
436
|
|
|
if (isset($result['downloadUrl'])) { |
437
|
|
|
return (new Thumbnail()) |
438
|
|
|
->setId(intval($thumbnailId)) |
439
|
|
|
->setUrl($result['downloadUrl']); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
return null; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* {@inheritdoc} |
447
|
|
|
*/ |
448
|
|
|
public function updateThumbnail($videoManagerId, $videoId, $thumbnailId, $active) |
449
|
|
|
{ |
450
|
|
|
$options = [ |
451
|
|
|
self::OPT_VIDEO_MANAGER_ID => $videoManagerId, |
452
|
|
|
'json' => ['active' => boolval($active)], |
453
|
|
|
]; |
454
|
|
|
|
455
|
|
|
$this->makeRequest( |
456
|
|
|
'PATCH', |
457
|
|
|
'videos/'.$videoId.'/thumbnails/'.intval($thumbnailId), |
458
|
|
|
$options |
459
|
|
|
); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* {@inheritdoc} |
464
|
|
|
*/ |
465
|
|
|
public function getUserInfo($idToken) |
466
|
|
|
{ |
467
|
|
|
$options = [ |
468
|
|
|
'body' => json_encode(['jwt_id_token' => $idToken]), |
469
|
|
|
'headers' => [ |
470
|
|
|
'Content-Type' => 'application/json', |
471
|
|
|
], |
472
|
|
|
]; |
473
|
|
|
|
474
|
|
|
$response = $this->makeRequest('POST', 'corp-tube-admin/user-info', $options); |
475
|
|
|
|
476
|
|
|
return $this->deserialize($response->getBody()->getContents(), UserInfo::class); |
|
|
|
|
477
|
|
|
} |
478
|
|
|
} |
479
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.