TripalWebServiceResource.inc 11 KB

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