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:
55: if (!($property = $schema->getProperty($propertyName))) {
56:
57: if (false === ($property = $schema->validateAdditionalProperty($value))) {
58: throw new \RuntimeException(
59: 'If a property does not exist in the schema, the `additionalProperties` property must be set'
60: );
61: }
62: }
63:
64:
65: $property->setName($propertyName);
66: $property->setValue($value);
67: $property->validate();
68:
69:
70: if (!$value) {
71: $operationType = OperationType::REMOVE;
72: } elseif ($this->offsetExists($propertyName)) {
73: $operationType = OperationType::REPLACE;
74: } else {
75: $operationType = $schema->decideOperationType($property);
76: }
77:
78:
79: $operation = JsonOperation::factory($schema, $property, $operationType);
80:
81:
82: $document->addOperation($operation);
83: }
84:
85:
86: $body = $document->toString();
87:
88: return $this->getClient()
89: ->patch($this->getUrl(), $this->getService()->getPatchHeaders(), $body)
90: ->send();
91: }
92:
93: 94: 95: 96: 97:
98: public function refresh()
99: {
100: $response = $this->getClient()->get($this->getUrl())->send();
101:
102: $this->setData($response->json());
103:
104: return $response;
105: }
106:
107: 108: 109: 110: 111:
112: public function delete()
113: {
114: return $this->getClient()->delete($this->getUrl())->send();
115: }
116:
117: 118: 119: 120: 121: 122:
123: public function listMembers(array $params = array())
124: {
125: $url = clone $this->getUrl();
126: $url->addPath(Member::resourceName())->setQuery($params);
127:
128: return $this->getService()->resourceList('Member', $url, $this);
129: }
130:
131: 132: 133: 134: 135: 136:
137: public function member($data)
138: {
139: $data = (array) $data;
140:
141: $member = $this->getService()->resource('Member', null, $this);
142: $member->setData($data);
143:
144: if (isset($data['member_id'])) {
145: $member->setId($data['member_id']);
146: }
147:
148: return $member;
149: }
150:
151: 152: 153: 154: 155: 156:
157: public function getMember($memberId)
158: {
159: $url = clone $this->getUrl();
160: $url->addPath('members');
161: $url->addPath((string) $memberId);
162:
163: $data = $this->getClient()->get($url)->send()->json();
164:
165: return $this->member($data);
166: }
167:
168: 169: 170: 171: 172: 173:
174: public function createMember($tenantId)
175: {
176: $url = $this->getUrl();
177: $url->addPath('members');
178:
179: $json = json_encode(array('member' => $tenantId));
180: return $this->getClient()->post($url, self::getJsonHeader(), $json)->send();
181: }
182:
183: 184: 185: 186: 187: 188:
189: public function deleteMember($tenantId)
190: {
191: $url = $this->getUrl();
192: $url->addPath('members');
193: $url->addPath((string)$tenantId);
194:
195: return $this->getClient()->delete($url)->send();
196: }
197:
198: 199: 200: 201: 202: 203:
204: public function addTag($tag)
205: {
206: $url = clone $this->getUrl();
207: $url->addPath('tags')->addPath((string) $tag);
208:
209: return $this->getClient()->put($url)->send();
210: }
211:
212: 213: 214: 215: 216: 217:
218: public function deleteTag($tag)
219: {
220: $url = clone $this->getUrl();
221: $url->addPath('tags')->addPath((string) $tag);
222:
223: return $this->getClient()->delete($url)->send();
224: }
225: }
226: