File indexing completed on 2024-06-16 05:29:54

0001 <?php
0002 /**
0003  * LICENSE
0004  *
0005  * This source file is subject to the new BSD license that is bundled
0006  * with this package in the file LICENSE.txt.
0007  * It is also available through the world-wide-web at this URL:
0008  * http://framework.zend.com/license/new-bsd
0009  * If you did not receive a copy of the license and are unable to
0010  * obtain it through the world-wide-web, please send an email
0011  * to license@zend.com so we can send you a copy immediately.
0012  *
0013  * @category   Zend
0014  * @package    Zend_Cloud
0015  * @subpackage DocumentService
0016  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0017  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0018  */
0019 
0020 /**
0021  * Class encapsulating documents. Fields are stored in a name/value
0022  * array. Data are represented as strings.
0023  *
0024  * TODO Can fields be large enough to warrant support for streams?
0025  *
0026  * @category   Zend
0027  * @package    Zend_Cloud
0028  * @subpackage DocumentService
0029  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0030  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0031  */
0032 class Zend_Cloud_DocumentService_Document
0033     implements ArrayAccess, IteratorAggregate, Countable
0034 {
0035     /** key in document denoting identifier */
0036     const KEY_FIELD = '_id';
0037 
0038     /**
0039      * ID of this document.
0040      * @var mixed
0041      */
0042     protected $_id;
0043 
0044     /**
0045      * Name/value array of field names to values.
0046      * @var array
0047      */
0048     protected $_fields;
0049 
0050     /**
0051      * Construct an instance of Zend_Cloud_DocumentService_Document.
0052      *
0053      * If no identifier is provided, but a field matching KEY_FIELD is present,
0054      * then that field's value will be used as the document identifier.
0055      *
0056      * @param  array $fields
0057      * @param  mixed $id Document identifier
0058      * @return void
0059      */
0060     public function __construct($fields, $id = null)
0061     {
0062         if (!is_array($fields) && !$fields instanceof ArrayAccess) {
0063             // require_once 'Zend/Cloud/DocumentService/Exception.php';
0064             throw new Zend_Cloud_DocumentService_Exception('Fields must be an array or implement ArrayAccess');
0065         }
0066 
0067         if (isset($fields[self::KEY_FIELD])) {
0068             $id = $fields[self::KEY_FIELD];
0069             unset($fields[self::KEY_FIELD]);
0070         }
0071 
0072         $this->_fields = $fields;
0073         $this->setId($id);
0074     }
0075 
0076     /**
0077      * Set document identifier
0078      *
0079      * @param  mixed $id
0080      * @return Zend_Cloud_DocumentService_Document
0081      */
0082     public function setId($id)
0083     {
0084         $this->_id = $id;
0085         return $this;
0086     }
0087 
0088     /**
0089      * Get ID name.
0090      *
0091      * @return string
0092      */
0093     public function getId()
0094     {
0095         return $this->_id;
0096     }
0097 
0098     /**
0099      * Get fields as array.
0100      *
0101      * @return array
0102      */
0103     public function getFields()
0104     {
0105         return $this->_fields;
0106     }
0107 
0108     /**
0109      * Get field by name.
0110      *
0111      * @param  string $name
0112      * @return mixed
0113      */
0114     public function getField($name)
0115     {
0116         if (isset($this->_fields[$name])) {
0117             return $this->_fields[$name];
0118         }
0119         return null;
0120     }
0121 
0122     /**
0123      * Set field by name.
0124      *
0125      * @param  string $name
0126      * @param  mixed $value
0127      * @return Zend_Cloud_DocumentService_Document
0128      */
0129     public function setField($name, $value)
0130     {
0131         $this->_fields[$name] = $value;
0132         return $this;
0133     }
0134 
0135     /**
0136      * Overloading: get value
0137      *
0138      * @param  string $name
0139      * @return mixed
0140      */
0141     public function __get($name)
0142     {
0143         return $this->getField($name);
0144     }
0145 
0146     /**
0147      * Overloading: set field
0148      *
0149      * @param  string $name
0150      * @param  mixed $value
0151      * @return void
0152      */
0153     public function __set($name, $value)
0154     {
0155         $this->setField($name, $value);
0156     }
0157 
0158     /**
0159      * ArrayAccess: does field exist?
0160      *
0161      * @param  string $name
0162      * @return bool
0163      */
0164     public function offsetExists($name)
0165     {
0166         return isset($this->_fields[$name]);
0167     }
0168 
0169     /**
0170      * ArrayAccess: get field by name
0171      *
0172      * @param  string $name
0173      * @return mixed
0174      */
0175     public function offsetGet($name)
0176     {
0177         return $this->getField($name);
0178     }
0179 
0180     /**
0181      * ArrayAccess: set field to value
0182      *
0183      * @param  string $name
0184      * @param  mixed $value
0185      * @return void
0186      */
0187     public function offsetSet($name, $value)
0188     {
0189         $this->setField($name, $value);
0190     }
0191 
0192     /**
0193      * ArrayAccess: remove field from document
0194      *
0195      * @param  string $name
0196      * @return void
0197      */
0198     public function offsetUnset($name)
0199     {
0200         if ($this->offsetExists($name)) {
0201             unset($this->_fields[$name]);
0202         }
0203     }
0204 
0205     /**
0206      * Overloading: retrieve and set fields by name
0207      *
0208      * @param  string $name
0209      * @param  mixed $args
0210      * @return mixed
0211      */
0212     public function __call($name, $args)
0213     {
0214         $prefix = substr($name, 0, 3);
0215         if ($prefix == 'get') {
0216             // Get value
0217             $option = substr($name, 3);
0218             return $this->getField($option);
0219         } elseif ($prefix == 'set') {
0220             // set value
0221             $option = substr($name, 3);
0222             return $this->setField($option, $args[0]);
0223         }
0224 
0225         // require_once 'Zend/Cloud/OperationNotAvailableException.php';
0226         throw new Zend_Cloud_OperationNotAvailableException("Unknown operation $name");
0227     }
0228 
0229     /**
0230      * Countable: return count of fields in document
0231      *
0232      * @return int
0233      */
0234     public function count()
0235     {
0236         return count($this->_fields);
0237     }
0238 
0239     /**
0240      * IteratorAggregate: return iterator for iterating over fields
0241      *
0242      * @return Iterator
0243      */
0244     public function getIterator()
0245     {
0246         return new ArrayIterator($this->_fields);
0247     }
0248 }