| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- /**
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Cloud
- * @subpackage DocumentService
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- /**
- * Class encapsulating documents. Fields are stored in a name/value
- * array. Data are represented as strings.
- *
- * TODO Can fields be large enough to warrant support for streams?
- *
- * @category Zend
- * @package Zend_Cloud
- * @subpackage DocumentService
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_Cloud_DocumentService_Document
- implements ArrayAccess, IteratorAggregate, Countable
- {
- /** key in document denoting identifier */
- const KEY_FIELD = '_id';
- /**
- * ID of this document.
- * @var mixed
- */
- protected $_id;
- /**
- * Name/value array of field names to values.
- * @var array
- */
- protected $_fields;
- /**
- * Construct an instance of Zend_Cloud_DocumentService_Document.
- *
- * If no identifier is provided, but a field matching KEY_FIELD is present,
- * then that field's value will be used as the document identifier.
- *
- * @param array $fields
- * @param mixed $id Document identifier
- * @return void
- */
- public function __construct($fields, $id = null)
- {
- if (!is_array($fields) && !$fields instanceof ArrayAccess) {
- require_once 'Zend/Cloud/DocumentService/Exception.php';
- throw new Zend_Cloud_DocumentService_Exception('Fields must be an array or implement ArrayAccess');
- }
- if (isset($fields[self::KEY_FIELD])) {
- $id = $fields[self::KEY_FIELD];
- unset($fields[self::KEY_FIELD]);
- }
- $this->_fields = $fields;
- $this->setId($id);
- }
- /**
- * Set document identifier
- *
- * @param mixed $id
- * @return Zend_Cloud_DocumentService_Document
- */
- public function setId($id)
- {
- $this->_id = $id;
- return $this;
- }
- /**
- * Get ID name.
- *
- * @return string
- */
- public function getId()
- {
- return $this->_id;
- }
- /**
- * Get fields as array.
- *
- * @return array
- */
- public function getFields()
- {
- return $this->_fields;
- }
- /**
- * Get field by name.
- *
- * @param string $name
- * @return mixed
- */
- public function getField($name)
- {
- if (isset($this->_fields[$name])) {
- return $this->_fields[$name];
- }
- return null;
- }
- /**
- * Set field by name.
- *
- * @param string $name
- * @param mixed $value
- * @return Zend_Cloud_DocumentService_Document
- */
- public function setField($name, $value)
- {
- $this->_fields[$name] = $value;
- return $this;
- }
- /**
- * Overloading: get value
- *
- * @param string $name
- * @return mixed
- */
- public function __get($name)
- {
- return $this->getField($name);
- }
- /**
- * Overloading: set field
- *
- * @param string $name
- * @param mixed $value
- * @return void
- */
- public function __set($name, $value)
- {
- $this->setField($name, $value);
- }
- /**
- * ArrayAccess: does field exist?
- *
- * @param string $name
- * @return bool
- */
- public function offsetExists($name)
- {
- return isset($this->_fields[$name]);
- }
- /**
- * ArrayAccess: get field by name
- *
- * @param string $name
- * @return mixed
- */
- public function offsetGet($name)
- {
- return $this->getField($name);
- }
- /**
- * ArrayAccess: set field to value
- *
- * @param string $name
- * @param mixed $value
- * @return void
- */
- public function offsetSet($name, $value)
- {
- $this->setField($name, $value);
- }
- /**
- * ArrayAccess: remove field from document
- *
- * @param string $name
- * @return void
- */
- public function offsetUnset($name)
- {
- if ($this->offsetExists($name)) {
- unset($this->_fields[$name]);
- }
- }
- /**
- * Overloading: retrieve and set fields by name
- *
- * @param string $name
- * @param mixed $args
- * @return mixed
- */
- public function __call($name, $args)
- {
- $prefix = substr($name, 0, 3);
- if ($prefix == 'get') {
- // Get value
- $option = substr($name, 3);
- return $this->getField($option);
- } elseif ($prefix == 'set') {
- // set value
- $option = substr($name, 3);
- return $this->setField($option, $args[0]);
- }
- require_once 'Zend/Cloud/OperationNotAvailableException.php';
- throw new Zend_Cloud_OperationNotAvailableException("Unknown operation $name");
- }
- /**
- * Countable: return count of fields in document
- *
- * @return int
- */
- public function count()
- {
- return count($this->_fields);
- }
- /**
- * IteratorAggregate: return iterator for iterating over fields
- *
- * @return Iterator
- */
- public function getIterator()
- {
- return new ArrayIterator($this->_fields);
- }
- }
|