1: <?php
2: /**
3: * A metadata object, used by other components in Compute and Object Storage
4: *
5: * @copyright 2012-2013 Rackspace Hosting, Inc.
6: * See COPYING for licensing information
7: *
8: * @package phpOpenCloud
9: * @version 1.0
10: * @author Glen Campbell <glen.campbell@rackspace.com>
11: */
12:
13: namespace OpenCloud\Common;
14:
15: /**
16: * The Metadata class represents either Server or Image metadata
17: *
18: * @api
19: * @author Glen Campbell <glen.campbell@rackspace.com>
20: */
21: class Metadata extends Base
22: {
23:
24: // array holding the names of keys that were set
25: private $_keylist = array();
26:
27: /**
28: * This setter overrides the base one, since the metadata key can be
29: * anything
30: *
31: * @param string $key
32: * @param string $value
33: * @return void
34: */
35: public function __set($key, $value)
36: {
37: // set the value and track the keys
38: if (!in_array($key, $this->_keylist)) {
39: $this->_keylist[] = $key;
40: }
41:
42: $this->$key = $value;
43: }
44:
45: /**
46: * Returns the list of keys defined
47: *
48: * @return array
49: */
50: public function Keylist()
51: {
52: return $this->_keylist;
53: }
54:
55: /**
56: * Sets metadata values from an array, with optional prefix
57: *
58: * If $prefix is provided, then only array keys that match the prefix
59: * are set as metadata values, and $prefix is stripped from the key name.
60: *
61: * @param array $values an array of key/value pairs to set
62: * @param string $prefix if provided, a prefix that is used to identify
63: * metadata values. For example, you can set values from headers
64: * for a Container by using $prefix='X-Container-Meta-'.
65: * @return void
66: */
67: public function setArray($values, $prefix = null)
68: {
69: if (empty($values)) {
70: return false;
71: }
72:
73: foreach ($values as $key => $value) {
74: if ($prefix) {
75: if (strpos($key, $prefix) === 0) {
76: $name = substr($key, strlen($prefix));
77: $this->getLogger()->info(
78: Lang::translate('Setting [{name}] to [{value}]'),
79: array(
80: 'name' => $name,
81: 'value' => $value
82: )
83: );
84: $this->$name = $value;
85: }
86: } else {
87: $this->$key = $value;
88: }
89: }
90: }
91:
92: }
93: