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 ($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 ($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 (array $headers)
128: {
129: $output = array();
130: foreach ($headers as $header => $value) {
131: $prefix = self::GLOBAL_METADATA_PREFIX . '-' . static::METADATA_LABEL . '-Meta-';
132: $output[$prefix . $header] = $value;
133: }
134:
135: return $output;
136: }
137:
138: 139: 140: 141: 142: 143: 144:
145: public function setMetadata($data, $constructFromResponse = false)
146: {
147: if ($constructFromResponse) {
148: $metadata = new $this->metadataClass;
149: $metadata->setArray(self::trimHeaders($data));
150: $data = $metadata;
151: }
152:
153: $this->metadata = $data;
154:
155: return $this;
156: }
157:
158: 159: 160:
161: public function getMetadata()
162: {
163: return $this->metadata;
164: }
165:
166: 167: 168: 169: 170: 171: 172: 173:
174: public function saveMetadata(array $metadata, $stockPrefix = true)
175: {
176: $headers = ($stockPrefix === true) ? self::stockHeaders($metadata) : $metadata;
177:
178: return $this->getClient()->post($this->getUrl(), $headers)->send();
179: }
180:
181: 182: 183: 184: 185:
186: public function retrieveMetadata()
187: {
188: $response = $this->getClient()
189: ->head($this->getUrl())
190: ->send();
191:
192: $this->setMetadata($response->getHeaders(), true);
193:
194: return $this->metadata;
195: }
196:
197: 198: 199: 200: 201: 202:
203: public function unsetMetadataItem($key)
204: {
205: $header = sprintf('%s-Remove-%s-Meta-%s', self::GLOBAL_METADATA_PREFIX,
206: static::METADATA_LABEL, $key);
207:
208: $headers = array($header => 'True');
209:
210: return $this->getClient()
211: ->post($this->getUrl(), $headers)
212: ->send();
213: }
214:
215: 216: 217: 218: 219: 220:
221: public function appendToMetadata(array $values)
222: {
223: return (!empty($this->metadata) && is_array($this->metadata))
224: ? array_merge($this->metadata, $values)
225: : $values;
226: }
227: }
228: