source: Dev/branches/rest-dojo-ui/Demo/rdfapi/resModel/ResContainer.php @ 312

Last change on this file since 312 was 312, checked in by jkraaijeveld, 13 years ago
File size: 5.5 KB
Line 
1<?php
2// ----------------------------------------------------------------------------------
3// Class: ResContainer
4// ----------------------------------------------------------------------------------
5
6/**
7* An RDF Container.
8* This Class defines methods for accessing RDF container resources.
9* These methods operate on the RDF statements contained in a model.
10*
11* @version  $Id: ResContainer.php 320 2006-11-21 09:38:51Z tgauss $
12* @author Daniel Westphal <mail at d-westphal dot de>
13*
14* @package      resModel
15* @access       public
16**/
17
18class ResContainer extends ResResource
19{
20        /**
21        * Holds a ResResource of this container type rdf:Seq, rdf:Alt, or rdf:Bag
22        * @var          ResResource
23        * @access       private
24        */
25        var $containerType;
26       
27       
28        /**
29    * Constructor
30        * You can supply a URI
31    *
32    * @param string $uri
33        * @access       public
34    */ 
35        function ResContainer($uri = null)
36        {
37                parent::ResResource($uri);
38        }       
39       
40        /**
41        * Add a new value to a container.
42        * The new value is added as the last element of the container.
43        *
44        * @param        object ResResource/ResLiteral   $object
45        * @access       public
46        */
47        function add($object)
48        {
49                //type this container, if it isn't already typed
50                if(!$this->hasProperty(new ResResource(RDF_NAMESPACE_URI.RDF_TYPE)))
51                        $this->addProperty(new ResResource(RDF_NAMESPACE_URI.RDF_TYPE),$this->containerType);
52                //get the current size
53                $actualSize=$this->size();
54                //add the object to the last position
55                $this->addProperty(new ResResource(RDF_NAMESPACE_URI.'_'.($actualSize+1)),$object);
56        }
57       
58        /**
59        * Determine whether the container contains a value
60        *
61        * @param        obejct ResResource/ResLiteral   $resResource
62        * @return       boolean
63        * @access       public
64        */
65        function contains($resResource)
66        {
67                //get all container's properties
68                foreach ($this->listProperties() as $statement)
69                {
70                        //if the property matches a container membership property
71                        if ($this->_predicateLabelMatchesMembershipProperty($statement->getLabelPredicate()))
72                        {
73                                //check, if it's the value, we're looking for.
74                                if ($resResource->equals($statement->getObject()))
75                                        return true;
76                        }       
77                }
78                return false;
79        }
80       
81        /**
82        * Returns true, if this resource is a container from type rdf:Alt
83        *
84        * @return       boolean
85        * @access       public
86        */     
87        function isAlt()
88        {
89                return ($this->containerType->getURI()==RDF_NAMESPACE_URI.RDF_ALT);
90        }
91       
92        /**
93        * Returns true, if this resource is a container from type rdf:Bag
94        *
95        * @return       boolean
96        * @access       public
97        */     
98        function isBag()
99        {
100                return ($this->containerType->getURI()==RDF_NAMESPACE_URI.RDF_BAG);
101        }
102
103        /**
104        * Returns true, if this resource is a container from type rdf:Seq
105        *
106        * @return       boolean
107        * @access       public
108        */     
109        function isSeq()
110        {
111                return ($this->containerType->getURI()==RDF_NAMESPACE_URI.RDF_SEQ);
112        }
113       
114        /**
115        * Get an array of all resources that are values of this container
116        *
117        * @return       array
118        * @access       public
119        */     
120        function getMembers()
121        {
122                $return=array();
123                foreach ($this->listProperties() as $statement)
124                {
125                        $predicateLabel=$statement->getLabelPredicate();
126                        if ($this->_predicateLabelMatchesMembershipProperty($predicateLabel))
127                        {
128                                $return[$this->_getMemberIndexNrFromMembershipPropertyLabel($predicateLabel)] = $statement->getObject();
129                        }       
130                }
131                return $return;
132        }
133       
134        /**
135        * Remove a value from the container.
136        *
137        * Once removed, the values in the container with a higher ordinal value are renumbered.
138        * The renumbering algorithm depends on the type of container.
139        *
140        * @param        obejct ResResource/ResLiteral   $resResource
141        * @access       public
142        */
143        function remove($object)
144        {
145                $deleteFromIndex=array();
146                //get all container members
147                $memberIndex=$this->getMembers();
148               
149                //check each container member if it equals the resoure to be removed
150                foreach ($memberIndex as $key => $value)
151                {
152                        //save the statements positio in the container
153                        if($object->equals($value))
154                                        $deleteFromIndex[]=$key;       
155                }
156
157                //delete all found container members
158                foreach ($deleteFromIndex as $index)
159                {
160                        $this->removeAll($this->_getMembershipPropertyWithIndex($index));
161
162                        //renumber all members with higher ordinal numbers than the deleted one
163                        for ($i = $index;$i < count($memberIndex); $i++)
164                        {
165                                $this->removeAll($this->_getMembershipPropertyWithIndex($i+1));
166                                $this->addProperty($this->_getMembershipPropertyWithIndex($i),$memberIndex[$i+1]);
167                        }               
168                }
169               
170        }
171       
172        /**
173        * Returns the number values in the container.
174        *
175        * @return       integer
176        * @access       public
177        */     
178        function size()
179        {
180                return count($this->getMembers());
181        }
182       
183        /**
184        * Checks, if a predicate label fits a container membership property rdf:_n
185        *
186        * @param        string  $predicateLabel
187        * @return       boolean
188        * @access       private
189        */
190        function _predicateLabelMatchesMembershipProperty($predicateLabel)
191        {
192                return substr($predicateLabel,0,strlen(RDF_NAMESPACE_URI.'_')) == RDF_NAMESPACE_URI.'_';
193        }
194       
195        /**
196        * Get the ordinal number from a membership property rdf:_n
197        *
198        * @param        string  $predicateLabel
199        * @return       integer
200        * @access       private
201        */
202        function _getMemberIndexNrFromMembershipPropertyLabel($predicateLabel)
203        {
204                return (int)substr($predicateLabel,strlen(RDF_NAMESPACE_URI.'_'));
205        }
206       
207        /**
208        * Get a membership property rdf:_n with index $int
209        *
210        * @param        intger  $int
211        * @return       string
212        * @access       private
213        */
214        function _getMembershipPropertyWithIndex($int)
215        {
216                return new ResResource(RDF_NAMESPACE_URI.'_'.$int);     
217        }
218}
219?>
Note: See TracBrowser for help on using the repository browser.