1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17
18 namespace OpenCloud\ObjectStore\Resource;
19
20 use Guzzle\Http\Message\Response;
21 use OpenCloud\Common\Base;
22 use OpenCloud\Common\Service\ServiceInterface;
23
24 25 26 27 28
29 abstract class AbstractResource extends Base
30 {
31 const GLOBAL_METADATA_PREFIX = 'X';
32
33
34 protected $metadata;
35
36
37 protected $metadataClass = 'OpenCloud\\Common\\Metadata';
38
39
40 protected $service;
41
42 public function __construct(ServiceInterface $service)
43 {
44 $this->service = $service;
45 $this->metadata = new $this->metadataClass;
46 }
47
48 public function getService()
49 {
50 return $this->service;
51 }
52
53 public function getCdnService()
54 {
55 return $this->service->getCDNService();
56 }
57
58 public function getClient()
59 {
60 return $this->service->getClient();
61 }
62
63 64 65 66 67 68 69
70 public static function fromResponse(Response $response, ServiceInterface $service)
71 {
72 $object = new static($service);
73
74 if (null !== ($headers = $response->getHeaders())) {
75 $object->setMetadata($headers, true);
76 }
77
78 return $object;
79 }
80
81 82 83 84 85 86
87 public static function trimHeaders($headers)
88 {
89 $output = array();
90
91 foreach ($headers as $header => $value) {
92
93 if (static::headerIsValidMetadata($header) && ($key = self::stripPrefix($header))) {
94 $output[$key] = (string) $value;
95 }
96 }
97
98 return $output;
99 }
100
101 protected static function headerIsValidMetadata($header)
102 {
103 $pattern = sprintf('#^%s\-#i', self::GLOBAL_METADATA_PREFIX);
104
105 return preg_match($pattern, $header);
106 }
107
108 109 110 111 112 113
114 protected static function stripPrefix($header)
115 {
116 $pattern = '#^' . self::GLOBAL_METADATA_PREFIX . '\-(' . static::METADATA_LABEL . '-)?(Meta-)?#i';
117
118 return preg_replace($pattern, '', $header);
119 }
120
121 122 123 124 125 126
127 public static function stockHeaders(array $headers)
128 {
129 $output = array();
130 $prefix = null;
131 $corsHeaders = array(
132 'Access-Control-Allow-Origin',
133 'Access-Control-Expose-Headers',
134 'Access-Control-Max-Age',
135 'Access-Control-Allow-Credentials',
136 'Access-Control-Allow-Methods',
137 'Access-Control-Allow-Headers'
138 );
139 foreach ($headers as $header => $value) {
140 if (!in_array($header, $corsHeaders)) {
141 $prefix = self::GLOBAL_METADATA_PREFIX . '-' . static::METADATA_LABEL . '-Meta-';
142 }
143 $output[$prefix . $header] = $value;
144 }
145
146 return $output;
147 }
148
149 150 151 152 153 154 155
156 public function setMetadata($data, $constructFromResponse = false)
157 {
158 if ($constructFromResponse) {
159 $metadata = new $this->metadataClass;
160 $metadata->setArray(self::trimHeaders($data));
161 $data = $metadata;
162 }
163
164 $this->metadata = $data;
165
166 return $this;
167 }
168
169 170 171
172 public function getMetadata()
173 {
174 return $this->metadata;
175 }
176
177 178 179 180 181 182 183 184
185 public function saveMetadata(array $metadata, $stockPrefix = true)
186 {
187 $headers = ($stockPrefix === true) ? self::stockHeaders($metadata) : $metadata;
188
189 return $this->getClient()->post($this->getUrl(), $headers)->send();
190 }
191
192 193 194 195 196
197 public function retrieveMetadata()
198 {
199 $response = $this->getClient()
200 ->head($this->getUrl())
201 ->send();
202
203 $this->setMetadata($response->getHeaders(), true);
204
205 return $this->metadata;
206 }
207
208 209 210 211 212 213
214 public function unsetMetadataItem($key)
215 {
216 $header = sprintf('%s-Remove-%s-Meta-%s', self::GLOBAL_METADATA_PREFIX,
217 static::METADATA_LABEL, $key);
218
219 $headers = array($header => 'True');
220
221 return $this->getClient()
222 ->post($this->getUrl(), $headers)
223 ->send();
224 }
225
226 227 228 229 230 231
232 public function appendToMetadata(array $values)
233 {
234 return (!empty($this->metadata) && is_array($this->metadata))
235 ? array_merge($this->metadata, $values)
236 : $values;
237 }
238 }
239