forked from php-opencloud/openstack
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractResource.php
More file actions
168 lines (140 loc) · 4.49 KB
/
Copy pathAbstractResource.php
File metadata and controls
168 lines (140 loc) · 4.49 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
<?php
declare(strict_types=1);
namespace OpenStack\Common\Resource;
use OpenStack\Common\Transport\Serializable;
use OpenStack\Common\Transport\Utils;
use Psr\Http\Message\ResponseInterface;
/**
* Represents a top-level abstraction of a remote API resource. Usually a resource represents a discrete
* entity such as a Server, Container, Load Balancer. Apart from a representation of state, a resource can
* also execute RESTFul operations on itself (updating, deleting, listing) or on other models.
*/
abstract class AbstractResource implements ResourceInterface, Serializable
{
/**
* The JSON key that indicates how the API nests singular resources. For example, when
* performing a GET, it could respond with ``{"server": {"id": "12345"}}``. In this case,
* "server" is the resource key, since the essential state of the server is nested inside.
*
* @var string
*/
protected $resourceKey;
/**
* An array of aliases that will be checked when the resource is being populated. For example,.
*
* 'FOO_BAR' => 'fooBar'
*
* will extract FOO_BAR from the response, and save it as 'fooBar' in the resource.
*
* @var array
*/
protected $aliases = [];
/**
* Populates the current resource from a response object.
*
* @param ResponseInterface $response
*
* @return AbstractResource
*/
public function populateFromResponse(ResponseInterface $response)
{
if (0 === strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
$json = Utils::jsonDecode($response);
if (!empty($json)) {
$this->populateFromArray(Utils::flattenJson($json, $this->resourceKey));
}
}
return $this;
}
/**
* Populates the current resource from a data array.
*
* @param array $array
*
* @return mixed|void
*/
public function populateFromArray(array $array)
{
$aliases = $this->getAliases();
foreach ($array as $key => $val) {
$alias = $aliases[$key] ?? false;
if ($alias instanceof Alias) {
$key = $alias->propertyName;
$val = $alias->getValue($this, $val);
}
if (property_exists($this, $key)) {
$this->{$key} = $val;
}
}
return $this;
}
/**
* Constructs alias objects.
*
* @return Alias[]
*/
protected function getAliases(): array
{
$aliases = [];
foreach ((array) $this->aliases as $alias => $property) {
$aliases[$alias] = new Alias($property);
}
return $aliases;
}
/**
* Internal method which retrieves the values of provided keys.
*
* @param array $keys
*
* @return array
*/
protected function getAttrs(array $keys)
{
$output = [];
foreach ($keys as $key) {
if (property_exists($this, $key) && $this->$key !== null) {
$output[$key] = $this->$key;
}
}
return $output;
}
public function model(string $class, $data = null): ResourceInterface
{
$model = new $class();
// @codeCoverageIgnoreStart
if (!$model instanceof ResourceInterface) {
throw new \RuntimeException(sprintf('%s does not implement %s', $class, ResourceInterface::class));
}
// @codeCoverageIgnoreEnd
if ($data instanceof ResponseInterface) {
$model->populateFromResponse($data);
} elseif (is_array($data)) {
$model->populateFromArray($data);
}
return $model;
}
public function serialize(): \stdClass
{
$output = new \stdClass();
foreach ((new \ReflectionClass($this))->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
$name = $property->getName();
$val = $this->{$name};
$fn = function ($val) {
if ($val instanceof Serializable) {
return $val->serialize();
} elseif ($val instanceof \DateTimeImmutable) {
return $val->format('c');
} else {
return $val;
}
};
if (is_array($val)) {
foreach ($val as $sk => $sv) {
$val[$sk] = $fn($sv);
}
}
$output->{$name} = $fn($val);
}
return $output;
}
}