TripalWebServiceResource.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 $service_path
  29. *
  30. */
  31. public function __construct($service_path) {
  32. $this->context = [];
  33. $this->data = [];
  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['sw_url']);
  39. $vocab = tripal_get_vocabulary_details('hydra');
  40. $this->addContextItem('hydra', $vocab['sw_url']);
  41. $vocab = tripal_get_vocabulary_details('dc');
  42. $this->addContextItem('dc', $vocab['sw_url']);
  43. $vocab = tripal_get_vocabulary_details('schema');
  44. $this->addContextItem('schema', $vocab['sw_url']);
  45. $vocab = tripal_get_vocabulary_details('local');
  46. $this->addContextItem('local', url($vocab['sw_url'], ['absolute' => TRUE]));
  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. * This function should not be called directory. Rather, the
  54. * addContextTerm() and addContextVocab() functions built
  55. * into the TripalWebService class should be used as these will help
  56. * ensure terms are added proper for the context of the web service.
  57. *
  58. * @param $name
  59. * The term name.
  60. * @param $iri
  61. * The Internationalized Resource Identifiers or it's shortcut.
  62. *
  63. */
  64. public function addContextItem($name, $iri) {
  65. if (array_key_exists($name, $this->context)) {
  66. return;
  67. }
  68. $this->context[$name] = $iri;
  69. }
  70. /**
  71. * Removes a term for the '@context' section for this resource.
  72. *
  73. * @param $name
  74. * The term name.
  75. * @param $iri
  76. * The Internationalized Resource Identifiers or it's shortcut.
  77. */
  78. public function removeContextItem($name, $iri) {
  79. // TODO: make sure that if a shortcut is used that the parent is present.
  80. unset($this->context[$name]);
  81. }
  82. /**
  83. * Sets the resource type.
  84. *
  85. * The type exist in the context of the web service.
  86. *
  87. * @param $type
  88. * The type
  89. */
  90. public function setType($type) {
  91. $this->checkKey($type);
  92. $this->type = $type;
  93. $this->data['@type'] = $type;
  94. }
  95. /**
  96. * Checks a key to ensure it is in the Context before being used.
  97. *
  98. * This function should be used before adding a property or type to this
  99. * resource. It ensures that the key for the property is already in the
  100. * context.
  101. *
  102. * @param $key
  103. * The key to check.
  104. *
  105. * @throws Exception
  106. */
  107. private function checkKey($key) {
  108. // Make sure the key is already present in the context.
  109. $keys = array_keys($this->context);
  110. // Keys that are full HTML are acceptable
  111. if (preg_match('/^(http|https):\/\/.*/', $key)) {
  112. return;
  113. }
  114. // If the key has a colon separating the vocabulary and the term then we
  115. // just need to make sure that the vocabulary is present.
  116. $matches = [];
  117. if (preg_match('/^(.*?):(.*?)$/', $key, $matches)) {
  118. $vocab = $matches[1];
  119. $accession = $matches[2];
  120. // The underscore represents the blank node. So, these keys are okay.
  121. if ($vocab == '_') {
  122. return;
  123. }
  124. // If the vocabulary is not in the.
  125. if (!in_array($vocab, $keys)) {
  126. throw new Exception(t("The key, !key, has a vocabulary that has not yet been added to the " .
  127. "context. Use the addContextItem() function to add the vocabulary prior to adding a value for it.", ['!key' => $key]));
  128. }
  129. }
  130. else {
  131. // If the key is not in the context then throw an error.
  132. if (!in_array($key, $keys)) {
  133. throw new Exception(t("The key, !key, has not yet been added to the " .
  134. "context. Use the addContextItem() function to add this key prior to adding a value for it.", ['!key' => $key]));
  135. }
  136. }
  137. }
  138. /**
  139. * Checks the value to make sure there are no problems with.
  140. *
  141. * Will also expand any TriaplWebServiceResource by adding their
  142. * context to this resource and substitute their data in place of the
  143. * value.
  144. *
  145. * @param $value
  146. *
  147. */
  148. private function checkValue(&$value) {
  149. if (is_array($value)) {
  150. foreach ($value as $k => $v) {
  151. // If this is an integer then this is a numeric indexed array
  152. // and we can just check the value. If not, then make sure the
  153. // key has been added to the context.
  154. if (preg_match('/^\d+$/', $k)) {
  155. $this->checkValue($value[$k]);
  156. }
  157. else {
  158. if ($k != '@id' and $k != '@type') {
  159. $this->checkKey($k);
  160. }
  161. $this->checkValue($value[$k]);
  162. }
  163. }
  164. }
  165. else {
  166. // If this is a TripalWebServiceResource then get it's data and use that
  167. // as the new value. Also, add in the elements context to this resource.
  168. if (is_a($value, 'TripalWebServiceResource') or is_subclass_of($value, 'TripalWebServiceResource')) {
  169. $context = $value->getContext();
  170. foreach ($context as $k => $v) {
  171. $this->addContextItem($k, $v);
  172. }
  173. $value = $value->getData();
  174. }
  175. }
  176. }
  177. /**
  178. * Set's the unique identifier for the resource.
  179. *
  180. * Every resource must have a unique Idientifer. In JSON-LD the '@id' element
  181. * identifies the IRI for the resource which will include the unique
  182. * identifier. The TraiplWebService to which a resource is added will
  183. * build the IRIs but it needs the unique ID of each resource.
  184. *
  185. * @param $id
  186. * The unique identifier for the resource.
  187. */
  188. public function setID($id) {
  189. $this->id = $id;
  190. $this->data['@id'] = $this->getURI($id);
  191. }
  192. /**
  193. * Retrieves the IRI for an entity of a given ID in this web service.
  194. *
  195. * @param $id
  196. * The unique identifier for the resource.
  197. */
  198. public function getURI($id) {
  199. // If this is a key/value pair for an id and if the vocab portion is
  200. // an underscore then this represents a blank node and we don't want
  201. // to add the full path.
  202. $matches = [];
  203. if (preg_match('/^(.*?):(.*?)$/', $id, $matches)) {
  204. $vocab = $matches[1];
  205. if ($vocab == '_') {
  206. return $id;
  207. }
  208. return $id;
  209. }
  210. else {
  211. return $this->service_path . '/' . $id;
  212. }
  213. }
  214. /**
  215. * Retrieves the unique identifier for this resource.
  216. *
  217. * Every resource must have a unique Idientifer. In JSON-LD the '@id' element
  218. * identifies the IRI for the resource which will include the unique
  219. * identifier. The TraiplWebService to which a resource is added will
  220. * build the IRIs but it needs the unique ID of each resource.
  221. *
  222. * @return
  223. * The unique identifier for the resource.
  224. */
  225. public function getID() {
  226. return $this->id;
  227. }
  228. /**
  229. * Retrieves the type of this resource.
  230. *
  231. * @return
  232. * The name of the resource.
  233. */
  234. public function getType() {
  235. return $this->type;
  236. }
  237. /**
  238. * Adds a new key/value pair to the web serivces response.
  239. *
  240. * The value must either be a scalar or another TripalWebServiceResource
  241. * object. If the same key is usesd multiple times then the resulting
  242. * resource will be presented as an array of elements.
  243. *
  244. * @param unknown $key
  245. * The name of the $key to add. This key must already be present in the
  246. * web service context by first adding it using the addContextItem()
  247. * member function.
  248. * @param unknown $value
  249. * The value of the key which must either be a scalar or a
  250. * TripalWebServiceResource instance.
  251. */
  252. public function addProperty($key, $value) {
  253. $this->checkKey($key);
  254. $this->checkValue($value);
  255. // If this is a numeric keyed array then add each item.
  256. if (is_array($value) and count(array_filter(array_keys($value), 'is_int')) == count(array_keys($value))) {
  257. if (!array_key_exists($key, $this->data)) {
  258. $this->data[$key] = [];
  259. }
  260. foreach ($value as $item) {
  261. $this->addProperty($key, $item);
  262. }
  263. return;
  264. }
  265. // If this key doesn't exist then add the value directly to the key.
  266. if (!array_key_exists($key, $this->data)) {
  267. $this->data[$key] = $value;
  268. }
  269. // Else if it does exist then we need to make sure that the element is
  270. // an array and add it.
  271. else {
  272. // If this is the second element, then convert it to an array. The
  273. // second test in the condition below is for for a numeric array.
  274. if (!is_array($this->data[$key]) or count(array_filter(array_keys($this->data[$key]), 'is_string')) > 0) {
  275. $element = $this->data[$key];
  276. $this->data[$key] = [];
  277. $this->data[$key][] = $element;
  278. }
  279. $this->data[$key][] = $value;
  280. }
  281. }
  282. /**
  283. * Retrieves the value of a property.
  284. *
  285. * @param $key
  286. * The name of the property.
  287. *
  288. * @param
  289. * The value of the property. This could be a scalar, array or
  290. * a TripalWebService object.
  291. */
  292. function getProperty($key) {
  293. return $this->data[$key];
  294. }
  295. /**
  296. * Retrieves the data section of the resource.
  297. *
  298. * The JSON-LD response constists of two sections the '@context' section
  299. * and the data section. This function only returns the data section
  300. * for this resource
  301. *
  302. * @return
  303. * An associative array containing the data section of the response.
  304. */
  305. public function getData() {
  306. return $this->data;
  307. }
  308. /**
  309. * Retrieves the data section of the resource.
  310. *
  311. * The JSON-LD response constists of two sections the '@context' section
  312. * and the data section. This function only returns the data section
  313. * for this resource
  314. *
  315. * @return
  316. * An associative array containing the data section of the response.
  317. */
  318. public function getContext() {
  319. return $this->context;
  320. }
  321. /**
  322. * Copies the context from a given TripalWebService resource to this
  323. * resource.
  324. *
  325. * @param $resource
  326. */
  327. public function setContext($resource) {
  328. if (!is_a($resource, 'TripalWebServiceResource')) {
  329. throw new Exception("The \$resource argument provided to the TripalWebServiceResource::setContext() function must be an instance of a TripalWebServiceResource.");
  330. }
  331. $this->context = $resource->getContext();
  332. }
  333. }