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
54: * Items to add
55: */
56: public function __construct($aItems) {
57: if (is_array($aItems)) {
58: $this->_aIteratorItems = $aItems;
59: } else {
60: $this->_aIteratorItems = array();
61: }
62:
63: $this->reset();
64: }
65:
66: /**
67: * reset: Resets the iterator to the first element
68: *
69: * This function moves the iterator to the first element
70: */
71: public function reset() {
72: reset($this->_aIteratorItems);
73: }
74:
75: /**
76: * next: Returns the next item in the iterator
77: *
78: * This function returns the item, or false if no
79: * items are left.
80: *
81: * @return mixed
82: * item or false if nothing was found
83: */
84: public function next() {
85: $item = each($this->_aIteratorItems);
86:
87: if ($item === false) {
88: return false;
89: } else {
90: return $item["value"];
91: }
92: }
93:
94: /**
95: * count: Returns the number of items in the iterator
96: *
97: * @return int
98: * Number of items
99: */
100: public function count() {
101: return count($this->_aIteratorItems);
102: }
103: }
104: