Source for file StatementIterator.php

Documentation is available at StatementIterator.php

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

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