1: <?php
2: /**
3: * This file contains the iterator class.
4: *
5: * @package Core
6: * @subpackage Util
7: * @author Unknown
8: * @copyright four for business AG <www.4fb.de>
9: * @license http://www.contenido.org/license/LIZENZ.txt
10: * @link http://www.4fb.de
11: * @link http://www.contenido.org
12: */
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: /**
17: * A class which represents the C/C++/JAVA Iterator support.
18: *
19: * Iterating items is a mechanism to "step" trough a list of defined items.
20: * Basically, the iterator is similar to an array, but provides easy functions
21: * to step trough the list.
22: *
23: * An instance of an iterator is usually created by a class returning multiple
24: * items and automatically filled using the $aItems parameter of the
25: * constructor, and then returned to the caller.
26: *
27: * The caller receives the iterator object and can step trough all items using
28: * the "next" method.
29: *
30: * @package Core
31: * @subpackage Util
32: */
33: class cIterator {
34:
35: /**
36: * Holds the items to iterate.
37: *
38: * @var array
39: */
40: protected $_aIteratorItems;
41:
42: /**
43: * Holds the keys of the array which should be iterated
44: *
45: * @var array
46: */
47: protected $_keys;
48:
49: /**
50: * Constructor to create an instance of this class.
51: *
52: * This function initializes the constructor, adds the passed items
53: * and moves the iterator to the first element.
54: *
55: * @param array $aItems
56: * Items to add
57: */
58: public function __construct($aItems) {
59: $this->_aIteratorItems = is_array($aItems) ? $aItems : array();
60: $this->reset();
61: }
62:
63: /**
64: * Resets the iterator to the first element.
65: *
66: * This function moves the iterator to the first element
67: */
68: public function reset() {
69: $this->_keys = array_keys($this->_aIteratorItems);
70: }
71:
72: /**
73: * Returns the next item in the iterator.
74: *
75: * This function returns the item, or false if no items are left.
76: *
77: * @return mixed
78: * item or false if no items are left
79: */
80: public function next() {
81: $key = array_shift($this->_keys);
82: return isset($this->_aIteratorItems[$key]) ? $this->_aIteratorItems[$key] : false;
83: }
84:
85: /**
86: * Returns the number of items in the iterator.
87: *
88: * @return int
89: * number of items
90: */
91: public function count() {
92: return count($this->_aIteratorItems);
93: }
94: }
95: