-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathApiResourceBase.php
More file actions
595 lines (526 loc) · 17.7 KB
/
Copy pathApiResourceBase.php
File metadata and controls
595 lines (526 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<?php
declare(strict_types=1);
/** @noinspection PhpDocMissingThrowsInspection */
/** @noinspection PhpUnhandledExceptionInspection */
namespace Platformsh\Client\Model;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;
use Platformsh\Client\Exception\ApiResponseException;
use Platformsh\Client\Exception\OperationUnavailableException;
use Psr\Http\Message\RequestInterface;
/**
* The base class for API resources.
*/
abstract class ApiResourceBase implements \ArrayAccess
{
protected static ?string $collectionItemsKey;
protected static array $required = [];
protected ClientInterface $client;
protected string $baseUrl;
protected array $data;
protected bool $isFull = false;
protected ?Collection $parentCollection = null;
/**
* @param array $data The raw data for the resource
* (as deserialized from JSON).
* @param string|null $baseUrl The absolute URL to the resource or its
* collection.
* @param ?ClientInterface $client A suitably configured Guzzle client.
* @param bool $full Whether the data is a complete
* representation of the resource.
*/
public function __construct(array $data, ?string $baseUrl = null, ?ClientInterface $client = null, bool $full = true)
{
$this->client = $client ?: new Client();
$this->baseUrl = (string) $baseUrl;
$this->isFull = $full;
$this->setData($data);
}
/**
* Magic getter, allowing resource properties to be accessed.
*
* Properties can be documented in implementing classes' docblocks.
*
* @return mixed
*/
public function __get(string $name)
{
return $this->getProperty($name, false);
}
/**
* @return bool
*/
public function __isset(string $name)
{
return $this->hasProperty($name);
}
/**
* Prevent setting magic properties.
*
* @throws \BadMethodCallException
*/
public function __set(string $name, mixed $value)
{
throw new \BadMethodCallException('Properties are read-only');
}
public function setBaseUrl(string $baseUrl): void
{
$this->baseUrl = $baseUrl;
}
public function offsetExists(mixed $offset): bool
{
return $this->hasProperty($offset);
}
public function offsetGet(mixed $offset): mixed
{
return $this->getProperty($offset, false);
}
/**
* @throws \BadMethodCallException
*/
public function offsetSet(mixed $offset, $value): void
{
throw new \BadMethodCallException('Properties are read-only');
}
/**
* @throws \BadMethodCallException
*/
public function offsetUnset(mixed $offset): void
{
throw new \BadMethodCallException('Properties are read-only');
}
/**
* Get all the API data for this resource.
*/
public function getData(): array
{
return $this->data;
}
/**
* Ensure that this is a full representation of the resource (not a stub).
*/
public function ensureFull(): void
{
if (! $this->isFull) {
$this->refresh();
}
}
/**
* Get a resource by its ID.
*
* @param string $id The ID of the resource, or the
* full URL.
* @param ?string $collectionUrl The URL of the collection.
* @param ClientInterface $client A suitably configured Guzzle
* client.
*
* @return static|false The resource object, or false if the resource is
* not found.
*@throws \InvalidArgumentException if the resource ID is invalid
*/
public static function get(string $id, ?string $collectionUrl, ClientInterface $client): false|static
{
if ($id === '.' || $id === '..') {
throw new \InvalidArgumentException('Invalid resource ID: ' . $id);
}
try {
$url = $collectionUrl ? rtrim($collectionUrl, '/') . '/' . urlencode($id) : $id;
$request = new Request('get', $url);
$data = self::send($request, $client);
return new static($data, $url, $client, true);
} catch (BadResponseException $e) {
if ($e->getResponse()->getStatusCode() === 404) {
return false;
}
throw $e;
}
}
/**
* Create a resource.
*
* @return Result
* @noinspection PhpMissingReturnTypeInspection
*/
public static function create(array $body, string $collectionUrl, ClientInterface $client)
{
if ($errors = static::checkNew($body)) {
$message = 'Cannot create resource due to validation error(s): ' . implode('; ', $errors);
throw new \InvalidArgumentException($message);
}
$request = new Request('post', $collectionUrl, [
'Content-Type' => 'application/json',
], \GuzzleHttp\Utils::jsonEncode($body));
$data = self::send($request, $client);
return new Result($data, $collectionUrl, $client, static::class);
}
/**
* Send a Guzzle request.
*
* Using this method allows exceptions to be standardized.
*
* @internal
*/
public static function send(RequestInterface $request, ClientInterface $client, array $options = []): array
{
try {
$response = $client->send($request, $options);
$body = $response->getBody()->getContents();
$data = [];
if ($body) {
$response->getBody()->seek(0);
$body = $response->getBody()->getContents();
$data = \GuzzleHttp\Utils::jsonDecode($body, true);
}
return (array) $data;
} catch (GuzzleException $e) {
throw ApiResponseException::wrapGuzzleException($e);
}
}
/**
* Get the required properties for creating a new resource.
*/
public static function getRequired(): array
{
return static::$required;
}
/**
* Returns a list of resources (a collection).
*
* @param string $url The collection URL.
* @param int $limit A limit on the number of resources to
* return. Deprecated.
* @param array $options An array of additional Guzzle request
* options.
* @param ClientInterface $client A suitably configured Guzzle client.
*
* @return static[]
*/
public static function getCollection(string $url, int $limit, array $options, ClientInterface $client): array
{
$items = static::getCollectionWithParent($url, $client, $options)['items'];
if (! empty($limit) && count($items) > $limit) {
$items = array_slice($items, 0, $limit);
}
return $items;
}
/**
* Returns a list of resources and the Collection that contained them.
*
* @param string $url The collection URL.
* @param ClientInterface $client A suitably configured Guzzle client.
* @param array $options An array of additional Guzzle request
* options.
*
* @return array{items: static[], collection: Collection}
*/
public static function getCollectionWithParent(string $url, ClientInterface $client, array $options = []): array
{
$request = new Request('GET', $url);
$data = self::send($request, $client, $options);
$collection = new Collection($data, $client, $url);
return [
'items' => static::wrapCollection($collection, $url, $client),
'collection' => $collection,
];
}
/**
* Create an array of resource instances from a collection's JSON data.
*
* @param array|Collection $data The deserialized JSON from the
* collection (i.e. a list of resources,
* each of which is an array of data).
* @param string $baseUrl The URL to the collection.
* @param ClientInterface $client A suitably configured Guzzle client.
*
* @return static[]
*/
public static function wrapCollection(array|Collection $data, string $baseUrl, ClientInterface $client): array
{
if ($data instanceof Collection) {
$parent = $data;
$data = $data->getData();
} else {
$parent = new Collection($data, $client, $baseUrl);
}
$resources = [];
$items = $data;
if (isset(static::$collectionItemsKey)) {
$items = $items[static::$collectionItemsKey];
}
foreach ($items as $item) {
$resource = new static($item, $baseUrl, $client);
$resource->setParentCollection($parent);
$resources[] = $resource;
}
return $resources;
}
/**
* Execute an operation on the resource.
*/
public function runOperation(string $op, string $method = 'POST', array $body = []): Result
{
if (! $this->operationAvailable($op, true)) {
throw new OperationUnavailableException("Operation not available: {$op}");
}
$request = new Request($method, $this->getLink("#{$op}"), [
'Content-Type' => 'application/json',
], $body ? \json_encode($body) : null);
$data = $this->send($request, $this->client);
return new Result($data, $this->baseUrl, $this->client, static::class);
}
/**
* Check whether a property exists in the resource.
*/
public function hasProperty(string $property, bool $lazyLoad = true): bool
{
if (! $this->isProperty($property)) {
return false;
}
if (! array_key_exists($property, $this->data) && $lazyLoad) {
$this->ensureFull();
}
return array_key_exists($property, $this->data);
}
/**
* Get a property of the resource.
*
* @return mixed|null
* The property value, or null if the property does not exist (and
* $required is false).
*@throws \InvalidArgumentException If $required is true and the property
* is not found.
*/
public function getProperty(string $property, bool $required = true, bool $lazyLoad = true): mixed
{
if (! $this->hasProperty($property, $lazyLoad)) {
if ($required) {
throw new \InvalidArgumentException("Property not found: {$property}");
}
return null;
}
return $this->data[$property];
}
/**
* Delete the resource.
*/
public function delete(): Result
{
$data = $this->sendRequest($this->getUri(), 'delete');
return new Result($data, $this->getUri(), $this->client, static::class);
}
/**
* Update the resource.
*
* This updates the resource's internal data with the API response.
*/
public function update(array $values): Result
{
if ($errors = $this->checkUpdate($values)) {
$message = 'Cannot update resource due to validation error(s): ' . implode('; ', $errors);
throw new \InvalidArgumentException($message);
}
$data = $this->runOperation('edit', 'patch', $values)->getData();
if (isset($data['_embedded']['entity'])) {
$this->setData($data['_embedded']['entity']);
$this->isFull = true;
}
return new Result($data, $this->baseUrl, $this->client, static::class);
}
/**
* Get the resource's URI.
*/
public function getUri(bool $absolute = true): string
{
return $this->getLink('self', $absolute);
}
/**
* Refresh the resource.
*/
public function refresh(array $options = []): void
{
$request = new Request('get', $this->getUri());
$this->setData(self::send($request, $this->client, $options));
$this->isFull = true;
}
/**
* Check whether an operation is available on the resource.
*/
public function operationAvailable(string $op, bool $refreshDuringCheck = false): bool
{
// Ensure this resource is a full representation.
if (! $this->isFull) {
$this->refresh();
$refreshDuringCheck = false;
}
// Check if the operation is available in the HAL links.
$available = $this->isOperationAvailable($op);
if ($available) {
return true;
}
// If not, and $refreshDuringCheck is on, then refresh the resource.
if ($refreshDuringCheck) {
$this->refresh();
$available = $this->isOperationAvailable($op);
}
return $available;
}
/**
* Check whether the resource has a link.
*/
public function hasLink($rel): bool
{
return isset($this->data['_links'][$rel]['href']);
}
/**
* Get a link for a given resource relation.
*/
public function getLink(string $rel, bool $absolute = true): string
{
if (! $this->hasLink($rel)) {
throw new \InvalidArgumentException("Link not found: {$rel}");
}
$url = $this->data['_links'][$rel]['href'];
if ($absolute || str_contains($url, '//')) {
$url = $this->makeAbsoluteUrl($url);
}
return $url;
}
/**
* Get a list of this resource's property names.
*
* @return string[]
*/
public function getPropertyNames(): array
{
return array_filter(array_keys($this->data), [$this, 'isProperty']);
}
/**
* Get an array of this resource's properties and their values.
*/
public function getProperties(bool $lazyLoad = true): array
{
if ($lazyLoad) {
$this->ensureFull();
}
$keys = $this->getPropertyNames();
return array_intersect_key($this->data, array_flip($keys));
}
/**
* Returns the wrapping collection, if this resource's data was fetched via one.
*
* Useful for pagination.
*/
public function getParentCollection(): ?Collection
{
return $this->parentCollection;
}
/**
* A simple helper function to send an HTTP request.
*/
protected function sendRequest(string $url, string $method = 'get', array $options = []): array
{
return $this->send(
new Request($method, $url),
$this->client,
$options
);
}
/**
* Validate a new resource.
*
* @return string[] An array of validation errors.
*/
protected static function checkNew(array $data): array
{
$errors = [];
if ($missing = array_diff(static::getRequired(), array_keys($data))) {
$errors[] = 'Missing: ' . implode(', ', $missing);
}
foreach ($data as $key => $value) {
$errors += static::checkProperty($key, $value);
}
return $errors;
}
/**
* Validate a property of the resource, for creating or updating.
*
* @return string[] An array of validation errors.
*/
protected static function checkProperty(string $property, mixed $value): array
{
return [];
}
/**
* Run a long-running operation.
*
*@see Resource::runOperation()
*
* @deprecated use runOperation() instead
*/
protected function runLongOperation(string $op, string $method = 'post', array $body = []): Activity
{
@trigger_error('This method is deprecated as actions may return multiple activities. Use runOperation() if possible.', E_USER_DEPRECATED);
$result = $this->runOperation($op, $method, $body);
$activities = $result->getActivities();
if (count($activities) !== 1) {
trigger_error(sprintf('Expected one activity, found %d', count($activities)), E_USER_WARNING);
}
return reset($activities);
}
/**
* Validate values for update.
*
* @return string[] An array of validation errors.
*/
protected static function checkUpdate(array $values): array
{
$errors = [];
foreach ($values as $key => $value) {
$errors += static::checkProperty($key, $value);
}
return $errors;
}
protected function setData(array $data): void
{
$this->data = $data;
}
/**
* Internal: check whether an operation is available on the resource.
*/
protected function isOperationAvailable(string $op): bool
{
return isset($this->data['_links']["#{$op}"]['href']);
}
/**
* Make a URL absolute, based on the base URL.
*/
protected function makeAbsoluteUrl(string $relativeUrl, ?string $baseUrl = null): string
{
$baseUrl = $baseUrl ?: $this->baseUrl;
if (empty($baseUrl)) {
throw new \RuntimeException('No base URL');
}
$base = Utils::uriFor($baseUrl);
$target = Utils::uriFor($relativeUrl);
// Ensure an absolute base URL overrides an absolute target URL.
if ($base->getScheme() !== '' && \in_array($target->getScheme(), ['http', 'https'], true)) {
return (string) $base->withPath($target->getPath());
}
return (string) $base->withPath((string) $target);
}
protected function isProperty(string $key): bool
{
return $key !== '_links' && $key !== '_embedded';
}
/**
* Sets a parent collection for this resource.
*/
protected function setParentCollection(Collection $parent): void
{
$this->parentCollection = $parent;
}
}