1 | <?php |
||
2 | /** |
||
3 | * @link https://www.yiiframework.com/ |
||
4 | * @copyright Copyright (c) 2008 Yii Software LLC |
||
5 | * @license https://www.yiiframework.com/license/ |
||
6 | */ |
||
7 | |||
8 | namespace yii\web; |
||
9 | |||
10 | /** |
||
11 | * SessionIterator implements an [[\Iterator|iterator]] for traversing session variables managed by [[Session]]. |
||
12 | * |
||
13 | * @author Qiang Xue <[email protected]> |
||
14 | * @since 2.0 |
||
15 | */ |
||
16 | class SessionIterator implements \Iterator |
||
17 | { |
||
18 | /** |
||
19 | * @var array list of keys in the map |
||
20 | */ |
||
21 | private $_keys; |
||
22 | /** |
||
23 | * @var string|int|false current key |
||
24 | */ |
||
25 | private $_key; |
||
26 | |||
27 | |||
28 | /** |
||
29 | * Constructor. |
||
30 | */ |
||
31 | public function __construct() |
||
32 | { |
||
33 | $this->_keys = array_keys(isset($_SESSION) ? $_SESSION : []); |
||
34 | $this->rewind(); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Rewinds internal array pointer. |
||
39 | * This method is required by the interface [[\Iterator]]. |
||
40 | */ |
||
41 | #[\ReturnTypeWillChange] |
||
42 | public function rewind() |
||
43 | { |
||
44 | $this->_key = reset($this->_keys); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Returns the key of the current array element. |
||
49 | * This method is required by the interface [[\Iterator]]. |
||
50 | * @return string|int|null the key of the current array element |
||
51 | */ |
||
52 | #[\ReturnTypeWillChange] |
||
53 | public function key() |
||
54 | { |
||
55 | return $this->_key === false ? null : $this->_key; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Returns the current array element. |
||
60 | * This method is required by the interface [[\Iterator]]. |
||
61 | * @return mixed the current array element |
||
62 | */ |
||
63 | #[\ReturnTypeWillChange] |
||
64 | public function current() |
||
65 | { |
||
66 | return $this->_key !== false && 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 | #[\ReturnTypeWillChange] |
||
74 | public function next() |
||
75 | { |
||
76 | do { |
||
77 | $this->_key = next($this->_keys); |
||
78 | } while ($this->_key !== false && !isset($_SESSION[$this->_key])); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Returns whether there is an element at current position. |
||
83 | * This method is required by the interface [[\Iterator]]. |
||
84 | * @return bool |
||
85 | */ |
||
86 | #[\ReturnTypeWillChange] |
||
87 | public function valid() |
||
88 | { |
||
89 | return $this->_key !== false; |
||
90 | } |
||
91 | } |
||
92 |