TripalWebServiceResource.inc 7.2 KB

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