1: <?php
2: /**
3: * CStackIterator class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CStackIterator implements an iterator for {@link CStack}.
13: *
14: * It allows CStack to return a new iterator for traversing the items in the stack.
15: *
16: * @author Qiang Xue <qiang.xue@gmail.com>
17: * @package system.collections
18: * @since 1.0
19: */
20: class CStackIterator implements Iterator
21: {
22: /**
23: * @var array the data to be iterated through
24: */
25: private $_d;
26: /**
27: * @var integer index of the current item
28: */
29: private $_i;
30: /**
31: * @var integer count of the data items
32: */
33: private $_c;
34:
35: /**
36: * Constructor.
37: * @param array $data the data to be iterated through
38: */
39: public function __construct(&$data)
40: {
41: $this->_d=&$data;
42: $this->_i=0;
43: $this->_c=count($this->_d);
44: }
45:
46: /**
47: * Rewinds internal array pointer.
48: * This method is required by the interface Iterator.
49: */
50: public function rewind()
51: {
52: $this->_i=0;
53: }
54:
55: /**
56: * Returns the key of the current array item.
57: * This method is required by the interface Iterator.
58: * @return integer the key of the current array item
59: */
60: public function key()
61: {
62: return $this->_i;
63: }
64:
65: /**
66: * Returns the current array item.
67: * This method is required by the interface Iterator.
68: * @return mixed the current array item
69: */
70: public function current()
71: {
72: return $this->_d[$this->_i];
73: }
74:
75: /**
76: * Moves the internal pointer to the next array item.
77: * This method is required by the interface Iterator.
78: */
79: public function next()
80: {
81: $this->_i++;
82: }
83:
84: /**
85: * Returns whether there is an item at current position.
86: * This method is required by the interface Iterator.
87: * @return boolean
88: */
89: public function valid()
90: {
91: return $this->_i<$this->_c;
92: }
93: }
94: