Abstract.php
390 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * Zend Framework |
| 6 | * |
| 7 | * LICENSE |
| 8 | * |
| 9 | * This source file is subject to the new BSD license that is bundled |
| 10 | * with this package in the file LICENSE.txt. |
| 11 | * It is also available through the world-wide-web at this URL: |
| 12 | * http://framework.zend.com/license/new-bsd |
| 13 | * If you did not receive a copy of the license and are unable to |
| 14 | * obtain it through the world-wide-web, please send an email |
| 15 | * to license@zend.com so we can send you a copy immediately. |
| 16 | * |
| 17 | * @category Zend |
| 18 | * @package Zend_Db |
| 19 | * @subpackage Table |
| 20 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 21 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 22 | * @version $Id: Abstract.php 23775 2011-03-01 17:25:24Z ralph $ |
| 23 | */ |
| 24 | /** |
| 25 | * @category Zend |
| 26 | * @package Zend_Db |
| 27 | * @subpackage Table |
| 28 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 29 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 30 | */ |
| 31 | abstract class Zend_Db_Table_Rowset_Abstract implements \SeekableIterator, \Countable, \ArrayAccess |
| 32 | { |
| 33 | /** |
| 34 | * The original data for each row. |
| 35 | * |
| 36 | * @var array |
| 37 | */ |
| 38 | protected $_data = array(); |
| 39 | /** |
| 40 | * Zend_Db_Table_Abstract object. |
| 41 | * |
| 42 | * @var Zend_Db_Table_Abstract |
| 43 | */ |
| 44 | protected $_table; |
| 45 | /** |
| 46 | * Connected is true if we have a reference to a live |
| 47 | * Zend_Db_Table_Abstract object. |
| 48 | * This is false after the Rowset has been deserialized. |
| 49 | * |
| 50 | * @var boolean |
| 51 | */ |
| 52 | protected $_connected = \true; |
| 53 | /** |
| 54 | * Zend_Db_Table_Abstract class name. |
| 55 | * |
| 56 | * @var string |
| 57 | */ |
| 58 | protected $_tableClass; |
| 59 | /** |
| 60 | * Zend_Db_Table_Row_Abstract class name. |
| 61 | * |
| 62 | * @var string |
| 63 | */ |
| 64 | protected $_rowClass = 'Zend_Db_Table_Row'; |
| 65 | /** |
| 66 | * Iterator pointer. |
| 67 | * |
| 68 | * @var integer |
| 69 | */ |
| 70 | protected $_pointer = 0; |
| 71 | /** |
| 72 | * How many data rows there are. |
| 73 | * |
| 74 | * @var integer |
| 75 | */ |
| 76 | protected $_count; |
| 77 | /** |
| 78 | * Collection of instantiated Zend_Db_Table_Row objects. |
| 79 | * |
| 80 | * @var array |
| 81 | */ |
| 82 | protected $_rows = array(); |
| 83 | /** |
| 84 | * @var boolean |
| 85 | */ |
| 86 | protected $_stored = \false; |
| 87 | /** |
| 88 | * @var boolean |
| 89 | */ |
| 90 | protected $_readOnly = \false; |
| 91 | /** |
| 92 | * Constructor. |
| 93 | * |
| 94 | * @param array $config |
| 95 | */ |
| 96 | public function __construct(array $config) |
| 97 | { |
| 98 | if (isset($config['table'])) { |
| 99 | $this->_table = $config['table']; |
| 100 | $this->_tableClass = \get_class($this->_table); |
| 101 | } |
| 102 | if (isset($config['rowClass'])) { |
| 103 | $this->_rowClass = $config['rowClass']; |
| 104 | } |
| 105 | if (!\class_exists($this->_rowClass)) { |
| 106 | // require_once 'Zend/Loader.php'; |
| 107 | \Zend_Loader::loadClass($this->_rowClass); |
| 108 | } |
| 109 | if (isset($config['data'])) { |
| 110 | $this->_data = $config['data']; |
| 111 | } |
| 112 | if (isset($config['readOnly'])) { |
| 113 | $this->_readOnly = $config['readOnly']; |
| 114 | } |
| 115 | if (isset($config['stored'])) { |
| 116 | $this->_stored = $config['stored']; |
| 117 | } |
| 118 | // set the count of rows |
| 119 | $this->_count = \count($this->_data); |
| 120 | $this->init(); |
| 121 | } |
| 122 | /** |
| 123 | * Store data, class names, and state in serialized object |
| 124 | * |
| 125 | * @return array |
| 126 | */ |
| 127 | public function __sleep() |
| 128 | { |
| 129 | return array('_data', '_tableClass', '_rowClass', '_pointer', '_count', '_rows', '_stored', '_readOnly'); |
| 130 | } |
| 131 | /** |
| 132 | * Setup to do on wakeup. |
| 133 | * A de-serialized Rowset should not be assumed to have access to a live |
| 134 | * database connection, so set _connected = false. |
| 135 | * |
| 136 | * @return void |
| 137 | */ |
| 138 | public function __wakeup() |
| 139 | { |
| 140 | $this->_connected = \false; |
| 141 | } |
| 142 | /** |
| 143 | * Initialize object |
| 144 | * |
| 145 | * Called from {@link __construct()} as final step of object instantiation. |
| 146 | * |
| 147 | * @return void |
| 148 | */ |
| 149 | public function init() |
| 150 | { |
| 151 | } |
| 152 | /** |
| 153 | * Return the connected state of the rowset. |
| 154 | * |
| 155 | * @return boolean |
| 156 | */ |
| 157 | public function isConnected() |
| 158 | { |
| 159 | return $this->_connected; |
| 160 | } |
| 161 | /** |
| 162 | * Returns the table object, or null if this is disconnected rowset |
| 163 | * |
| 164 | * @return Zend_Db_Table_Abstract |
| 165 | */ |
| 166 | public function getTable() |
| 167 | { |
| 168 | return $this->_table; |
| 169 | } |
| 170 | /** |
| 171 | * Set the table object, to re-establish a live connection |
| 172 | * to the database for a Rowset that has been de-serialized. |
| 173 | * |
| 174 | * @param Zend_Db_Table_Abstract $table |
| 175 | * @return boolean |
| 176 | * @throws Zend_Db_Table_Row_Exception |
| 177 | */ |
| 178 | public function setTable(\Zend_Db_Table_Abstract $table) |
| 179 | { |
| 180 | $this->_table = $table; |
| 181 | $this->_connected = \false; |
| 182 | // @todo This works only if we have iterated through |
| 183 | // the result set once to instantiate the rows. |
| 184 | foreach ($this as $row) { |
| 185 | $connected = $row->setTable($table); |
| 186 | if ($connected == \true) { |
| 187 | $this->_connected = \true; |
| 188 | } |
| 189 | } |
| 190 | return $this->_connected; |
| 191 | } |
| 192 | /** |
| 193 | * Query the class name of the Table object for which this |
| 194 | * Rowset was created. |
| 195 | * |
| 196 | * @return string |
| 197 | */ |
| 198 | public function getTableClass() |
| 199 | { |
| 200 | return $this->_tableClass; |
| 201 | } |
| 202 | /** |
| 203 | * Rewind the Iterator to the first element. |
| 204 | * Similar to the reset() function for arrays in PHP. |
| 205 | * Required by interface Iterator. |
| 206 | * |
| 207 | * @return Zend_Db_Table_Rowset_Abstract Fluent interface. |
| 208 | */ |
| 209 | public function rewind() |
| 210 | { |
| 211 | $this->_pointer = 0; |
| 212 | return $this; |
| 213 | } |
| 214 | /** |
| 215 | * Return the current element. |
| 216 | * Similar to the current() function for arrays in PHP |
| 217 | * Required by interface Iterator. |
| 218 | * |
| 219 | * @return Zend_Db_Table_Row_Abstract current element from the collection |
| 220 | */ |
| 221 | public function current() |
| 222 | { |
| 223 | if ($this->valid() === \false) { |
| 224 | return null; |
| 225 | } |
| 226 | // return the row object |
| 227 | return $this->_loadAndReturnRow($this->_pointer); |
| 228 | } |
| 229 | /** |
| 230 | * Return the identifying key of the current element. |
| 231 | * Similar to the key() function for arrays in PHP. |
| 232 | * Required by interface Iterator. |
| 233 | * |
| 234 | * @return int |
| 235 | */ |
| 236 | public function key() |
| 237 | { |
| 238 | return $this->_pointer; |
| 239 | } |
| 240 | /** |
| 241 | * Move forward to next element. |
| 242 | * Similar to the next() function for arrays in PHP. |
| 243 | * Required by interface Iterator. |
| 244 | * |
| 245 | * @return void |
| 246 | */ |
| 247 | public function next() |
| 248 | { |
| 249 | ++$this->_pointer; |
| 250 | } |
| 251 | /** |
| 252 | * Check if there is a current element after calls to rewind() or next(). |
| 253 | * Used to check if we've iterated to the end of the collection. |
| 254 | * Required by interface Iterator. |
| 255 | * |
| 256 | * @return bool False if there's nothing more to iterate over |
| 257 | */ |
| 258 | public function valid() |
| 259 | { |
| 260 | return $this->_pointer >= 0 && $this->_pointer < $this->_count; |
| 261 | } |
| 262 | /** |
| 263 | * Returns the number of elements in the collection. |
| 264 | * |
| 265 | * Implements Countable::count() |
| 266 | * |
| 267 | * @return int |
| 268 | */ |
| 269 | public function count() |
| 270 | { |
| 271 | return $this->_count; |
| 272 | } |
| 273 | /** |
| 274 | * Take the Iterator to position $position |
| 275 | * Required by interface SeekableIterator. |
| 276 | * |
| 277 | * @param int $position the position to seek to |
| 278 | * @return Zend_Db_Table_Rowset_Abstract |
| 279 | * @throws Zend_Db_Table_Rowset_Exception |
| 280 | */ |
| 281 | public function seek($position) |
| 282 | { |
| 283 | $position = (int) $position; |
| 284 | if ($position < 0 || $position >= $this->_count) { |
| 285 | // require_once 'Zend/Db/Table/Rowset/Exception.php'; |
| 286 | throw new \Zend_Db_Table_Rowset_Exception("Illegal index {$position}"); |
| 287 | } |
| 288 | $this->_pointer = $position; |
| 289 | return $this; |
| 290 | } |
| 291 | /** |
| 292 | * Check if an offset exists |
| 293 | * Required by the ArrayAccess implementation |
| 294 | * |
| 295 | * @param string $offset |
| 296 | * @return boolean |
| 297 | */ |
| 298 | public function offsetExists($offset) |
| 299 | { |
| 300 | return isset($this->_data[(int) $offset]); |
| 301 | } |
| 302 | /** |
| 303 | * Get the row for the given offset |
| 304 | * Required by the ArrayAccess implementation |
| 305 | * |
| 306 | * @param string $offset |
| 307 | * @return Zend_Db_Table_Row_Abstract |
| 308 | */ |
| 309 | public function offsetGet($offset) |
| 310 | { |
| 311 | $offset = (int) $offset; |
| 312 | if ($offset < 0 || $offset >= $this->_count) { |
| 313 | // require_once 'Zend/Db/Table/Rowset/Exception.php'; |
| 314 | throw new \Zend_Db_Table_Rowset_Exception("Illegal index {$offset}"); |
| 315 | } |
| 316 | $this->_pointer = $offset; |
| 317 | return $this->current(); |
| 318 | } |
| 319 | /** |
| 320 | * Does nothing |
| 321 | * Required by the ArrayAccess implementation |
| 322 | * |
| 323 | * @param string $offset |
| 324 | * @param mixed $value |
| 325 | */ |
| 326 | public function offsetSet($offset, $value) |
| 327 | { |
| 328 | } |
| 329 | /** |
| 330 | * Does nothing |
| 331 | * Required by the ArrayAccess implementation |
| 332 | * |
| 333 | * @param string $offset |
| 334 | */ |
| 335 | public function offsetUnset($offset) |
| 336 | { |
| 337 | } |
| 338 | /** |
| 339 | * Returns a Zend_Db_Table_Row from a known position into the Iterator |
| 340 | * |
| 341 | * @param int $position the position of the row expected |
| 342 | * @param bool $seek wether or not seek the iterator to that position after |
| 343 | * @return Zend_Db_Table_Row |
| 344 | * @throws Zend_Db_Table_Rowset_Exception |
| 345 | */ |
| 346 | public function getRow($position, $seek = \false) |
| 347 | { |
| 348 | try { |
| 349 | $row = $this->_loadAndReturnRow($position); |
| 350 | } catch (\Zend_Db_Table_Rowset_Exception $e) { |
| 351 | // require_once 'Zend/Db/Table/Rowset/Exception.php'; |
| 352 | throw new \Zend_Db_Table_Rowset_Exception('No row could be found at position ' . (int) $position, 0, $e); |
| 353 | } |
| 354 | if ($seek == \true) { |
| 355 | $this->seek($position); |
| 356 | } |
| 357 | return $row; |
| 358 | } |
| 359 | /** |
| 360 | * Returns all data as an array. |
| 361 | * |
| 362 | * Updates the $_data property with current row object values. |
| 363 | * |
| 364 | * @return array |
| 365 | */ |
| 366 | public function toArray() |
| 367 | { |
| 368 | // @todo This works only if we have iterated through |
| 369 | // the result set once to instantiate the rows. |
| 370 | foreach ($this->_rows as $i => $row) { |
| 371 | $this->_data[$i] = $row->toArray(); |
| 372 | } |
| 373 | return $this->_data; |
| 374 | } |
| 375 | protected function _loadAndReturnRow($position) |
| 376 | { |
| 377 | if (!isset($this->_data[$position])) { |
| 378 | // require_once 'Zend/Db/Table/Rowset/Exception.php'; |
| 379 | throw new \Zend_Db_Table_Rowset_Exception("Data for provided position does not exist"); |
| 380 | } |
| 381 | // do we already have a row object for this position? |
| 382 | if (empty($this->_rows[$position])) { |
| 383 | $this->_rows[$position] = new $this->_rowClass(array('table' => $this->_table, 'data' => $this->_data[$position], 'stored' => $this->_stored, 'readOnly' => $this->_readOnly)); |
| 384 | } |
| 385 | // return the row object |
| 386 | return $this->_rows[$position]; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 |