-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathResult.php
More file actions
96 lines (80 loc) · 2.47 KB
/
Result.php
File metadata and controls
96 lines (80 loc) · 2.47 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
<?php
declare(strict_types=1);
namespace Platformsh\Client\Model;
use GuzzleHttp\ClientInterface;
/**
* A class wrapping the result of an API call.
*/
class Result extends ApiResourceBase
{
protected string $resourceClass;
public function __construct(array $data, $baseUrl, ClientInterface $client, string $className)
{
parent::__construct($data, $baseUrl, $client);
$this->setResourceClass($className);
}
/**
* @internal
*/
public function setResourceClass(string $className): void
{
if (! class_exists($className)) {
throw new \InvalidArgumentException("Class not found: {$className}");
}
$this->resourceClass = $className;
}
/**
* Count the activities embedded in the result.
*/
public function countActivities(): int
{
if (! isset($this->data['_embedded']['activities'])) {
return 0;
}
return count($this->data['_embedded']['activities']);
}
/**
* Get activities embedded in the result.
*
* A result could embed 0, 1, or many activities.
*
* @return Activity[]
*/
public function getActivities(): array
{
if (! isset($this->data['_embedded']['activities'])) {
return [];
}
$activities = [];
foreach ($this->data['_embedded']['activities'] as $data) {
$activities[] = new Activity($data, $this->baseUrl, $this->client);
}
return $activities;
}
/**
* Get the entity embedded in the result.
*
* @throws \Exception If no entity was embedded.
*
* @return ApiResourceBase
* An instance of ApiResourceBase - the implementing class name was set
* when this Result was instantiated.
*/
public function getEntity(): ApiResourceBase
{
if (! isset($this->data['_embedded']['entity'])) {
throw new \Exception('No entity found in result');
}
$data = $this->data['_embedded']['entity'];
$resourceClass = $this->resourceClass;
return new $resourceClass($data, $this->baseUrl, $this->client);
}
public function update(array $values): self
{
throw new \BadMethodCallException('Cannot update() a Result instance directly. Perhaps use getEntity().');
}
public function delete(): self
{
throw new \BadMethodCallException('Cannot delete() a Result instance directly. Perhaps use getEntity().');
}
}