1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Common\Resource; |
4
|
|
|
|
5
|
|
|
use OpenStack\Common\Transport\Serializable; |
6
|
|
|
use OpenStack\Common\Transport\Utils; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Represents a top-level abstraction of a remote API resource. Usually a resource represents a discrete |
11
|
|
|
* entity such as a Server, Container, Load Balancer. Apart from a representation of state, a resource can |
12
|
|
|
* also execute RESTFul operations on itself (updating, deleting, listing) or on other models. |
13
|
|
|
* |
14
|
|
|
* @package OpenStack\Common\Resource |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractResource implements ResourceInterface, Serializable |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The JSON key that indicates how the API nests singular resources. For example, when |
20
|
|
|
* performing a GET, it could respond with ``{"server": {"id": "12345"}}``. In this case, |
21
|
|
|
* "server" is the resource key, since the essential state of the server is nested inside. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $resourceKey; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* An array of aliases that will be checked when the resource is being populated. For example, |
29
|
|
|
* |
30
|
|
|
* 'FOO_BAR' => 'fooBar' |
31
|
|
|
* |
32
|
|
|
* will extract FOO_BAR from the response, and save it as 'fooBar' in the resource. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $aliases = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Populates the current resource from a response object. |
40
|
|
|
* |
41
|
|
|
* @param ResponseInterface $response |
42
|
|
|
* |
43
|
|
|
* @return AbstractResource |
44
|
|
|
*/ |
45
|
|
|
public function populateFromResponse(ResponseInterface $response): self |
46
|
|
|
{ |
47
|
|
|
if (strpos($response->getHeaderLine('Content-Type'), 'application/json') === 0) { |
48
|
|
|
$json = Utils::jsonDecode($response); |
49
|
|
|
if (!empty($json)) { |
50
|
|
|
$this->populateFromArray(Utils::flattenJson($json, $this->resourceKey)); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Populates the current resource from a data array. |
59
|
|
|
* |
60
|
|
|
* @param array $array |
61
|
|
|
* |
62
|
|
|
* @return mixed|void |
63
|
74 |
|
*/ |
64
|
|
|
public function populateFromArray(array $array): self |
65
|
74 |
|
{ |
66
|
61 |
|
$aliases = $this->getAliases(); |
67
|
61 |
|
|
68
|
61 |
|
foreach ($array as $key => $val) { |
69
|
61 |
|
$alias = $aliases[$key] ?? false; |
70
|
61 |
|
|
71
|
|
|
if ($alias instanceof Alias) { |
72
|
74 |
|
$key = $alias->propertyName; |
73
|
|
|
$val = $alias->getValue($this, $val); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (property_exists($this, $key)) { |
77
|
|
|
$this->{$key} = $val; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
133 |
|
} |
83
|
|
|
|
84
|
133 |
|
/** |
85
|
|
|
* Constructs alias objects |
86
|
133 |
|
* |
87
|
133 |
|
* @return Alias[] |
88
|
|
|
*/ |
89
|
133 |
|
protected function getAliases(): array |
90
|
131 |
|
{ |
91
|
128 |
|
$aliases = []; |
92
|
128 |
|
|
93
|
|
|
foreach ((array)$this->aliases as $alias => $property) { |
94
|
131 |
|
$aliases[$alias] = new Alias($property); |
95
|
131 |
|
} |
96
|
133 |
|
|
97
|
133 |
|
return $aliases; |
98
|
|
|
} |
99
|
128 |
|
|
100
|
2 |
|
/** |
101
|
128 |
|
* Internal method which retrieves the values of provided keys. |
102
|
6 |
|
* |
103
|
6 |
|
* @param array $keys |
104
|
6 |
|
* |
105
|
6 |
|
* @return array |
106
|
6 |
|
*/ |
107
|
128 |
|
protected function getAttrs(array $keys) |
108
|
28 |
|
{ |
109
|
127 |
|
$output = []; |
110
|
11 |
|
|
111
|
11 |
|
foreach ($keys as $key) { |
112
|
|
|
if (property_exists($this, $key) && $this->$key !== null) { |
113
|
128 |
|
$output[$key] = $this->$key; |
114
|
|
|
} |
115
|
|
|
} |
116
|
124 |
|
|
117
|
|
|
return $output; |
118
|
124 |
|
} |
119
|
124 |
|
|
120
|
124 |
View Code Duplication |
public function model(string $class, $data = null): ResourceInterface |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$model = new $class(); |
123
|
12 |
|
|
124
|
|
|
// @codeCoverageIgnoreStart |
125
|
12 |
|
if (!$model instanceof ResourceInterface) { |
126
|
12 |
|
throw new \RuntimeException(sprintf('%s does not implement %s', $class, ResourceInterface::class)); |
127
|
12 |
|
} |
128
|
12 |
|
// @codeCoverageIgnoreEnd |
129
|
|
|
|
130
|
12 |
|
if ($data instanceof ResponseInterface) { |
131
|
|
|
$model->populateFromResponse($data); |
132
|
|
|
} elseif (is_array($data)) { |
133
|
131 |
|
$model->populateFromArray($data); |
134
|
|
|
} |
135
|
131 |
|
|
136
|
|
|
return $model; |
137
|
131 |
|
} |
138
|
3 |
|
|
139
|
|
|
public function serialize(): \stdClass |
140
|
|
|
{ |
141
|
128 |
|
$output = new \stdClass(); |
142
|
128 |
|
|
143
|
128 |
|
foreach ((new \ReflectionClass($this))->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { |
144
|
|
|
$name = $property->getName(); |
145
|
|
|
$val = $this->{$name}; |
146
|
|
|
|
147
|
|
|
$fn = function ($val) { |
148
|
|
|
return ($val instanceof Serializable) ? $val->serialize() : $val; |
149
|
|
|
}; |
150
|
|
|
|
151
|
|
|
if (is_array($val)) { |
152
|
|
|
foreach ($val as $sk => $sv) { |
153
|
65 |
|
$val[$sk] = $fn($sv); |
154
|
|
|
} |
155
|
65 |
|
} |
156
|
|
|
|
157
|
65 |
|
$output->{$name} = $fn($val); |
158
|
65 |
|
} |
159
|
65 |
|
|
160
|
65 |
|
return $output; |
161
|
65 |
|
} |
162
|
|
|
} |
163
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.