source: Dev/branches/rest-dojo-ui/Demo/rdfapi/util/StatementIterator.php @ 312

Last change on this file since 312 was 312, checked in by jkraaijeveld, 13 years ago
File size: 4.5 KB
Line 
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 * @version  $Id: StatementIterator.php 268 2006-05-15 05:28:09Z tgauss $
13 * @author Daniel Westphal <mail at d-westphal dot de>
14 * @author Chris Bizer <chris@bizer.de>
15 *
16 * @package utility
17 * @access      public
18 *
19 */
20 class StatementIterator extends Object {
21
22        /**
23        * Reference to the MemModel
24        * @var          object MemModel
25        * @access       private
26        */     
27    var $model;
28
29        /**
30        * Current position
31        * StatementIterator does not use the build in PHP array iterator,
32        * so you can use serveral iterators on a single MemModel.
33        * @var          integer
34        * @access       private
35        */     
36    var $position;
37   
38 
39   /**
40    * Constructor
41    *
42    * @param    object  MemModel
43        * @access       public
44    */
45    function StatementIterator(&$model) {
46                $this->model = $model;
47                reset($this->model->triples);
48                $this->position = -1;
49        }
50 
51  /**
52   * Returns TRUE if there are more statements.
53   * @return    boolean
54   * @access    public 
55   */
56  function hasNext() {
57                if ($this->position == -1)
58                {
59                        if(current($this->model->triples)!=NULL)
60                        {
61                                return TRUE;   
62                        } else
63                        {
64                                return FALSE;
65                        }
66                };
67
68                if (next($this->model->triples)!=NULL )
69                {       
70                        prev($this->model->triples);           
71                        return TRUE;
72                } else
73                {
74                        end($this->model->triples);
75                        return FALSE;
76                };
77   }
78
79  /**
80   * Returns TRUE if the first statement has not been reached.
81   * @return    boolean
82   * @access    public 
83   */
84  function hasPrevious() {
85                if (prev($this->model->triples)!=NULL)
86                {
87                        next($this->model->triples);
88                        return TRUE;
89                } else {
90                        reset($this->model->triples);
91                        return FALSE;
92                };
93  }     
94   
95  /**
96   * Returns the next statement.
97   * @return    statement or NULL if there is no next statement.
98   * @access    public 
99   */
100  function next() {
101                if ($this->position == -1)
102                {
103                        if($return=current($this->model->triples))
104                        {
105                                $this->position=key($this->model->triples);
106                                return $return;
107                        } else
108                        {
109                                $this->position=key($this->model->triples);
110                                return NULL;
111                        };
112                };
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       
147                if ($return=current($this->model->triples)) {
148                        return $return;
149                } else {
150                        return NULL;
151                }
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     /**
175   * Moves the pointer to a specific statement.
176   * If you set an off-bounds value, the position will be set to the last element
177   * @param     integer
178   * @return    void
179   * @access    public 
180   */
181  function moveTo($position) {
182       
183                        if ($position + 1 > count($this->model->triples))
184                        {
185                                end($this->model->triples);
186                        } else
187                        {
188                                reset($this->model->triples);
189                               
190                                for ($i = 1; $i < $position; $i++) {
191                                   next($this->model->triples);
192                                }
193                        };
194                        $this->position=key($this->model->triples);
195   }
196   
197     /**
198   * Returns the current position of the iterator.
199   * @return    integer
200   * @access    public 
201   */
202  function getCurrentPosition() {
203                        return key($this->model->triples);
204   }
205 
206}
207
208?>
Note: See TracBrowser for help on using the repository browser.