source: Dev/branches/rest-dojo-ui/server/rdfapi/util/IterFind.php @ 289

Last change on this file since 289 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 3.3 KB
Line 
1<?php
2
3// ----------------------------------------------------------------------------------
4// Class: IterFind
5// ----------------------------------------------------------------------------------
6
7
8/**
9* Implementation of a find-iterator which delivers statements or quads.
10*
11* This Iterator should be used in a for-loop like:
12* for($iterator = $memmodel->iterFind(null,null,null,null); $iterator->valid(); $iterator->next())
13* {
14*       $statement=$iterator->current();
15* };
16*
17* @version  $Id$
18* @author Daniel Westphal (http://www.d-westphal.de)
19*
20*
21* @package      utility
22* @access       public
23**/
24class IterFind
25{
26       
27        /**
28        * The current position
29        * @var          integer
30        * @access       private
31        */
32        var $key;
33       
34        /**
35        * boolean value, if the results should be returned as Quads
36        * @var          boolean
37        * @access       private
38        */
39        var $returnAsQuads;
40       
41        /**
42        * If the current resource is valid
43        * @var          boolean
44        * @access       private
45        */
46        var $valid;
47       
48        /**
49        * The current NamedGraph
50        * @var obejct NamedGraph
51        * @access       private
52        */
53        var $current;
54       
55        /**
56        * The graph to look in.
57        * @var string
58        * @access       private
59        */     
60        var $findGraph;
61       
62        /**
63        * The subject Resource to search for
64        * @var string
65        * @access       private
66        */     
67        var $findSubject;
68       
69        /**
70        * The predicate Resource to search for
71        * @var string
72        * @access       private
73        */     
74        var $findPredicate;
75       
76        /**
77        * The object Resource to search for
78        * @var string
79        * @access       private
80        */     
81        var $findObject;
82       
83       
84       
85        /**
86    * Constructor.
87    *
88        * $subject, $predicate, and $object are used like find().
89        * $graph has to be a reference to the graph to search in.
90        *
91        *
92    *
93    * @param $graph Resource
94    * @param $subject Resource
95    * @param $predicate Resource
96    * @param $object Resource
97    * @param $returnAsQuads boolean
98        * @access       public
99    */
100        function IterFind($graph,$subject,$predicate,$object,$returnAsQuads=false)
101        {
102                if ($graph==NULL)
103                {
104                        $this->valid=false;
105                        return;
106                }               
107                $this->findGraph=&$graph;
108                $this->findSubject=$subject;
109                $this->findPredicate=$predicate;
110                $this->findObject=$object;
111                $this->rewind();
112                $this->returnAsQuads=$returnAsQuads;
113        }
114       
115        /**
116    * Resets iterator list to start
117    *
118        * @access       public
119    */
120        function rewind()
121        {
122                $this->key = -1;
123                $this->next();
124        }
125       
126        /**
127    * Says if there are additional items left in the list
128    *
129    * @return   boolean
130        * @access       public
131    */
132        function valid()
133        {
134                return $this->valid;
135        }
136       
137        /**
138    * Moves Iterator to the next item in the list
139    *
140        * @access       public
141    */
142        function next()
143        {
144                $this->current = $this->findGraph->findFirstMatchingStatement($this->findSubject,$this->findPredicate,$this->findObject,++$this->key);
145                $this->valid=($this->current!=NULL);
146        }
147       
148        /**
149    * Returns the current item
150    *
151    * @return   mixed
152        * @access       public
153    */
154        function current()
155        {
156                if($this->returnAsQuads)
157                return new Quad(new Resource($this->findGraph->getGraphName()),$this->current->getSubject(),$this->current->getPredicate(),$this->current->getObject());
158                //else
159                return $this->current;
160        }
161       
162       
163        /**
164    * Returns the key of the current item
165    *
166    * @return   integer
167        * @access       public
168    */
169        function key()
170        {
171                return $this->key;
172        }
173}
174?>
Note: See TracBrowser for help on using the repository browser.