CHttpSessionIterator.php 1.9 KB

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