Source for file ResContainer.php

Documentation is available at ResContainer.php

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

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