1: <?php
 2: /**
 3:  * CHttpSessionIterator 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:  * CHttpSessionIterator implements an iterator for {@link CHttpSession}.
13:  *
14:  * It allows CHttpSession to return a new iterator for traversing the session variables.
15:  *
16:  * @author Qiang Xue <qiang.xue@gmail.com>
17:  * @package system.web
18:  * @since 1.0
19:  */
20: class CHttpSessionIterator implements Iterator
21: {
22:     /**
23:      * @var array list of keys in the map
24:      */
25:     private $_keys;
26:     /**
27:      * @var mixed current key
28:      */
29:     private $_key;
30: 
31:     /**
32:      * Constructor.
33:      * @param array the data to be iterated through
34:      */
35:     public function __construct()
36:     {
37:         $this->_keys=array_keys($_SESSION);
38:     }
39: 
40:     /**
41:      * Rewinds internal array pointer.
42:      * This method is required by the interface Iterator.
43:      */
44:     public function rewind()
45:     {
46:         $this->_key=reset($this->_keys);
47:     }
48: 
49:     /**
50:      * Returns the key of the current array element.
51:      * This method is required by the interface Iterator.
52:      * @return mixed the key of the current array element
53:      */
54:     public function key()
55:     {
56:         return $this->_key;
57:     }
58: 
59:     /**
60:      * Returns the current array element.
61:      * This method is required by the interface Iterator.
62:      * @return mixed the current array element
63:      */
64:     public function current()
65:     {
66:         return isset($_SESSION[$this->_key])?$_SESSION[$this->_key]:null;
67:     }
68: 
69:     /**
70:      * Moves the internal pointer to the next array element.
71:      * This method is required by the interface Iterator.
72:      */
73:     public function next()
74:     {
75:         do
76:         {
77:             $this->_key=next($this->_keys);
78:         }
79:         while(!isset($_SESSION[$this->_key]) && $this->_key!==false);
80:     }
81: 
82:     /**
83:      * Returns whether there is an element at current position.
84:      * This method is required by the interface Iterator.
85:      * @return boolean
86:      */
87:     public function valid()
88:     {
89:         return $this->_key!==false;
90:     }
91: }
92: