TripalWebServiceResource.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. class TripalWebServiceResource {
  3. /**
  4. * The unique identifier for this resource.
  5. */
  6. protected $id;
  7. /**
  8. * The unique type of resource. The type must exists in the
  9. * context for the web service.
  10. */
  11. protected $type;
  12. /**
  13. * The JSON-LD compatible context for this resource.
  14. */
  15. protected $context;
  16. /**
  17. * Holds the data portion of the JSON-LD response for this resource.
  18. */
  19. protected $data;
  20. /**
  21. * The URL path that the service is providing to access this resource.
  22. * This path plus the $id are concatenated to form the IRI for this resource.
  23. */
  24. protected $service_path;
  25. /**
  26. * Implements the constructor.
  27. *
  28. * @param TripalWebService $service
  29. * An instance of a TripalWebService or class that extends it.
  30. */
  31. public function __construct($service_path) {
  32. $this->context = array();
  33. $this->data = array();
  34. $this->service_path = $service_path;
  35. // First, add the RDFS and Hydra vocabularies to the context. All Tripal
  36. // web services should use these.
  37. $vocab = tripal_get_vocabulary_details('rdfs');
  38. $this->addContextItem('rdfs', $vocab['url']);
  39. $vocab = tripal_get_vocabulary_details('hydra');
  40. $this->addContextItem('hydra', $vocab['url']);
  41. $vocab = tripal_get_vocabulary_details('dc');
  42. $this->addContextItem('dc', $vocab['url']);
  43. $vocab = tripal_get_vocabulary_details('schema');
  44. $this->addContextItem('schema', $vocab['url']);
  45. $vocab = tripal_get_vocabulary_details('local');
  46. $this->addContextItem('local', $vocab['url']);
  47. $this->data['@id'] = $service_path;
  48. $this->data['@type'] = '';
  49. }
  50. /**
  51. * Adds a term to the '@context' section for this resource.
  52. *
  53. * @param $name
  54. * The term name.
  55. * @param $iri
  56. * The Internationalized Resource Identifiers or it's shortcut.
  57. */
  58. public function addContextItem($name, $iri) {
  59. // TODO: make sure that if a shortcut is used that the parent is present.
  60. $this->context[$name] = $iri;
  61. }
  62. /**
  63. * Removes a term for the '@context' section for this resource.
  64. *
  65. * @param $name
  66. * The term name.
  67. * @param $iri
  68. * The Internationalized Resource Identifiers or it's shortcut.
  69. */
  70. public function removeContextItem($name, $iri) {
  71. // TODO: make sure that if a shortcut is used that the parent is present.
  72. unset($this->context[$name]);
  73. }
  74. /**
  75. * Sets the resource type.
  76. *
  77. * The type exist in the context of the web service.
  78. *
  79. * @param $type
  80. * The type
  81. */
  82. public function setType($type) {
  83. $keys = array_keys($this->context);
  84. if (!in_array($type, $keys)) {
  85. throw new Exception("The resource type, '$type', has not yet been added to the " .
  86. "context of the web service. Use the addContextItem() function of the web service " .
  87. "to add this term.");
  88. }
  89. $this->type = $type;
  90. $this->data['@type'] = $type;
  91. }
  92. /**
  93. * Set's the unique identifier for the resource.
  94. *
  95. * Every resource must have a unique Idientifer. In JSON-LD the '@id' element
  96. * identifies the IRI for the resource which will include the unique
  97. * identifier. The TraiplWebService to which a resource is added will
  98. * build the IRIs but it needs the unique ID of each resource.
  99. *
  100. * @param $id
  101. * The unique identifier for the resource.
  102. */
  103. public function setID($id) {
  104. $this->id = $id;
  105. $this->data['@id'] = $this->service_path . '/' . $id;
  106. }
  107. /**
  108. * Retrieves the unique identifier for this resource.
  109. *
  110. * Every resource must have a unique Idientifer. In JSON-LD the '@id' element
  111. * identifies the IRI for the resource which will include the unique
  112. * identifier. The TraiplWebService to which a resource is added will
  113. * build the IRIs but it needs the unique ID of each resource.
  114. *
  115. * @return
  116. * The unique identifier for the resource.
  117. */
  118. public function getID() {
  119. return $this->id;
  120. }
  121. /**
  122. * Retreives the type of this resource.
  123. *
  124. * @return
  125. * The name of the resource.
  126. */
  127. public function getType() {
  128. return $this->type;
  129. }
  130. /**
  131. * Adds a new key/value pair to the web serivces response.
  132. *
  133. * The value must either be a scalar or another TripalWebServiceResource
  134. * object.
  135. *
  136. * @param unknown $key
  137. * The name of the $key to add. This key must already be present in the
  138. * web service context by first adding it using the addContextItem()
  139. * member function.
  140. * @param unknown $value
  141. * The value of the key which must either be a scalar or a
  142. * TripalWebServiceResource instance.
  143. */
  144. public function addProperty($key, $value) {
  145. // Make sure the key is already present in the context.
  146. $keys = array_keys($this->context);
  147. if (!in_array($key, $keys)) {
  148. throw new Exception("The key, '$key', has not yet been added to the " .
  149. "context. Use the addContextItem() function to add this key prior to adding a value for it.");
  150. }
  151. if (is_scalar($value)) {
  152. $this->data[$key] = $value;
  153. }
  154. else if (!is_subclass_of($value, 'TripalWebServiceResource')) {
  155. $this->data[$key] = $value;
  156. }
  157. else {
  158. throw new Exception("The value must either be a scalar or a TripalWebServiceResource");
  159. }
  160. }
  161. /**
  162. * A recursive function that ensures all keys in an item are in the context.
  163. *
  164. * @param $key
  165. * The name of the current key.
  166. * @param $value
  167. * The avlue assigned to the current key.
  168. *
  169. * @throws Exception
  170. * Throws an exception of a key is not present in the context.
  171. */
  172. private function checkDataItem($key, $value) {
  173. // Make sure the key is already present in the context.
  174. $keys = array_keys($this->context);
  175. if (!in_array($key, $keys)) {
  176. throw new Exception("The key, '$key', has not yet been added to the " .
  177. "context. Use the addContextItem() function to add this key prior to adding a value for it.");
  178. }
  179. // If the value is an associative array then recurse
  180. if (is_array($value)) {
  181. // Check if this is an associatave array (non-integer keys).
  182. if (count(array_filter(array_keys($array), 'is_string')) > 0) {
  183. foreach ($value as $sub_key => $sub_value) {
  184. $this->checkDataItem($sub_key, $sub_value);
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * Retrieves the data section of the resource.
  191. *
  192. * The JSON-LD response constists of two sections the '@context' section
  193. * and the data section. This function only returns the data section
  194. * for this resource
  195. *
  196. * @return
  197. * An associative array containing the data section of the response.
  198. */
  199. public function getData() {
  200. return $this->data;
  201. }
  202. /**
  203. * Retrieves the data section of the resource.
  204. *
  205. * The JSON-LD response constists of two sections the '@context' section
  206. * and the data section. This function only returns the data section
  207. * for this resource
  208. *
  209. * @return
  210. * An associative array containing the data section of the response.
  211. */
  212. public function getContext() {
  213. return $this->context;
  214. }
  215. /**
  216. * Copies the context from a given TripalWebService resource to this
  217. * resource.
  218. *
  219. * @param $resource
  220. */
  221. public function setContext($resource) {
  222. if (!is_a($resource, 'TripalWebServiceResource')) {
  223. throw new Exception("The \$resource argument must be an instance of a TripalWebServiceResource.");
  224. }
  225. $this->context = $resource->getContext();
  226. }
  227. }