1: <?php
2: /**
3: * PHP OpenCloud library.
4: *
5: * @copyright Copyright 2013 Rackspace US, Inc. See COPYING for licensing information.
6: * @license https://www.apache.org/licenses/LICENSE-2.0 Apache 2.0
7: * @version 1.6.0
8: * @author Glen Campbell <glen.campbell@rackspace.com>
9: * @author Jamie Hannaford <jamie.hannaford@rackspace.com>
10: */
11:
12: namespace OpenCloud\ObjectStore\Resource;
13:
14: use OpenCloud\Common\Base;
15: use OpenCloud\Common\Metadata;
16: use OpenCloud\Common\Exceptions\NameError;
17: use OpenCloud\Common\Exceptions\MetadataPrefixError;
18: use OpenCloud\Common\Request\Response\Http;
19:
20: /**
21: * Abstract base class which implements shared functionality of ObjectStore
22: * resources. Provides support, for example, for metadata-handling and other
23: * features that are common to the ObjectStore components.
24: */
25: abstract class AbstractStorageObject extends Base
26: {
27:
28: const ACCOUNT_META_PREFIX = 'X-Account-';
29: const CONTAINER_META_PREFIX = 'X-Container-Meta-';
30: const OBJECT_META_PREFIX = 'X-Object-Meta-';
31: const CDNCONTAINER_META_PREFIX = 'X-Cdn-';
32:
33: /**
34: * Metadata belonging to a resource.
35: *
36: * @var OpenCloud\Common\Metadata
37: */
38: public $metadata;
39:
40: /**
41: * Initializes the metadata component
42: */
43: public function __construct()
44: {
45: $this->metadata = new Metadata;
46: }
47:
48: /**
49: * Given an Http response object, converts the appropriate headers
50: * to metadata
51: *
52: * @param OpenCloud\Common\Request\Response\Http
53: * @return void
54: */
55: public function getMetadata(Http $response)
56: {
57: $this->metadata = new Metadata;
58: $this->metadata->setArray($response->headers(), $this->prefix());
59: }
60:
61: /**
62: * If object has metadata, return an associative array of headers.
63: *
64: * For example, if a DataObject has a metadata item named 'FOO',
65: * then this would return array('X-Object-Meta-FOO'=>$value);
66: *
67: * @return array
68: */
69: public function metadataHeaders()
70: {
71: $headers = array();
72:
73: // only build if we have metadata
74: if (is_object($this->metadata)) {
75: foreach ($this->metadata as $key => $value) {
76: $headers[$this->prefix() . $key] = $value;
77: }
78: }
79:
80: return $headers;
81: }
82:
83: /**
84: * Returns the displayable name of the object
85: *
86: * Can be overridden by child objects; *must* be overridden by child
87: * objects if the object does not have a `name` attribute defined.
88: *
89: * @api
90: * @throws NameError if attribute 'name' is not defined
91: */
92: public function name()
93: {
94: if (property_exists($this, 'name')) {
95: return $this->name;
96: } else {
97: throw new NameError(sprintf(
98: 'Name attribute does not exist for [%s]',
99: get_class($this)
100: ));
101: }
102: }
103:
104: /**
105: * Override parent method.
106: *
107: * @return null
108: */
109: public static function jsonName()
110: {
111: return null;
112: }
113:
114: /**
115: * Override parent method.
116: *
117: * @return null
118: */
119: public static function jsonCollectionName()
120: {
121: return null;
122: }
123:
124: /**
125: * Override parent method.
126: *
127: * @return null
128: */
129: public static function jsonCollectionElement()
130: {
131: return null;
132: }
133:
134: /**
135: * Returns the proper prefix for the specified type of object
136: *
137: * @param string $type The type of object; derived from `get_class()` if not
138: * specified.
139: * @codeCoverageIgnore
140: */
141: private function prefix($type = null)
142: {
143: if ($type === null) {
144: $parts = preg_split('/\\\/', get_class($this));
145: $type = $parts[count($parts)-1];
146: }
147:
148: switch($type) {
149: case 'Account':
150: $prefix = self::ACCOUNT_META_PREFIX;
151: break;
152: case 'CDNContainer':
153: $prefix = self::CDNCONTAINER_META_PREFIX;
154: break;
155: case 'Container':
156: $prefix = self::CONTAINER_META_PREFIX;
157: break;
158: case 'DataObject':
159: $prefix = self::OBJECT_META_PREFIX;
160: break;
161: default:
162: throw new MetadataPrefixError(sprintf(
163: 'Unrecognized metadata type [%s]',
164: $type
165: ));
166: }
167:
168: return $prefix;
169: }
170: }
171: