Source for file ResContainer.php

Documentation is available at ResContainer.php

  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. * <BR><BR>History:<UL>
  12. * <LI>10-01-2004 : First version of this class.</LI>
  13. *
  14. * @version V0.9.3
  15. * @author Daniel Westphal <mail at d-westphal dot de>
  16. *
  17. * @package resModel
  18. * @access public
  19. ***/
  20.  
  21. class ResContainer extends ResResource
  22. {
  23. /**
  24. * Holds a ResResource of this container type rdf:seq, rdf:alt, or rdf:bag
  25. * @var ResResource
  26. * @access private
  27. */
  28. var $containerType;
  29. /**
  30. * Constructor
  31. * You can supply a URI
  32. *
  33. * @param string $uri
  34. * @access public
  35. */
  36. function ResContainer($uri = null)
  37. {
  38. parent::ResResource($uri);
  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. * Determine whether the container contains a value
  59. *
  60. * @param obejct ResResource/ResLiteral $resResource
  61. * @return boolean
  62. * @access public
  63. */
  64. function contains($resResource)
  65. {
  66. //get all container''s properties
  67. foreach ($this->listProperties() as $statement)
  68. {
  69. //if the property matches a container membership property
  70. if ($this->_predicateLabelMatchesMembershipProperty($statement->getLabelPredicate()))
  71. {
  72. //check, if it''s the value, we''re looking for.
  73. if ($resResource->equals($statement->getObject()))
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. /**
  80. * Returns true, if this resource is a container from type rdf:alt
  81. *
  82. * @return boolean
  83. * @access public
  84. */
  85. function isAlt()
  86. {
  87. return ($this->containerType->getURI()==RDF_NAMESPACE_URI.RDF_ALT);
  88. }
  89. /**
  90. * Returns true, if this resource is a container from type rdf:bag
  91. *
  92. * @return boolean
  93. * @access public
  94. */
  95. function isBag()
  96. {
  97. return ($this->containerType->getURI()==RDF_NAMESPACE_URI.RDF_BAG);
  98. }
  99.  
  100. /**
  101. * Returns true, if this resource is a container from type rdf:seq
  102. *
  103. * @return boolean
  104. * @access public
  105. */
  106. function isSeq()
  107. {
  108. return ($this->containerType->getURI()==RDF_NAMESPACE_URI.RDF_SQE);
  109. }
  110. /**
  111. * Get an array of all resources that are values of this container
  112. *
  113. * @return array
  114. * @access public
  115. */
  116. function getMembers()
  117. {
  118. $return=array();
  119. foreach ($this->listProperties() as $statement)
  120. {
  121. $predicateLabel=$statement->getLabelPredicate();
  122. if ($this->_predicateLabelMatchesMembershipProperty($predicateLabel))
  123. {
  124. $return[$this->_getMemberIndexNrFromMembershipPropertyLabel($predicateLabel)] = $statement->getObject();
  125. }
  126. }
  127. return $return;
  128. }
  129. /**
  130. * Remove a value from the container.
  131. *
  132. * Once removed, the values in the container with a higher ordinal value are renumbered.
  133. * The renumbering algorithm depends on the type of container.
  134. *
  135. * @param obejct ResResource/ResLiteral $resResource
  136. * @access public
  137. */
  138. function remove($object)
  139. {
  140. $deleteFromIndex=array();
  141. //get all container members
  142. $memberIndex=$this->getMembers();
  143. //check each container member if it equals the resoure to be removed
  144. foreach ($memberIndex as $key => $value)
  145. {
  146. //save the statements positio in the container
  147. if($object->equals($value))
  148. $deleteFromIndex[]=$key;
  149. }
  150.  
  151. //delete all found container members
  152. foreach ($deleteFromIndex as $index)
  153. {
  154. $this->removeAll($this->_getMembershipPropertyWithIndex($index));
  155.  
  156. //renumber all members with higher ordinal numbers than the deleted one
  157. for ($i = $index;$i < count($memberIndex); $i++)
  158. {
  159. $this->removeAll($this->_getMembershipPropertyWithIndex($i+1));
  160. $this->addProperty($this->_getMembershipPropertyWithIndex($i),$memberIndex[$i+1]);
  161. }
  162. }
  163. }
  164. /**
  165. * Returns the number values in the container.
  166. *
  167. * @return integer
  168. * @access public
  169. */
  170. function size()
  171. {
  172. return count($this->getMembers());
  173. }
  174. /**
  175. * Checks, if a predicate label fits a container membership property rdf:_n
  176. *
  177. * @param string $predicateLabel
  178. * @return boolean
  179. * @access private
  180. */
  181. function _predicateLabelMatchesMembershipProperty($predicateLabel)
  182. {
  183. return substr($predicateLabel,0,strlen(RDF_NAMESPACE_URI.''_'')) == RDF_NAMESPACE_URI.''_'';
  184. }
  185. /**
  186. * Get the ordinal number from a membership property rdf:_n
  187. *
  188. * @param string $predicateLabel
  189. * @return integer
  190. * @access private
  191. */
  192. function _getMemberIndexNrFromMembershipPropertyLabel($predicateLabel)
  193. {
  194. return (int)substr($predicateLabel,strlen(RDF_NAMESPACE_URI.''_''));
  195. }
  196. /**
  197. * Get a membership property rdf:_n with index $int
  198. *
  199. * @param intger $int
  200. * @return string
  201. * @access private
  202. */
  203. function _getMembershipPropertyWithIndex($int)
  204. {
  205. return new ResResource(RDF_NAMESPACE_URI.''_''.$int);
  206. }
  207. }
  208. ?>

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