1: <?php
2: /**
3: * CMapIterator 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: * CMapIterator implements an iterator for {@link CMap}.
13: *
14: * It allows CMap to return a new iterator for traversing the items in the map.
15: *
16: * @author Qiang Xue <qiang.xue@gmail.com>
17: * @package system.collections
18: * @since 1.0
19: */
20: class CMapIterator implements Iterator
21: {
22: /**
23: * @var array the data to be iterated through
24: */
25: private $_d;
26: /**
27: * @var array list of keys in the map
28: */
29: private $_keys;
30: /**
31: * @var mixed current key
32: */
33: private $_key;
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->_keys=array_keys($data);
43: $this->_key=reset($this->_keys);
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->_key=reset($this->_keys);
53: }
54:
55: /**
56: * Returns the key of the current array element.
57: * This method is required by the interface Iterator.
58: * @return mixed the key of the current array element
59: */
60: public function key()
61: {
62: return $this->_key;
63: }
64:
65: /**
66: * Returns the current array element.
67: * This method is required by the interface Iterator.
68: * @return mixed the current array element
69: */
70: public function current()
71: {
72: return $this->_d[$this->_key];
73: }
74:
75: /**
76: * Moves the internal pointer to the next array element.
77: * This method is required by the interface Iterator.
78: */
79: public function next()
80: {
81: $this->_key=next($this->_keys);
82: }
83:
84: /**
85: * Returns whether there is an element at current position.
86: * This method is required by the interface Iterator.
87: * @return boolean
88: */
89: public function valid()
90: {
91: return $this->_key!==false;
92: }
93: }
94: