|
1 <?php |
|
2 /** |
|
3 * LICENSE |
|
4 * |
|
5 * This source file is subject to the new BSD license that is bundled |
|
6 * with this package in the file LICENSE.txt. |
|
7 * It is also available through the world-wide-web at this URL: |
|
8 * http://framework.zend.com/license/new-bsd |
|
9 * If you did not receive a copy of the license and are unable to |
|
10 * obtain it through the world-wide-web, please send an email |
|
11 * to license@zend.com so we can send you a copy immediately. |
|
12 * |
|
13 * @category Zend |
|
14 * @package Zend_Cloud |
|
15 * @subpackage DocumentService |
|
16 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
17 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
18 */ |
|
19 |
|
20 /** |
|
21 * Class encapsulating documents. Fields are stored in a name/value |
|
22 * array. Data are represented as strings. |
|
23 * |
|
24 * TODO Can fields be large enough to warrant support for streams? |
|
25 * |
|
26 * @category Zend |
|
27 * @package Zend_Cloud |
|
28 * @subpackage DocumentService |
|
29 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
30 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
31 */ |
|
32 class Zend_Cloud_DocumentService_Document |
|
33 implements ArrayAccess, IteratorAggregate, Countable |
|
34 { |
|
35 /** key in document denoting identifier */ |
|
36 const KEY_FIELD = '_id'; |
|
37 |
|
38 /** |
|
39 * ID of this document. |
|
40 * @var mixed |
|
41 */ |
|
42 protected $_id; |
|
43 |
|
44 /** |
|
45 * Name/value array of field names to values. |
|
46 * @var array |
|
47 */ |
|
48 protected $_fields; |
|
49 |
|
50 /** |
|
51 * Construct an instance of Zend_Cloud_DocumentService_Document. |
|
52 * |
|
53 * If no identifier is provided, but a field matching KEY_FIELD is present, |
|
54 * then that field's value will be used as the document identifier. |
|
55 * |
|
56 * @param array $fields |
|
57 * @param mixed $id Document identifier |
|
58 * @return void |
|
59 */ |
|
60 public function __construct($fields, $id = null) |
|
61 { |
|
62 if (!is_array($fields) && !$fields instanceof ArrayAccess) { |
|
63 require_once 'Zend/Cloud/DocumentService/Exception.php'; |
|
64 throw new Zend_Cloud_DocumentService_Exception('Fields must be an array or implement ArrayAccess'); |
|
65 } |
|
66 |
|
67 if (isset($fields[self::KEY_FIELD])) { |
|
68 $id = $fields[self::KEY_FIELD]; |
|
69 unset($fields[self::KEY_FIELD]); |
|
70 } |
|
71 |
|
72 $this->_fields = $fields; |
|
73 $this->setId($id); |
|
74 } |
|
75 |
|
76 /** |
|
77 * Set document identifier |
|
78 * |
|
79 * @param mixed $id |
|
80 * @return Zend_Cloud_DocumentService_Document |
|
81 */ |
|
82 public function setId($id) |
|
83 { |
|
84 $this->_id = $id; |
|
85 return $this; |
|
86 } |
|
87 |
|
88 /** |
|
89 * Get ID name. |
|
90 * |
|
91 * @return string |
|
92 */ |
|
93 public function getId() |
|
94 { |
|
95 return $this->_id; |
|
96 } |
|
97 |
|
98 /** |
|
99 * Get fields as array. |
|
100 * |
|
101 * @return array |
|
102 */ |
|
103 public function getFields() |
|
104 { |
|
105 return $this->_fields; |
|
106 } |
|
107 |
|
108 /** |
|
109 * Get field by name. |
|
110 * |
|
111 * @param string $name |
|
112 * @return mixed |
|
113 */ |
|
114 public function getField($name) |
|
115 { |
|
116 if (isset($this->_fields[$name])) { |
|
117 return $this->_fields[$name]; |
|
118 } |
|
119 return null; |
|
120 } |
|
121 |
|
122 /** |
|
123 * Set field by name. |
|
124 * |
|
125 * @param string $name |
|
126 * @param mixed $value |
|
127 * @return Zend_Cloud_DocumentService_Document |
|
128 */ |
|
129 public function setField($name, $value) |
|
130 { |
|
131 $this->_fields[$name] = $value; |
|
132 return $this; |
|
133 } |
|
134 |
|
135 /** |
|
136 * Overloading: get value |
|
137 * |
|
138 * @param string $name |
|
139 * @return mixed |
|
140 */ |
|
141 public function __get($name) |
|
142 { |
|
143 return $this->getField($name); |
|
144 } |
|
145 |
|
146 /** |
|
147 * Overloading: set field |
|
148 * |
|
149 * @param string $name |
|
150 * @param mixed $value |
|
151 * @return void |
|
152 */ |
|
153 public function __set($name, $value) |
|
154 { |
|
155 $this->setField($name, $value); |
|
156 } |
|
157 |
|
158 /** |
|
159 * ArrayAccess: does field exist? |
|
160 * |
|
161 * @param string $name |
|
162 * @return bool |
|
163 */ |
|
164 public function offsetExists($name) |
|
165 { |
|
166 return isset($this->_fields[$name]); |
|
167 } |
|
168 |
|
169 /** |
|
170 * ArrayAccess: get field by name |
|
171 * |
|
172 * @param string $name |
|
173 * @return mixed |
|
174 */ |
|
175 public function offsetGet($name) |
|
176 { |
|
177 return $this->getField($name); |
|
178 } |
|
179 |
|
180 /** |
|
181 * ArrayAccess: set field to value |
|
182 * |
|
183 * @param string $name |
|
184 * @param mixed $value |
|
185 * @return void |
|
186 */ |
|
187 public function offsetSet($name, $value) |
|
188 { |
|
189 $this->setField($name, $value); |
|
190 } |
|
191 |
|
192 /** |
|
193 * ArrayAccess: remove field from document |
|
194 * |
|
195 * @param string $name |
|
196 * @return void |
|
197 */ |
|
198 public function offsetUnset($name) |
|
199 { |
|
200 if ($this->offsetExists($name)) { |
|
201 unset($this->_fields[$name]); |
|
202 } |
|
203 } |
|
204 |
|
205 /** |
|
206 * Overloading: retrieve and set fields by name |
|
207 * |
|
208 * @param string $name |
|
209 * @param mixed $args |
|
210 * @return mixed |
|
211 */ |
|
212 public function __call($name, $args) |
|
213 { |
|
214 $prefix = substr($name, 0, 3); |
|
215 if ($prefix == 'get') { |
|
216 // Get value |
|
217 $option = substr($name, 3); |
|
218 return $this->getField($option); |
|
219 } elseif ($prefix == 'set') { |
|
220 // set value |
|
221 $option = substr($name, 3); |
|
222 return $this->setField($option, $args[0]); |
|
223 } |
|
224 |
|
225 require_once 'Zend/Cloud/OperationNotAvailableException.php'; |
|
226 throw new Zend_Cloud_OperationNotAvailableException("Unknown operation $name"); |
|
227 } |
|
228 |
|
229 /** |
|
230 * Countable: return count of fields in document |
|
231 * |
|
232 * @return int |
|
233 */ |
|
234 public function count() |
|
235 { |
|
236 return count($this->_fields); |
|
237 } |
|
238 |
|
239 /** |
|
240 * IteratorAggregate: return iterator for iterating over fields |
|
241 * |
|
242 * @return Iterator |
|
243 */ |
|
244 public function getIterator() |
|
245 { |
|
246 return new ArrayIterator($this->_fields); |
|
247 } |
|
248 } |