Source for file RdqlResultIterator.php

Documentation is available at RdqlResultIterator.php

  1. <?php
  2.  
  3. // ----------------------------------------------------------------------------------
  4. // Class: RdqlResultIterator
  5. // ----------------------------------------------------------------------------------
  6.  
  7. /**
  8. * Iterator for traversing Rdql results.
  9. * This class can be used for iterating forward and backward trough Rdql results.
  10. * It should be instanced using the rdqlQueryAsIterator() method of a MemModel or a DBModel.
  11. *
  12. *
  13. * <BR><BR>History:<UL>
  14. * <LI>25-03-2004 : Bug in the handling of empty results fixed (radol@gmx.de).</LI>
  15. * <LI>11-06-2003 : First version of this class.</LI>
  16. * </UL>
  17. *
  18. * @version V0.9.3
  19. * @author Daniel Westphal <mail@d-westphal.de>, Chris Bizer <chris@bizer.de>
  20. *
  21. * @package rdql
  22. * @access public
  23. *
  24. */
  25. class RdqlResultIterator extends Object {
  26.  
  27. /**
  28. * Reference to the Rdql result
  29. * @var array RdqlResult
  30. * @access private
  31. */
  32. var $RdqlResult;
  33.  
  34. /**
  35. * Current position
  36. * RdqlResultIterator does not use the build in PHP array iterator,
  37. * so you can use serveral iterators on a single Rdql result.
  38. * @var integer
  39. * @access private
  40. */
  41. var $position;
  42. /**
  43. * Constructor
  44. *
  45. * @param object RdqlResult
  46. * @access public
  47. */
  48. function RdqlResultIterator(&$RdqlResult) {
  49. $noResult = TRUE;
  50. foreach($RdqlResult[0] as $value)
  51. if ($value != NULL) {
  52. $noResult = FALSE;
  53. break;
  54. }
  55. if($noResult)
  56. $this->RdqlResult = NULL;
  57. else
  58. $this->RdqlResult = $RdqlResult;
  59.  
  60. $this->position = -1;
  61. }
  62. /**
  63. * Returns the labels of the result as array.
  64. * @return array of strings with the result labels OR null if there are no results.
  65. * @access public
  66. */
  67. function getResultLabels() {
  68. if(count($this->RdqlResult)>0) {
  69. return array_keys($this->RdqlResult[0]);
  70. } else return null;
  71. }
  72.  
  73. /**
  74. * Returns the number of results.
  75. * @return integer
  76. * @access public
  77. */
  78. function countResults() {
  79. return count($this->RdqlResult);
  80.  
  81. }
  82. /**
  83. * Returns TRUE if there are more results.
  84. * @return boolean
  85. * @access public
  86. */
  87. function hasNext() {
  88. if ($this->position < count($this->RdqlResult) - 1 ) {
  89. return TRUE;
  90. } else {
  91. return FALSE;
  92. }
  93. }
  94.  
  95. /**
  96. * Returns TRUE if the first result has not been reached.
  97. * @return boolean
  98. * @access public
  99. */
  100. function hasPrevious() {
  101. if ($this->position > 0) {
  102. return TRUE;
  103. } else {
  104. return FALSE;
  105. } }
  106. /**
  107. * Returns the next result array.
  108. * @param integer $element
  109. * @return result array OR single result if $element was specified OR NULL if there is no next result.
  110. * @access public
  111. */
  112. function next($element = null) {
  113. if ($this->position < count($this->RdqlResult) - 1) {
  114. $this->position++;
  115. if ($element) {return $this->RdqlResult[$this->position][$element];}
  116. else return $this->RdqlResult[$this->position];
  117. } else {
  118. return NULL;
  119. }
  120. }
  121.  
  122. /**
  123. * Returns the previous result.
  124. * @param integer $element
  125. * @return result array OR single result if $element was specified OR NULL if there is no next result.
  126. * @access public
  127. */
  128. function previous($element = null) {
  129. if ($this->position > 0) {
  130. $this->position--;
  131. if ($element) {return $this->RdqlResult[$this->position][$element];}
  132. else return $this->RdqlResult[$this->position];
  133. } else {
  134. return NULL;
  135. }
  136. }
  137.  
  138. /**
  139. * Returns the current result.
  140. * @param integer $element
  141. * @return result array OR single result if $element was specified OR NULL if there is no next result.
  142. * @access public
  143. */
  144. function current($element = null) {
  145. if (($this->position >= 0) && ($this->position < count($this->RdqlResult))) {
  146. if ($element) {return $this->RdqlResult[$this->position][$element];}
  147. else return $this->RdqlResult[$this->position];
  148. } else {
  149. return NULL;
  150. }
  151. }
  152. /**
  153. * Moves the pointer to the first result.
  154. * @return void
  155. * @access public
  156. */
  157. function moveFirst() {
  158. $this->position = 0;
  159. }
  160.  
  161. /**
  162. * Moves the pointer to the last result.
  163. * @return void
  164. * @access public
  165. */
  166. function moveLast() {
  167. $this->position = count($this->RdqlResult) - 1;
  168. }
  169. /**
  170. * Moves the pointer to a specific result.
  171. * If you set an off-bounds value, next(), previous() and current() will return NULL
  172. * @return void
  173. * @access public
  174. */
  175. function moveTo($position) {
  176. $this->position = $position;
  177. }
  178. /**
  179. * Returns the current position of the iterator.
  180. * @return integer
  181. * @access public
  182. */
  183. function getCurrentPosition() {
  184. return $this->position;
  185. }
  186. }
  187.  
  188. ?>

Documentation generated on Fri, 13 Jan 2006 07:49:23 +0100 by phpDocumentor 1.3.0RC4