1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17
18 namespace OpenCloud\Image\Resource;
19
20 use OpenCloud\Image\Enum\OperationType;
21 use OpenCloud\Image\Resource\JsonPatch\Document as JsonDocument;
22 use OpenCloud\Image\Resource\JsonPatch\Operation as JsonOperation;
23 use OpenCloud\Image\Resource\Schema\Schema;
24
25 26 27 28 29 30 31
32 class Image extends AbstractSchemaResource implements ImageInterface
33 {
34 protected static $url_resource = 'images';
35 protected static $json_name = '';
36 protected static $json_collection_name = 'images';
37
38 39 40 41 42 43 44 45
46 public function update(array $params, Schema $schema = null)
47 {
48 $schema = $schema ?: $this->getService()->getImageSchema();
49
50 $document = new JsonDocument();
51
52 foreach ($params as $propertyName => $value) {
53
54 if (!($property = $schema->getProperty($propertyName))) {
55
56 if (false === ($property = $schema->validateAdditionalProperty($value))) {
57 throw new \RuntimeException(
58 'If a property does not exist in the schema, the `additionalProperties` property must be set'
59 );
60 }
61 }
62
63
64 $property->setName($propertyName);
65 $property->setValue($value);
66 $property->validate();
67
68
69 if (!$value) {
70 $operationType = OperationType::REMOVE;
71 } elseif ($this->offsetExists($propertyName)) {
72 $operationType = OperationType::REPLACE;
73 } else {
74 $operationType = $schema->decideOperationType($property);
75 }
76
77
78 $operation = JsonOperation::factory($schema, $property, $operationType);
79
80
81 $document->addOperation($operation);
82 }
83
84
85 $body = $document->toString();
86
87 return $this->getClient()
88 ->patch($this->getUrl(), $this->getService()->getPatchHeaders(), $body)
89 ->send();
90 }
91
92 93 94 95 96
97 public function refresh()
98 {
99 $response = $this->getClient()->get($this->getUrl())->send();
100
101 $this->setData($response->json());
102
103 return $response;
104 }
105
106 107 108 109 110
111 public function delete()
112 {
113 return $this->getClient()->delete($this->getUrl())->send();
114 }
115
116 117 118 119 120 121
122 public function listMembers(array $params = array())
123 {
124 $url = clone $this->getUrl();
125 $url->addPath(Member::resourceName())->setQuery($params);
126
127 return $this->getService()->resourceList('Member', $url, $this);
128 }
129
130 131 132 133 134 135
136 public function member($data)
137 {
138 $data = (array) $data;
139
140 $member = $this->getService()->resource('Member', null, $this);
141 $member->setData($data);
142
143 if (isset($data['member_id'])) {
144 $member->setId($data['member_id']);
145 }
146
147 return $member;
148 }
149
150 151 152 153 154 155
156 public function getMember($memberId)
157 {
158 $url = clone $this->getUrl();
159 $url->addPath('members');
160 $url->addPath((string) $memberId);
161
162 $data = $this->getClient()->get($url)->send()->json();
163
164 return $this->member($data);
165 }
166
167 168 169 170 171 172
173 public function createMember($tenantId)
174 {
175 $url = $this->getUrl();
176 $url->addPath('members');
177
178 $json = json_encode(array('member' => $tenantId));
179 return $this->getClient()->post($url, self::getJsonHeader(), $json)->send();
180 }
181
182 183 184 185 186 187
188 public function deleteMember($tenantId)
189 {
190 $url = $this->getUrl();
191 $url->addPath('members');
192 $url->addPath((string)$tenantId);
193
194 return $this->getClient()->delete($url)->send();
195 }
196
197 198 199 200 201 202
203 public function addTag($tag)
204 {
205 $url = clone $this->getUrl();
206 $url->addPath('tags')->addPath((string) $tag);
207
208 return $this->getClient()->put($url)->send();
209 }
210
211 212 213 214 215 216
217 public function deleteTag($tag)
218 {
219 $url = clone $this->getUrl();
220 $url->addPath('tags')->addPath((string) $tag);
221
222 return $this->getClient()->delete($url)->send();
223 }
224 }
225