1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Compute\v2\Models; |
4
|
|
|
|
5
|
|
|
use OpenStack\Common\Resource\Alias; |
6
|
|
|
use OpenStack\Common\Resource\OperatorResource; |
7
|
|
|
use OpenStack\Common\Resource\Deletable; |
8
|
|
|
use OpenStack\Common\Resource\HasMetadata; |
9
|
|
|
use OpenStack\Common\Resource\Listable; |
10
|
|
|
use OpenStack\Common\Resource\Retrievable; |
11
|
|
|
use OpenStack\Common\Transport\Utils; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Represents a Compute v2 Image |
16
|
|
|
* |
17
|
|
|
* @property \OpenStack\Compute\v2\Api $api |
18
|
|
|
*/ |
19
|
|
|
class Image extends OperatorResource implements Listable, Retrievable, Deletable, HasMetadata |
20
|
|
|
{ |
21
|
|
|
/** @var string */ |
22
|
|
|
public $id; |
23
|
|
|
|
24
|
|
|
/** @var array */ |
25
|
|
|
public $links; |
26
|
|
|
|
27
|
|
|
/** @var array */ |
28
|
|
|
public $metadata; |
29
|
|
|
|
30
|
|
|
/** @var int */ |
31
|
|
|
public $minDisk; |
32
|
|
|
|
33
|
|
|
/** @var int */ |
34
|
|
|
public $minRam; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
public $name; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
public $progress; |
41
|
|
|
|
42
|
|
|
/** @var string */ |
43
|
|
|
public $status; |
44
|
|
|
|
45
|
|
|
/** @var \DateTimeImmutable */ |
46
|
|
|
public $created; |
47
|
|
|
|
48
|
|
|
/** @var \DateTimeImmutable */ |
49
|
|
|
public $updated; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
1 |
|
protected $aliases = []; |
55
|
|
|
|
56
|
1 |
|
protected $resourceKey = 'image'; |
57
|
1 |
|
protected $resourcesKey = 'images'; |
58
|
1 |
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
|
protected function getAliases() |
63
|
1 |
|
{ |
64
|
|
|
$aliases = parent::getAliases(); |
65
|
1 |
|
$aliases['created'] = new Alias('created', \DateTimeImmutable::class); |
66
|
1 |
|
$aliases['updated'] = new Alias('updated', \DateTimeImmutable::class); |
67
|
|
|
return $aliases; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
*/ |
73
|
1 |
|
public function retrieve() |
74
|
|
|
{ |
75
|
1 |
|
$response = $this->execute($this->api->getImage(), ['id' => (string)$this->id]); |
76
|
1 |
|
$this->populateFromResponse($response); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritDoc} |
81
|
|
|
*/ |
82
|
|
|
public function delete() |
83
|
|
|
{ |
84
|
|
|
$this->execute($this->api->deleteImage(), ['id' => (string)$this->id]); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
/** |
88
|
|
|
* Retrieves metadata from the API. |
89
|
1 |
|
* |
90
|
1 |
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function getMetadata(): array |
93
|
|
|
{ |
94
|
|
|
$response = $this->execute($this->api->getImageMetadata(), ['id' => $this->id]); |
95
|
|
|
return $this->parseMetadata($response); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Resets all the metadata for this image with the values provided. All existing metadata keys |
100
|
|
|
* will either be replaced or removed. |
101
|
|
|
* |
102
|
1 |
|
* @param array $metadata {@see \OpenStack\Compute\v2\Api::putImageMetadata} |
103
|
|
|
*/ |
104
|
1 |
|
public function resetMetadata(array $metadata) |
105
|
1 |
|
{ |
106
|
|
|
$response = $this->execute($this->api->putImageMetadata(), ['id' => $this->id, 'metadata' => $metadata]); |
107
|
|
|
$this->metadata = $this->parseMetadata($response); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Merges the existing metadata for the image with the values provided. Any existing keys |
112
|
|
|
* referenced in the user options will be replaced with the user's new values. All other |
113
|
|
|
* existing keys will remain unaffected. |
114
|
|
|
* |
115
|
1 |
|
* @param array $metadata {@see \OpenStack\Compute\v2\Api::postImageMetadata} |
116
|
|
|
*/ |
117
|
1 |
|
public function mergeMetadata(array $metadata) |
118
|
1 |
|
{ |
119
|
|
|
$response = $this->execute($this->api->postImageMetadata(), ['id' => $this->id, 'metadata' => $metadata]); |
120
|
|
|
$this->metadata = $this->parseMetadata($response); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Retrieve the value for a specific metadata key. |
125
|
|
|
* |
126
|
1 |
|
* @param string $key {@see \OpenStack\Compute\v2\Api::getImageMetadataKey} |
127
|
|
|
* |
128
|
1 |
|
* @return mixed |
129
|
1 |
|
*/ |
130
|
|
View Code Duplication |
public function getMetadataItem(string $key) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$response = $this->execute($this->api->getImageMetadataKey(), ['id' => $this->id, 'key' => $key]); |
133
|
|
|
$value = $this->parseMetadata($response)[$key]; |
134
|
|
|
$this->metadata[$key] = $value; |
135
|
|
|
return $value; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Remove a specific metadata key. |
140
|
|
|
* |
141
|
|
|
* @param string $key {@see \OpenStack\Compute\v2\Api::deleteImageMetadataKey} |
142
|
|
|
*/ |
143
|
|
View Code Duplication |
public function deleteMetadataItem(string $key) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
if (isset($this->metadata[$key])) { |
146
|
|
|
unset($this->metadata[$key]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$this->execute($this->api->deleteImageMetadataKey(), ['id' => $this->id, 'key' => $key]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function parseMetadata(ResponseInterface $response): array |
153
|
|
|
{ |
154
|
|
|
return Utils::jsonDecode($response)['metadata']; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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.