TripalWebServiceResource.inc 6.3 KB

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