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: * cIterator: 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,
26: * and then returned to the caller.
27: *
28: * The caller receives the iterator object and can step trough all items using
29: * the "next" method.
30: *
31: * @package Core
32: * @subpackage Util
33: */
34: class cIterator {
35:
36: /**
37: * Holds the items which should be iterated
38: *
39: * @var array
40: */
41: protected $_aIteratorItems;
42:
43: /**
44: * Constructor to create an instance of this class.
45: *
46: * This function initializes the constructor, adds the passed items
47: * and moves the iterator to the first element.
48: *
49: * @param array $aItems
50: * Items to add
51: */
52: public function __construct($aItems) {
53: if (is_array($aItems)) {
54: $this->_aIteratorItems = $aItems;
55: } else {
56: $this->_aIteratorItems = array();
57: }
58:
59: $this->reset();
60: }
61:
62: /**
63: * reset: Resets the iterator to the first element
64: *
65: * This function moves the iterator to the first element
66: */
67: public function reset() {
68: reset($this->_aIteratorItems);
69: }
70:
71: /**
72: * next: Returns the next item in the iterator
73: *
74: * This function returns the item, or false if no
75: * items are left.
76: *
77: * @return mixed
78: * item or false if nothing was found
79: */
80: public function next() {
81: $item = each($this->_aIteratorItems);
82:
83: if ($item === false) {
84: return false;
85: } else {
86: return $item["value"];
87: }
88: }
89:
90: /**
91: * count: Returns the number of items in the iterator
92: *
93: * @return int
94: * Number of items
95: */
96: public function count() {
97: return count($this->_aIteratorItems);
98: }
99: }
100: