| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Xyster Framework |
| 4 |
|
* |
| 5 |
|
* LICENSE |
| 6 |
|
* |
| 7 |
|
* This source file is subject to the new BSD license that is bundled |
| 8 |
|
* with this package in the file LICENSE.txt. |
| 9 |
|
* It is also available through the world-wide-web at this URL: |
| 10 |
|
* http://www.opensource.org/licenses/bsd-license.php |
| 11 |
|
* If you did not receive a copy of the license and are unable to |
| 12 |
|
* obtain it through the world-wide-web, please send an email |
| 13 |
|
* to xyster@devweblog.org so we can send you a copy immediately. |
| 14 |
|
* |
| 15 |
|
* @category Xyster |
| 16 |
|
* @package Xyster_Collection |
| 17 |
|
* @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org) |
| 18 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 19 |
|
*/ |
| 20 |
|
/** |
| 21 |
|
* Iterator for collections |
| 22 |
|
* |
| 23 |
|
* @category Xyster |
| 24 |
|
* @package Xyster_Collection |
| 25 |
|
* @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org) |
| 26 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 27 |
|
*/ |
| 28 |
|
class Xyster_Collection_Iterator implements SeekableIterator, Countable |
| 29 |
|
{ |
| 30 |
|
/** |
| 31 |
|
* The array over which to iterate |
| 32 |
|
* |
| 33 |
|
* @var array |
| 34 |
|
*/ |
| 35 |
|
protected $_items; |
| 36 |
|
|
| 37 |
|
/** |
| 38 |
|
* Creates a new iterator |
| 39 |
|
* |
| 40 |
|
* @param array $list An array to iterate |
| 41 |
|
*/ |
| 42 |
|
public function __construct( array $list ) |
| 43 |
|
{ |
| 44 |
184 |
$this->_items = $list; |
| 45 |
|
} |
| 46 |
|
|
| 47 |
|
/** |
| 48 |
|
* Counts the number of elements |
| 49 |
|
* |
| 50 |
|
* @return int Number of elements in collection |
| 51 |
|
*/ |
| 52 |
|
public function count() |
| 53 |
|
{ |
| 54 |
1 |
return count($this->_items); |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
/** |
| 58 |
|
* Return the current element |
| 59 |
|
* |
| 60 |
|
* @return mixed The current element |
| 61 |
|
*/ |
| 62 |
|
public function current() |
| 63 |
|
{ |
| 64 |
183 |
return current($this->_items); |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
/** |
| 68 |
|
* Return the key of the current element |
| 69 |
|
* |
| 70 |
|
* @return mixed Key of the current element |
| 71 |
|
*/ |
| 72 |
|
public function key() |
| 73 |
|
{ |
| 74 |
12 |
return key($this->_items); |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
/** |
| 78 |
|
* Move pointer forward to next element |
| 79 |
|
* |
| 80 |
|
*/ |
| 81 |
|
public function next() |
| 82 |
|
{ |
| 83 |
156 |
next($this->_items); |
| 84 |
|
} |
| 85 |
|
|
| 86 |
|
/** |
| 87 |
|
* Rewind the pointer to the first element |
| 88 |
|
* |
| 89 |
|
*/ |
| 90 |
|
public function rewind() |
| 91 |
|
{ |
| 92 |
179 |
reset($this->_items); |
| 93 |
|
} |
| 94 |
|
|
| 95 |
|
/** |
| 96 |
|
* Check if there is a current element after calls to rewind() or next() |
| 97 |
|
* |
| 98 |
|
* @return boolean |
| 99 |
|
*/ |
| 100 |
|
public function valid() |
| 101 |
|
{ |
| 102 |
179 |
return ( $this->current() !== false ); |
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
/** |
| 106 |
|
* Seek to an absolute position. |
| 107 |
|
* |
| 108 |
|
* @param int $index Position to seek |
| 109 |
|
* @throws Xyster_Collection_Exception if the seek position is not found |
| 110 |
|
*/ |
| 111 |
|
public function seek( $index ) |
| 112 |
|
{ |
| 113 |
1 |
$this->rewind(); |
| 114 |
1 |
$position = 0; |
| 115 |
1 |
while( $position < $index && $this->valid() ) { |
| 116 |
1 |
$this->next(); |
| 117 |
1 |
$position++; |
| 118 |
1 |
} |
| 119 |
1 |
if ( !$this->valid() ) { |
| 120 |
1 |
require_once 'Xyster/Collection/Exception.php'; |
| 121 |
1 |
throw new Xyster_Collection_Exception('Out of bounds'); |
| 122 |
0 |
} |
| 123 |
|
} |
| 124 |
|
} |