web/lib/Zend/Cloud/DocumentService/Document.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    11  * to license@zend.com so we can send you a copy immediately.
    11  * to license@zend.com so we can send you a copy immediately.
    12  *
    12  *
    13  * @category   Zend
    13  * @category   Zend
    14  * @package    Zend_Cloud
    14  * @package    Zend_Cloud
    15  * @subpackage DocumentService
    15  * @subpackage DocumentService
    16  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    16  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    17  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    18  */
    18  */
    19 
    19 
    20 /**
    20 /**
    21  * Class encapsulating documents. Fields are stored in a name/value
    21  * Class encapsulating documents. Fields are stored in a name/value
    24  * TODO Can fields be large enough to warrant support for streams?
    24  * TODO Can fields be large enough to warrant support for streams?
    25  *
    25  *
    26  * @category   Zend
    26  * @category   Zend
    27  * @package    Zend_Cloud
    27  * @package    Zend_Cloud
    28  * @subpackage DocumentService
    28  * @subpackage DocumentService
    29  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    29  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    30  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    30  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    31  */
    31  */
    32 class Zend_Cloud_DocumentService_Document 
    32 class Zend_Cloud_DocumentService_Document
    33     implements ArrayAccess, IteratorAggregate, Countable
    33     implements ArrayAccess, IteratorAggregate, Countable
    34 {
    34 {
    35     /** key in document denoting identifier */
    35     /** key in document denoting identifier */
    36     const KEY_FIELD = '_id';
    36     const KEY_FIELD = '_id';
    37 
    37 
    73         $this->setId($id);
    73         $this->setId($id);
    74     }
    74     }
    75 
    75 
    76     /**
    76     /**
    77      * Set document identifier
    77      * Set document identifier
    78      * 
    78      *
    79      * @param  mixed $id 
    79      * @param  mixed $id
    80      * @return Zend_Cloud_DocumentService_Document
    80      * @return Zend_Cloud_DocumentService_Document
    81      */
    81      */
    82     public function setId($id)
    82     public function setId($id)
    83     {
    83     {
    84         $this->_id = $id;
    84         $this->_id = $id;
    88     /**
    88     /**
    89      * Get ID name.
    89      * Get ID name.
    90      *
    90      *
    91      * @return string
    91      * @return string
    92      */
    92      */
    93     public function getId() 
    93     public function getId()
    94     {
    94     {
    95         return $this->_id;
    95         return $this->_id;
    96     }
    96     }
    97 
    97 
    98     /**
    98     /**
    99      * Get fields as array.
    99      * Get fields as array.
   100      *
   100      *
   101      * @return array
   101      * @return array
   102      */
   102      */
   103     public function getFields() 
   103     public function getFields()
   104     {
   104     {
   105         return $this->_fields;
   105         return $this->_fields;
   106     }
   106     }
   107 
   107 
   108     /**
   108     /**
   116         if (isset($this->_fields[$name])) {
   116         if (isset($this->_fields[$name])) {
   117             return $this->_fields[$name];
   117             return $this->_fields[$name];
   118         }
   118         }
   119         return null;
   119         return null;
   120     }
   120     }
   121     
   121 
   122     /**
   122     /**
   123      * Set field by name.
   123      * Set field by name.
   124      *
   124      *
   125      * @param  string $name
   125      * @param  string $name
   126      * @param  mixed $value
   126      * @param  mixed $value
   127      * @return Zend_Cloud_DocumentService_Document
   127      * @return Zend_Cloud_DocumentService_Document
   128      */
   128      */
   129     public function setField($name, $value) 
   129     public function setField($name, $value)
   130     {
   130     {
   131         $this->_fields[$name] = $value;
   131         $this->_fields[$name] = $value;
   132         return $this;
   132         return $this;
   133     }
   133     }
   134     
   134 
   135     /**
   135     /**
   136      * Overloading: get value
   136      * Overloading: get value
   137      * 
   137      *
   138      * @param  string $name 
   138      * @param  string $name
   139      * @return mixed
   139      * @return mixed
   140      */
   140      */
   141     public function __get($name)
   141     public function __get($name)
   142     {
   142     {
   143         return $this->getField($name);
   143         return $this->getField($name);
   144     }
   144     }
   145 
   145 
   146     /**
   146     /**
   147      * Overloading: set field
   147      * Overloading: set field
   148      * 
   148      *
   149      * @param  string $name 
   149      * @param  string $name
   150      * @param  mixed $value 
   150      * @param  mixed $value
   151      * @return void
   151      * @return void
   152      */
   152      */
   153     public function __set($name, $value)
   153     public function __set($name, $value)
   154     {
   154     {
   155         $this->setField($name, $value);
   155         $this->setField($name, $value);
   156     }
   156     }
   157     
   157 
   158     /**
   158     /**
   159      * ArrayAccess: does field exist?
   159      * ArrayAccess: does field exist?
   160      * 
   160      *
   161      * @param  string $name 
   161      * @param  string $name
   162      * @return bool
   162      * @return bool
   163      */
   163      */
   164     public function offsetExists($name)
   164     public function offsetExists($name)
   165     {
   165     {
   166         return isset($this->_fields[$name]);
   166         return isset($this->_fields[$name]);
   167     }
   167     }
   168     
   168 
   169     /**
   169     /**
   170      * ArrayAccess: get field by name
   170      * ArrayAccess: get field by name
   171      * 
   171      *
   172      * @param  string $name 
   172      * @param  string $name
   173      * @return mixed
   173      * @return mixed
   174      */
   174      */
   175     public function offsetGet($name)
   175     public function offsetGet($name)
   176     {
   176     {
   177         return $this->getField($name);
   177         return $this->getField($name);
   178     }
   178     }
   179     
   179 
   180     /**
   180     /**
   181      * ArrayAccess: set field to value
   181      * ArrayAccess: set field to value
   182      * 
   182      *
   183      * @param  string $name 
   183      * @param  string $name
   184      * @param  mixed $value 
   184      * @param  mixed $value
   185      * @return void
   185      * @return void
   186      */
   186      */
   187     public function offsetSet($name, $value)
   187     public function offsetSet($name, $value)
   188     {
   188     {
   189         $this->setField($name, $value);
   189         $this->setField($name, $value);
   190     }
   190     }
   191     
   191 
   192     /**
   192     /**
   193      * ArrayAccess: remove field from document
   193      * ArrayAccess: remove field from document
   194      * 
   194      *
   195      * @param  string $name 
   195      * @param  string $name
   196      * @return void
   196      * @return void
   197      */
   197      */
   198     public function offsetUnset($name)
   198     public function offsetUnset($name)
   199     {
   199     {
   200         if ($this->offsetExists($name)) {
   200         if ($this->offsetExists($name)) {
   201             unset($this->_fields[$name]);
   201             unset($this->_fields[$name]);
   202         }
   202         }
   203     }
   203     }
   204     
   204 
   205     /**
   205     /**
   206      * Overloading: retrieve and set fields by name
   206      * Overloading: retrieve and set fields by name
   207      * 
   207      *
   208      * @param  string $name 
   208      * @param  string $name
   209      * @param  mixed $args 
   209      * @param  mixed $args
   210      * @return mixed
   210      * @return mixed
   211      */
   211      */
   212     public function __call($name, $args)
   212     public function __call($name, $args)
   213     {
   213     {
   214         $prefix = substr($name, 0, 3);
   214         $prefix = substr($name, 0, 3);
   226         throw new Zend_Cloud_OperationNotAvailableException("Unknown operation $name");
   226         throw new Zend_Cloud_OperationNotAvailableException("Unknown operation $name");
   227     }
   227     }
   228 
   228 
   229     /**
   229     /**
   230      * Countable: return count of fields in document
   230      * Countable: return count of fields in document
   231      * 
   231      *
   232      * @return int
   232      * @return int
   233      */
   233      */
   234     public function count()
   234     public function count()
   235     {
   235     {
   236         return count($this->_fields);
   236         return count($this->_fields);
   237     }
   237     }
   238 
   238 
   239     /**
   239     /**
   240      * IteratorAggregate: return iterator for iterating over fields
   240      * IteratorAggregate: return iterator for iterating over fields
   241      * 
   241      *
   242      * @return Iterator
   242      * @return Iterator
   243      */
   243      */
   244     public function getIterator()
   244     public function getIterator()
   245     {
   245     {
   246         return new ArrayIterator($this->_fields);
   246         return new ArrayIterator($this->_fields);