Document.php 5.7 KB

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