1: <?php
2: /**
3: * PHP OpenCloud library.
4: *
5: * @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
6: * @license https://www.apache.org/licenses/LICENSE-2.0
7: * @author Glen Campbell <glen.campbell@rackspace.com>
8: * @author Jamie Hannaford <jamie.hannaford@rackspace.com>
9: */
10:
11: namespace OpenCloud\Common;
12:
13: /**
14: * The Metadata class represents either Server or Image metadata
15: *
16: * @api
17: * @author Glen Campbell <glen.campbell@rackspace.com>
18: */
19: class Metadata extends Base
20: {
21:
22: // array holding the names of keys that were set
23: protected $metadata = array();
24:
25: /**
26: * This setter overrides the base one, since the metadata key can be
27: * anything
28: *
29: * @param string $key
30: * @param string $value
31: * @return void
32: */
33: public function __set($property, $value)
34: {
35: return $this->setProperty($property, $value);
36: }
37:
38: public function __get($key)
39: {
40: return $this->getProperty($key);
41: }
42:
43: public function propertyExists($property, $allowRetry = true)
44: {
45: return isset($this->metadata[strtolower($property)])
46: || parent::propertyExists($property, $allowRetry);
47: }
48:
49: public function getProperty($property)
50: {
51: return $this->propertyExists($property) ? $this->metadata[strtolower($property)] : null;
52: }
53:
54: public function setProperty($property, $value)
55: {
56: $this->metadata[strtolower($property)] = $value;
57: }
58:
59: public function __isset($property)
60: {
61: return $this->propertyExists($property);
62: }
63:
64: /**
65: * Returns the list of keys defined
66: *
67: * @return array
68: */
69: public function keylist()
70: {
71: return $this->metadata;
72: }
73:
74: /**
75: * Sets metadata values from an array, with optional prefix
76: *
77: * If $prefix is provided, then only array keys that match the prefix
78: * are set as metadata values, and $prefix is stripped from the key name.
79: *
80: * @param array $values an array of key/value pairs to set
81: * @param string $prefix if provided, a prefix that is used to identify
82: * metadata values. For example, you can set values from headers
83: * for a Container by using $prefix='X-Container-Meta-'.
84: * @return void
85: */
86: public function setArray($values, $prefix = null)
87: {
88: if (empty($values)) {
89: return false;
90: }
91:
92: foreach ($values as $key => $value) {
93: if ($prefix && strpos($key, $prefix) === 0) {
94: $key = substr($key, strlen($prefix));
95: }
96: $this->setProperty($key, $value);
97: }
98: }
99:
100: public function toArray()
101: {
102: return $this->metadata;
103: }
104:
105: public function count()
106: {
107: return count($this->metadata);
108: }
109:
110: }
111: