TripalWebServiceResource.inc 6.7 KB

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