* @license http://github.com/basdenooijer/solarium/raw/master/COPYING * @link http://www.solarium-project.org/ * * @package Solarium * @subpackage Document */ /** * Read-only Solr document * * This is the default Solr document type returned by a select query. You can * access the fields as object properties or iterate over all fields. * * @package Solarium * @subpackage Document */ class Solarium_Document_ReadOnly implements IteratorAggregate, Countable, ArrayAccess { /** * All fields in this document * * @var array */ protected $_fields; /** * Constructor * * @param array $fields */ public function __construct(array $fields) { $this->_fields = $fields; } /** * Get all fields * * @return array */ public function getFields() { return $this->_fields; } /** * Get field value by name * * Magic access method for accessing fields as properties of this document * object, by field name. * * @param string $name * @return mixed */ public function __get($name) { if (!isset($this->_fields[$name])) { return null; } return $this->_fields[$name]; } /** * Set field value * * Magic method for setting a field as property of this object. Since this * is a readonly document an exception will be thrown to prevent this. * * @param string $name * @param string $value * @return void */ public function __set($name, $value) { throw new Solarium_Exception('A readonly document cannot be altered'); } /** * IteratorAggregate implementation * * @return ArrayIterator */ public function getIterator() { return new ArrayIterator($this->_fields); } /** * Countable implementation * * @return int */ public function count() { return count($this->_fields); } /** * ArrayAccess implementation * * @param miex $offset * @param mixed $value * @return void */ public function offsetSet($offset, $value) { $this->__set($offset, $value); } /** * ArrayAccess implementation * * @param mixed $offset * @return bool */ public function offsetExists($offset) { return ($this->__get($offset) !== null); } /** * ArrayAccess implementation * * @param mixed $offset * @return void */ public function offsetUnset($offset) { $this->__set($offset, null); } /** * ArrayAccess implementation * * @param mixed $offset * @return mixed|null */ public function offsetGet($offset) { return $this->__get($offset); } }