Source for file DibiResultIterator.php

Documentation is available at DibiResultIterator.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * dibi - tiny'n'smart database abstraction layer
  5. 5:  * ----------------------------------------------
  6. 6:  *
  7. 7:  * @copyright  Copyright (c) 2005, 2010 David Grudl
  8. 8:  * @license    http://dibiphp.com/license  dibi license
  9. 9:  * @link       http://dibiphp.com
  10. 10:  * @package    dibi
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * External result set iterator.
  17. 17:  *
  18. 18:  * This can be returned by DibiResult::getIterator() method or using foreach
  19. 19:  * <code>
  20. 20:  * $result = dibi::query('SELECT * FROM table');
  21. 21:  * foreach ($result as $row) {
  22. 22:  *    print_r($row);
  23. 23:  * }
  24. 24:  * unset($result);
  25. 25:  * </code>
  26. 26:  *
  27. 27:  * @copyright  Copyright (c) 2005, 2010 David Grudl
  28. 28:  * @package    dibi
  29. 29:  */
  30. 30: class DibiResultIterator implements IteratorCountable
  31. 31: {
  32. 32:     /** @var DibiResult */
  33. 33:     private $result;
  34. 34:  
  35. 35:     /** @var int */
  36. 36:     private $row;
  37. 37:  
  38. 38:     /** @var int */
  39. 39:     private $pointer;
  40. 40:  
  41. 41:  
  42. 42:     /**
  43. 43:      * @param  DibiResult 
  44. 44:      */
  45. 45:     public function __construct(DibiResult $result)
  46. 46:     {
  47. 47:         $this->result $result;
  48. 48:     }
  49. 49:  
  50. 50:  
  51. 51:  
  52. 52:     /**
  53. 53:      * Rewinds the iterator to the first element.
  54. 54:      * @return void 
  55. 55:      */
  56. 56:     public function rewind()
  57. 57:     {
  58. 58:         $this->pointer 0;
  59. 59:         $this->result->seek(0);
  60. 60:         $this->row $this->result->fetch();
  61. 61:     }
  62. 62:  
  63. 63:  
  64. 64:  
  65. 65:     /**
  66. 66:      * Returns the key of the current element.
  67. 67:      * @return mixed 
  68. 68:      */
  69. 69:     public function key()
  70. 70:     {
  71. 71:         return $this->pointer;
  72. 72:     }
  73. 73:  
  74. 74:  
  75. 75:  
  76. 76:     /**
  77. 77:      * Returns the current element.
  78. 78:      * @return mixed 
  79. 79:      */
  80. 80:     public function current()
  81. 81:     {
  82. 82:         return $this->row;
  83. 83:     }
  84. 84:  
  85. 85:  
  86. 86:  
  87. 87:     /**
  88. 88:      * Moves forward to next element.
  89. 89:      * @return void 
  90. 90:      */
  91. 91:     public function next()
  92. 92:     {
  93. 93:         $this->row $this->result->fetch();
  94. 94:         $this->pointer++;
  95. 95:     }
  96. 96:  
  97. 97:  
  98. 98:  
  99. 99:     /**
  100. 100:      * Checks if there is a current element after calls to rewind() or next().
  101. 101:      * @return bool 
  102. 102:      */
  103. 103:     public function valid()
  104. 104:     {
  105. 105:         return !empty($this->row);
  106. 106:     }
  107. 107:  
  108. 108:  
  109. 109:  
  110. 110:     /**
  111. 111:      * Required by the Countable interface.
  112. 112:      * @return int 
  113. 113:      */
  114. 114:     public function count()
  115. 115:     {
  116. 116:         return $this->result->getRowCount();
  117. 117:     }
  118. 118: