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