CListIterator.php 1.8 KB

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