Result.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\data\source;
  9. abstract class Result extends \lithium\core\Object implements \Iterator {
  10. /**
  11. * Contains the cached result set.
  12. */
  13. protected $_cache = null;
  14. /**
  15. * The current position of the iterator.
  16. */
  17. protected $_iterator = 0;
  18. /**
  19. * Contains the current element of the result set.
  20. */
  21. protected $_current = false;
  22. /**
  23. * Setted to `true` when the collection has begun iterating.
  24. * @var integer
  25. */
  26. protected $_started = false;
  27. /**
  28. * If the result resource has been initialized
  29. */
  30. protected $_init = false;
  31. /**
  32. * Indicates whether the current position is valid or not.
  33. *
  34. * @var boolean
  35. * @see lithium\data\source\Result::valid()
  36. */
  37. protected $_valid = false;
  38. /**
  39. * If the result resource has been initialized
  40. */
  41. protected $_key = null;
  42. /**
  43. * The bound resource.
  44. */
  45. protected $_resource = null;
  46. /**
  47. * Autoconfig.
  48. */
  49. protected $_autoConfig = array('resource');
  50. /**
  51. * Returns the used resource.
  52. */
  53. public function resource() {
  54. return $this->_resource;
  55. }
  56. /**
  57. * Checks if current position is valid.
  58. *
  59. * @return boolean `true` if valid, `false` otherwise.
  60. */
  61. public function valid() {
  62. if (!$this->_init) {
  63. $this->_valid = $this->_fetch();
  64. }
  65. return $this->_valid;
  66. }
  67. /**
  68. * Rewinds the result set to the first position.
  69. */
  70. public function rewind() {
  71. $this->_iterator = 0;
  72. $this->_started = false;
  73. $this->_key = null;
  74. $this->_current = false;
  75. $this->_init = false;
  76. }
  77. /**
  78. * Contains the current result.
  79. *
  80. * @return array The current result (or `null` if there is none).
  81. */
  82. public function current() {
  83. if (!$this->_init) {
  84. $this->_fetch();
  85. }
  86. $this->_started = true;
  87. return $this->_current;
  88. }
  89. /**
  90. * Returns the current key position on the result.
  91. *
  92. * @return integer The current iterator position.
  93. */
  94. public function key() {
  95. if (!$this->_init) {
  96. $this->_fetch();
  97. }
  98. $this->_started = true;
  99. return $this->_key;
  100. }
  101. /**
  102. * Fetches the previous element from the cache.
  103. *
  104. * @return mixed The previous result (or `false` if there is none).
  105. */
  106. public function prev() {
  107. if (!$this->_cache) {
  108. return;
  109. }
  110. if (isset($this->_cache[--$this->_iterator - 1])) {
  111. $this->_key = $this->_iterator - 1;
  112. return $this->_current = $this->_cache[$this->_iterator - 1];
  113. }
  114. return false;
  115. }
  116. /**
  117. * Fetches the next element from the resource.
  118. *
  119. * @return mixed The next result (or `false` if there is none).
  120. */
  121. public function next() {
  122. if ($this->_started === false) {
  123. return $this->current();
  124. }
  125. $this->_valid = $this->_fetch();
  126. if (!$this->_valid) {
  127. $this->_key = null;
  128. $this->_current = false;
  129. }
  130. return $this->current();
  131. }
  132. /**
  133. * Fetches the current element from the resource.
  134. *
  135. * @return boolean Return `true` on success or `false` otherwise.
  136. */
  137. protected function _fetch() {
  138. $this->_init = true;
  139. if ($this->_fetchFromCache() || $this->_fetchFromResource()) {
  140. return true;
  141. }
  142. return false;
  143. }
  144. abstract protected function _fetchFromResource();
  145. /**
  146. * Returns the result from the primed cache.
  147. *
  148. * @return boolean Return `true` on success or `false` if it has not been cached yet.
  149. */
  150. protected function _fetchFromCache() {
  151. if ($this->_iterator < count($this->_cache)) {
  152. $this->_key = $this->_iterator;
  153. $this->_current = $this->_cache[$this->_iterator++];
  154. return true;
  155. }
  156. return false;
  157. }
  158. /**
  159. * Close the resource.
  160. */
  161. public function close() {
  162. unset($this->_resource);
  163. $this->_resource = null;
  164. }
  165. /**
  166. * The destructor.
  167. */
  168. public function __destruct() {
  169. $this->close();
  170. }
  171. }
  172. ?>