1: <?php
2: /**
3: * Copyright 2012-2014 Rackspace US, Inc.
4: *
5: * Licensed under the Apache License, Version 2.0 (the "License");
6: * you may not use this file except in compliance with the License.
7: * You may obtain a copy of the License at
8: *
9: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: namespace OpenCloud\Common;
19:
20: /**
21: * The Metadata class represents either Server or Image metadata
22: */
23: class Metadata extends Base implements \Countable
24: {
25: /**
26: * @var array Internal data store.
27: */
28: protected $metadata = array();
29:
30: /**
31: * This setter overrides the base one, since the metadata key can be
32: * anything
33: *
34: * @param string $property
35: * @param string $value
36: * @return void
37: */
38: public function __set($property, $value)
39: {
40: return $this->setProperty($property, $value);
41: }
42:
43: public function __get($key)
44: {
45: return $this->getProperty($key);
46: }
47:
48: public function propertyExists($property, $allowRetry = true)
49: {
50: return isset($this->metadata[strtolower($property)])
51: || parent::propertyExists($property, $allowRetry);
52: }
53:
54: public function getProperty($property)
55: {
56: return $this->propertyExists($property) ? $this->metadata[strtolower($property)] : null;
57: }
58:
59: public function setProperty($property, $value)
60: {
61: $this->metadata[strtolower($property)] = $value;
62: }
63:
64: public function __isset($property)
65: {
66: return $this->propertyExists($property);
67: }
68:
69: /**
70: * Returns the list of keys defined
71: *
72: * @return array
73: */
74: public function keylist()
75: {
76: return $this->metadata;
77: }
78:
79: /**
80: * Sets metadata values from an array, with optional prefix
81: *
82: * If $prefix is provided, then only array keys that match the prefix
83: * are set as metadata values, and $prefix is stripped from the key name.
84: *
85: * @param array $values an array of key/value pairs to set
86: * @param string $prefix if provided, a prefix that is used to identify
87: * metadata values. For example, you can set values from headers
88: * for a Container by using $prefix='X-Container-Meta-'.
89: * @return void
90: */
91: public function setArray($values, $prefix = null)
92: {
93: if (empty($values)) {
94: return false;
95: }
96:
97: foreach ($values as $key => $value) {
98: if ($prefix && strpos($key, $prefix) === 0) {
99: $key = substr($key, strlen($prefix));
100: }
101: $this->setProperty($key, $value);
102: }
103: }
104:
105: public function toArray()
106: {
107: return $this->metadata;
108: }
109:
110: public function count()
111: {
112: return count($this->metadata);
113: }
114: }
115: