Source for file RdqlResultIterator.php

Documentation is available at RdqlResultIterator.php

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

Documentation generated on Fri, 17 Dec 2004 16:17:44 +0100 by phpDocumentor 1.3.0RC3