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