Source for file StatementIterator.php

Documentation is available at StatementIterator.php

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

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