[7576] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
| 4 | * |
---|
| 5 | * Redistribution and use in source and binary forms, with or without |
---|
| 6 | * modification, are permitted provided that the following conditions are met: |
---|
| 7 | * |
---|
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
| 9 | * this list of conditions and the following disclaimer. |
---|
| 10 | * |
---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
| 12 | * this listof conditions and the following disclaimer in the documentation |
---|
| 13 | * and/or other materials provided with the distribution. |
---|
| 14 | * |
---|
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
| 26 | * |
---|
| 27 | * The views and conclusions contained in the software and documentation are |
---|
| 28 | * those of the authors and should not be interpreted as representing official |
---|
| 29 | * policies, either expressed or implied, of the copyright holder. |
---|
| 30 | * |
---|
| 31 | * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl> |
---|
| 32 | * @license http://github.com/basdenooijer/solarium/raw/master/COPYING |
---|
| 33 | * @link http://www.solarium-project.org/ |
---|
| 34 | * |
---|
| 35 | * @package Solarium |
---|
| 36 | * @subpackage Document |
---|
| 37 | */ |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * Read-only Solr document |
---|
| 41 | * |
---|
| 42 | * This is the default Solr document type returned by a select query. You can |
---|
| 43 | * access the fields as object properties or iterate over all fields. |
---|
| 44 | * |
---|
| 45 | * @package Solarium |
---|
| 46 | * @subpackage Document |
---|
| 47 | */ |
---|
| 48 | class Solarium_Document_ReadOnly |
---|
| 49 | implements IteratorAggregate, Countable, ArrayAccess |
---|
| 50 | { |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * All fields in this document |
---|
| 54 | * |
---|
| 55 | * @var array |
---|
| 56 | */ |
---|
| 57 | protected $_fields; |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Constructor |
---|
| 62 | * |
---|
| 63 | * @param array $fields |
---|
| 64 | */ |
---|
| 65 | public function __construct(array $fields) |
---|
| 66 | { |
---|
| 67 | $this->_fields = $fields; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * Get all fields |
---|
| 73 | * |
---|
| 74 | * @return array |
---|
| 75 | */ |
---|
| 76 | public function getFields() |
---|
| 77 | { |
---|
| 78 | return $this->_fields; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * Get field value by name |
---|
| 83 | * |
---|
| 84 | * Magic access method for accessing fields as properties of this document |
---|
| 85 | * object, by field name. |
---|
| 86 | * |
---|
| 87 | * @param string $name |
---|
| 88 | * @return mixed |
---|
| 89 | */ |
---|
| 90 | public function __get($name) |
---|
| 91 | { |
---|
| 92 | if (!isset($this->_fields[$name])) { |
---|
| 93 | return null; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | return $this->_fields[$name]; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * Set field value |
---|
| 101 | * |
---|
| 102 | * Magic method for setting a field as property of this object. Since this |
---|
| 103 | * is a readonly document an exception will be thrown to prevent this. |
---|
| 104 | * |
---|
| 105 | * @param string $name |
---|
| 106 | * @param string $value |
---|
| 107 | * @return void |
---|
| 108 | */ |
---|
| 109 | public function __set($name, $value) |
---|
| 110 | { |
---|
| 111 | throw new Solarium_Exception('A readonly document cannot be altered'); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /** |
---|
| 115 | * IteratorAggregate implementation |
---|
| 116 | * |
---|
| 117 | * @return ArrayIterator |
---|
| 118 | */ |
---|
| 119 | public function getIterator() |
---|
| 120 | { |
---|
| 121 | return new ArrayIterator($this->_fields); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | /** |
---|
| 125 | * Countable implementation |
---|
| 126 | * |
---|
| 127 | * @return int |
---|
| 128 | */ |
---|
| 129 | public function count() |
---|
| 130 | { |
---|
| 131 | return count($this->_fields); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | /** |
---|
| 135 | * ArrayAccess implementation |
---|
| 136 | * |
---|
| 137 | * @param miex $offset |
---|
| 138 | * @param mixed $value |
---|
| 139 | * @return void |
---|
| 140 | */ |
---|
| 141 | public function offsetSet($offset, $value) |
---|
| 142 | { |
---|
| 143 | $this->__set($offset, $value); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | /** |
---|
| 147 | * ArrayAccess implementation |
---|
| 148 | * |
---|
| 149 | * @param mixed $offset |
---|
| 150 | * @return bool |
---|
| 151 | */ |
---|
| 152 | public function offsetExists($offset) |
---|
| 153 | { |
---|
| 154 | return ($this->__get($offset) !== null); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
| 158 | * ArrayAccess implementation |
---|
| 159 | * |
---|
| 160 | * @param mixed $offset |
---|
| 161 | * @return void |
---|
| 162 | */ |
---|
| 163 | public function offsetUnset($offset) |
---|
| 164 | { |
---|
| 165 | $this->__set($offset, null); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | * ArrayAccess implementation |
---|
| 170 | * |
---|
| 171 | * @param mixed $offset |
---|
| 172 | * @return mixed|null |
---|
| 173 | */ |
---|
| 174 | public function offsetGet($offset) |
---|
| 175 | { |
---|
| 176 | return $this->__get($offset); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | } |
---|