TripalWebService.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. <?php
  2. class TripalWebService {
  3. // --------------------------------------------------------------------------
  4. // EDITABLE STATIC CONSTANTS
  5. //
  6. // The following constants SHOULD be set for each descendent class. They are
  7. // used by the static functions to provide information to Drupal about
  8. // the field and it's default widget and formatter.
  9. // --------------------------------------------------------------------------
  10. /**
  11. * The human-readable label for this web service.
  12. */
  13. public static $label = 'Base WebService';
  14. /**
  15. * A bit of text to describe what this service provides.
  16. */
  17. public static $description = 'This is the base class for Tripal web services as is not meant to be used on it\'s own';
  18. /**
  19. * A machine-readable type for this service. This name must be unique
  20. * among all Tripal web services and is used to form the URL to access
  21. * this service.
  22. */
  23. public static $type = 'services';
  24. // --------------------------------------------------------------------------
  25. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  26. // --------------------------------------------------------------------------
  27. /**
  28. * The resource that will be returned by the webservice given the
  29. * arguments provided. This is a private
  30. */
  31. protected $resource;
  32. /**
  33. * An array containing the elements of the URL path. Each level of the
  34. * URL appears in a separate element of the array. The service type and
  35. * version are automatically removed from the array.
  36. */
  37. protected $path;
  38. /**
  39. * The set of paramters provided to the sesrvice. These are the values
  40. * that would occur in a URL after the question mark in an HTTP GET or
  41. * the data items of an HTTP POST.
  42. */
  43. protected $params;
  44. /**
  45. * The URL at which Tripal web services are found. This is used
  46. * for creating the IRI for resources.
  47. */
  48. protected $base_path;
  49. /**
  50. * The list of documented classes used by this service. For the web service
  51. * to be discoverable all of the entity classes and their collections
  52. * must be defined.
  53. */
  54. protected $documentation;
  55. // --------------------------------------------------------------------------
  56. // CONSTRUCTORS
  57. // --------------------------------------------------------------------------
  58. /**
  59. * Implements the constructor.
  60. */
  61. public function __construct($base_path) {
  62. if (!$base_path) {
  63. throw new Exception('Please provide a $base_path argument when creating a new TripalWebService.');
  64. }
  65. // Create a default resource so that the service always some something.
  66. $this->resource = new TripalWebServiceResource($base_path);
  67. // Intialize the private members variables.
  68. $this->path = array();
  69. $this->params = array();
  70. $this->base_path = $base_path;
  71. $this->documentation = array();
  72. $this->addDocClass(array(
  73. "id" => "http://www.w3.org/ns/hydra/core#Resource",
  74. "name" => 'resource',
  75. "title" => "Resource",
  76. 'subClassOf' => NULL,
  77. ));
  78. }
  79. // --------------------------------------------------------------------------
  80. // OVERRIDEABLE FUNCTIONS
  81. // --------------------------------------------------------------------------
  82. /**
  83. * Responds to the request argument provided to the service.
  84. *
  85. * This function should be implemented by a TripalWebService child class.
  86. *
  87. */
  88. public function handleRequest() {
  89. // TODO: make sure the $this->path and $this->params are set before
  90. // continuing.
  91. }
  92. // --------------------------------------------------------------------------
  93. // CLASS FUNCTIONS -- DO NOT OVERRIDE
  94. // --------------------------------------------------------------------------
  95. /**
  96. * Sets the URL path for the resource being called.
  97. *
  98. * @param $path
  99. * An array containing the elements of the URL path. Each level of the
  100. * URL appears in a separate element of the array. The service type and
  101. * version are automatically removed from the array. For example, a
  102. * URL of the type http://localhost/web-services/content/v0.1/Gene/sequence
  103. * will result in a $path array containing the following:
  104. * @code
  105. * array(
  106. * 'Gene',
  107. * 'sequence',
  108. * );
  109. * @endcode
  110. *
  111. * @param unknown $path
  112. */
  113. public function setPath($path) {
  114. $this->path = $path;
  115. }
  116. /**
  117. * Sets the parameters for the resource.
  118. *
  119. * @param $params
  120. * The set of paramters provided to the sesrvice. These are the values
  121. * that would occur in a URL after the question mark in an HTTP GET or
  122. * the data items of an HTTP POST.
  123. */
  124. public function setParams($params) {
  125. $this->params = $params;
  126. }
  127. /**
  128. * Retrieves the version number for this web service.
  129. *
  130. * Each web service must have version number built into the name of the
  131. * class. The version number appears at the end of the class name, begins
  132. * with a lower-case 'v' and is followed by two numbers (major and minor) that
  133. * are separated by an underscore. This function identifies the version
  134. * from the class name and returns it here in a human-readable format.
  135. *
  136. * @param $sanitize
  137. * Set to TRUE to convert the period to underscore.
  138. *
  139. * @return
  140. * The version number for this web service.
  141. */
  142. public function getVersion($sanitize = FALSE) {
  143. $class = get_class($this);
  144. $major_version = '';
  145. $minor_version = '';
  146. if (preg_match('/v(\d+)_(\d+)$/', $class, $matches)) {
  147. $major_version = $matches[1];
  148. $minor_version = $matches[2];
  149. return 'v' . $major_version . '.' . $minor_version;
  150. }
  151. return '';
  152. }
  153. /**
  154. * Retrieves the context section of the response.
  155. *
  156. * The JSON-LD response constists of two sections the '@context' section
  157. * and the data section. This function only returns the context section
  158. * of the response.
  159. *
  160. * @return
  161. * An associative array containing the context section of the response.
  162. */
  163. public function getContext() {
  164. return $this->resource->getContext();
  165. }
  166. /**
  167. * Returns the full web service response.
  168. *
  169. * The response includes both the @context and data sections of the
  170. * JSON-LD response.
  171. *
  172. * @return
  173. * An associative array containing that can be converted to JSON.
  174. */
  175. public function getResponse() {
  176. $context = $this->resource ? $this->resource->getContext() : array();
  177. $type = $this->resource ? $this->resource->getType() : 'unknown';
  178. $json_ld = array(
  179. '@context' => $context,
  180. '@id' => '',
  181. '@type' => $type,
  182. );
  183. $data = $this->getData();
  184. return array_merge($json_ld, $data);
  185. }
  186. /**
  187. * Retreives the service URL for this service.
  188. */
  189. public function getServicePath() {
  190. $class = get_class($this);
  191. $version = $this->getVersion();
  192. $type = $class::$type;
  193. return $this->base_path . '/' . $type . '/' . $version;
  194. }
  195. /**
  196. * Retrieves the data section of the response.
  197. *
  198. * The JSON-LD response constists of two sections the '@context' section
  199. * and the data section. This function only returns the data section
  200. * of the response.
  201. *
  202. * @return
  203. * An associative array containing the data section of the response.
  204. */
  205. public function getData() {
  206. if ($this->resource) {
  207. return $this->resource->getData();
  208. }
  209. return array();
  210. }
  211. /**
  212. * Sets the resource to be returned by this web service.
  213. *
  214. * @param $resource.
  215. * An implementation of a TripalWebServiceResource.
  216. */
  217. public function setResource($resource) {
  218. // Make sure the $servcie provides is a TripalWebServcie class.
  219. if (!is_a($resource, 'TripalWebServiceResource')) {
  220. throw new Exception("Cannot add a new resource to this web service as it is not a TripalWebServiceResource.");
  221. }
  222. $this->resource = $resource;
  223. }
  224. /**
  225. * Set an error for the service.
  226. *
  227. * @param $message
  228. * The error message to report.
  229. */
  230. public function setError($message) {
  231. $this->resource = new TripalWebServiceResource($this->base_path);
  232. $this->resource->setID('error');
  233. $this->resource->addContextItem('error', 'rdfs:error');
  234. $this->resource->setType('error');
  235. $this->resource->addProperty('error', $message);
  236. }
  237. /**
  238. * Retrieves an array contining the supported classes.
  239. *
  240. * Supported classe are resources provided by this web services and the
  241. * operations supported by those classes.
  242. *
  243. * @return
  244. * An array of TripalWebServiceResource objects that follow the Hydra
  245. * documentation for documenting supported classes.
  246. */
  247. public function getDocumentation() {
  248. return $this->documentation;
  249. }
  250. /**
  251. * Defines an entity class for the web services.
  252. *
  253. * A class defines a resource including information about its properties
  254. * and the actions that can be performed. Each class as a unique @id,
  255. * title, type and description. The $details parameter is used to provide
  256. * this information. Additionally, a resource may support Create, Read
  257. * Update and Delete (CRUD) operations. Those are defined using the
  258. * $ops argument. Finally, resources may have properties (or fields). These
  259. * properties should be defined using the $props arugment.
  260. *
  261. * @param $details.
  262. * An array of key/value pairs providing the details for the class. Valid
  263. * keys include:
  264. * - id: The unique IRI for this class.
  265. * - name: a computer-readable name for this class (i.e. no spaces,
  266. * no special characters). This name is used to construct
  267. * type identifiers for operations.
  268. * - title: The title for the resource that this Class represents.
  269. * - description: (Optional). A description of the resource.
  270. * - subClassOf: (Optional). If this class is a subclass of another
  271. * class then the value should be the @id of the parent class. This
  272. * defaults to hydra:Resource. If no subClass is desired, set it
  273. * to NULL.
  274. * - type: (Optional). Indicates the type of class. Defaults to
  275. * hydra:Class
  276. * @param $ops
  277. * If the resource supports CRUD functionality then those functions should
  278. * be defined using this argument. This is an associative array with
  279. * the following keys: GET, POST, PUT, DELETE. These keys, if provided,
  280. * indicate that a resource of this type can be retrieved (GET),
  281. * created (POST), updated (PUT) or deleted (DELETE). The value for each
  282. * key should be an associative array that supports the following keys:
  283. * = type: the @id that determines the type of operation. This type
  284. * should be specific to the resource, and it need not be a term
  285. * from a real vocabulary. Use the '_:' prefix for the term. For
  286. * example, for an 'Event' resource with a GET method, an appropriate
  287. * type would be '_:event_retrieve'.
  288. * - label: A label that describes the operation in the
  289. * context of the resource.
  290. * - description: A description for the operation. Can be set to NULL
  291. * for no description.
  292. * - expects: The information expected by the Web API.
  293. * - returns: The @id of a Class that will be returned by
  294. * the operation. Set to NULL if there is no return value.
  295. * - statusCodes: An array of status codes that could be returned when
  296. * an error occurs. Each element of the array is an associative
  297. * array with two key/value pairs: 'code' and 'description'. Set the
  298. * 'code' to be the HTTP code that can be returned on error and the
  299. * 'description' to an error message appropriate for the error. For
  300. * example an HTTP 404 error means "Not Found". You can sepecify this
  301. * code and provide a description appropriate for the method and
  302. * class.
  303. * @param $props.
  304. * Defines the list of properties that the resource provides. This is an
  305. * array of properties where each property is an associative array
  306. * containing the following keys:
  307. * - type: The @id of the type of property. Alternatively, this can
  308. * be an instance of a TripalWebServiceResource when the property
  309. * must support operations such as a property of type hydra:Link.
  310. * - title: (Optional). The human-readable title of this property. If
  311. * this key is absent then the title will be set to the term's title.
  312. * - description: (Optional). A short description of this property. If
  313. * this key is absent then the description will be set to the term's
  314. * description.
  315. * - required: Set to TRUE to indicate if this property is a required
  316. * field when creating a new resource.
  317. * - readable: Set to TRUE to indicate that the client can retrieve the
  318. * property's value?
  319. * altered by an update.
  320. * - writeable: Set to TRUE if the client can alter the value of the
  321. * property.
  322. * - domain: the @id of the rdfs:domain.
  323. * - range: the @id of the rdfs:range.
  324. */
  325. protected function addDocClass($details = array(), $ops = array(), $props = array()) {
  326. $supported_class = new TripalWebServiceResource($this->getServicePath());
  327. // Add the context which all classes will need
  328. $supported_class->addContextItem('description', 'rdfs:comment');
  329. $supported_class->addContextItem('subClassOf', 'hydra:subClassOf');
  330. $supported_class->addContextItem('description', 'rdfs:comment');
  331. $supported_class->addContextItem('label', 'rdfs:label');
  332. // Set the Class ID.
  333. $class_id = $details['id'];
  334. $supported_class->setID($class_id);
  335. // Set the class Type.
  336. if (array_key_exists('type', $details)) {
  337. $supported_class->setType($details['type']);
  338. }
  339. else {
  340. $supported_class->setType('hydra:Class');
  341. }
  342. // Set title and description.
  343. $supported_class->addProperty('hydra:title', $details['title']);
  344. $supported_class->addProperty('hydra:description', array_key_exists('description', $details) ? $details['description'] : NULL);
  345. // Set the sub class.
  346. if (array_key_exists('subClassOf', $details)) {
  347. if ($details['subClassOf']) {
  348. $supported_class->addProperty('subClassOf', $details['subClassOf']);
  349. }
  350. }
  351. else {
  352. $supported_class->addProperty('subClassOf', 'hydra:Resource');
  353. }
  354. // Now add the supported operations.
  355. $class_ops = array();
  356. foreach ($ops as $op => $op_details) {
  357. $class_ops[] = $this->generateDocClassOp($supported_class, $op, $op_details);
  358. }
  359. $supported_class->addContextItem('supportedOperation', 'hydra:supportedOperation');
  360. $supported_class->addProperty('supportedOperation', $class_ops);
  361. // Now add in the supported proprerties.
  362. $class_props = array();
  363. foreach ($props as $prop) {
  364. $class_props[] = $this->generateDocClassProp($supported_class, $prop);
  365. }
  366. $supported_class->addContextItem('supportedProperty', 'hydra:supportedProperty');
  367. $supported_class->addProperty('supportedProperty', $class_props);
  368. $this->documentation[] = $supported_class;
  369. }
  370. /**
  371. * A helper function for the addClass() function for generating a property.
  372. */
  373. private function generateDocClassProp($supported_class, $prop) {
  374. $supported_class->addContextItem('property', array(
  375. "@id" => "hydra:property",
  376. "@type" => "@id"
  377. ));
  378. $supported_class->addContextItem('domain', array(
  379. "@id" => "rdfs:domain",
  380. "@type" => "@id"
  381. ));
  382. $supported_class->addContextItem('range', array(
  383. "@id" => "rdfs:range",
  384. "@type" => "@id"
  385. ));
  386. $supported_class->addContextItem('readable', 'hydra:readable');
  387. $supported_class->addContextItem('writeable', 'hydra:writeable');
  388. $supported_class->addContextItem('required', 'hydra:required');
  389. $class_prop = array(
  390. 'property' => $prop['type'],
  391. 'hydra:title' => $prop['title'],
  392. 'hydra:description' => array_key_exists('description', $prop) ? $prop['description'] : NULL,
  393. 'required' => array_key_exists('required', $prop) ? $prop['required'] : NULL,
  394. 'readable' => array_key_exists('readonly', $prop) ? $prop['readonly'] : NULL,
  395. 'writeable' => array_key_exists('writeonly', $prop) ? $prop['writeonly'] : NULL,
  396. );
  397. if (array_key_exists('domain', $prop)) {
  398. $class_prop['domain'] = $prop['domain'];
  399. }
  400. if (array_key_exists('range', $prop)) {
  401. $class_prop['range'] = $prop['range'];
  402. }
  403. if (array_key_exists('operations', $prop)) {
  404. $class_prop['supportedOperation'] = array();
  405. foreach ($prop['operations'] as $op => $op_details) {
  406. $class_prop['supportedOperation'][] = $this->generateOp($supported_class, $op, $op_details);
  407. }
  408. }
  409. return $class_prop;
  410. }
  411. /**
  412. * A helper function for the addClass() function for generating an operation.
  413. */
  414. private function generateDocClassOp($supported_class, $op, $op_details) {
  415. if ($op != 'GET' and $op != 'PUT' and $op != 'DELETE' and $op != 'POST') {
  416. throw new Exception(t('The method, @method, provided to the TripalWebService::addClass function is not an oppropriate operations.', array('@method' => $op)));
  417. }
  418. if (!array_key_exists('type', $op_details)) {
  419. throw new Exception(t('Please provide a type in the operations array passed to the TripalWebService::addClass() function: @details', array('@details' => print_r($op_details, TRUE))));
  420. }
  421. if (!array_key_exists('label', $op_details)) {
  422. throw new Exception(t('Please provide a label in the operations array passed to the TripalWebService::addClass() function: @details', array('@details' => print_r($op_details, TRUE))));
  423. }
  424. $class_op = new TripalWebServiceResource($this->base_path);
  425. $class_op->setID($op_details['type']);
  426. switch($op) {
  427. case 'GET':
  428. $class_op->setType('hydra:Operation');
  429. break;
  430. case 'POST':
  431. $class_op->setType('http://schema.org/AddAction');
  432. break;
  433. case 'DELETE':
  434. $class_op->setType('http://schema.org/DeleteAction');
  435. break;
  436. case 'PUT':
  437. $class_op->setType('http://schema.org/UpdateAction');
  438. break;
  439. }
  440. $class_op->addContextItem('method', 'hydra:method');
  441. $class_op->addContextItem('label', 'rdfs:label');
  442. $class_op->addContextItem('description', 'rdfs:comment');
  443. $class_op->addContextItem('expects', array(
  444. "@id" => "hydra:expects",
  445. "@type" => "@id"
  446. ));
  447. $class_op->addContextItem('returns', array(
  448. "@id" => "hydra:returns",
  449. "@type" => "@id"
  450. ));
  451. $class_op->addContextItem('statusCodes', 'hydra:statusCodes');
  452. $class_op->addContextItem('code', 'hydra:statusCode');
  453. $class_op->addProperty('method', $op);
  454. $class_op->addProperty('label', array_key_exists('label', $op_details) ? $op_details['label'] : 'Retrieves an instance of this resource');
  455. $class_op->addProperty('description', array_key_exists('description', $op_details) ? $op_details['description'] : NULL);
  456. $status_codes = array_key_exists('statusCodes', $op_details) ? $op_details['statusCodes'] : array();
  457. foreach ($status_codes as $code) {
  458. if (array_key_exists('code', $code) and array_key_exists('description', $code)) {
  459. $class_op->addProperty('statusCodes', $code);
  460. }
  461. else {
  462. throw new Exception(t('The status code provided to TripalWebService::addClass function is improperly formatted @code.', array('@code' => print_r($code, TRUE))));
  463. }
  464. }
  465. if (count($status_codes) == 0) {
  466. $class_op->addProperty('statusCodes', array());
  467. }
  468. $class_op->addProperty('expects', array_key_exists('expects', $op_details) ? $op_details['expects'] : NULL);
  469. $class_op->addProperty('returns', array_key_exists('returns', $op_details) ? $op_details['returns'] : ' "http://www.w3.org/2002/07/owl#Nothing",');
  470. return $class_op;
  471. }
  472. /**
  473. * Converts a term array into an value appropriate for an @id or @type.
  474. *
  475. * @param $term
  476. * The term array.
  477. * @param $santize
  478. * An array of keywords indicating how to santize the key. By default,
  479. * no sanitizing occurs. The two valid types are 'lowercase', and 'spacing'
  480. * where 'lowercase' converts the term name to all lowercase and
  481. * 'spacing' replaces any spaces with underscores.
  482. *
  483. * @return
  484. * The id (the term name but with spaces replaced with underscores).
  485. */
  486. protected function getContextTerm($term, $sanitize = array()) {
  487. if (!$term) {
  488. $backtrace = debug_backtrace();
  489. throw new Exception('getContextTerm: Please provide a non NUll or non empty $term.');
  490. }
  491. if (!$term['name']) {
  492. throw new Exception('getContextTerm: The provided term does not have a name: ' . print_r($term, TRUE));
  493. }
  494. $key = $term['name'];
  495. $key_adj = $key;
  496. if (in_array('spacing', $sanitize)) {
  497. $key_adj = preg_replace('/ /', '_', $key_adj);
  498. }
  499. if (in_array('lowercase', $sanitize)) {
  500. $key_adj = strtolower($key_adj);
  501. }
  502. return $key_adj;
  503. }
  504. /**
  505. * Adds a term to the '@context' section for a given resource.
  506. *
  507. * @param $resource
  508. * A TripalWebServiceResource instance.
  509. * @param $term
  510. * The term array.
  511. * @param $santize
  512. * An array of keywords indicating how to santize the key. By default,
  513. * no sanitizing occurs. The two valid types are 'lowercase', and 'spacing'
  514. * where 'lowercase' converts the term name to all lowercase and
  515. * 'spacing' replaces any spaces with underscores.
  516. * @return
  517. * The id (the term name but with spaces replaced with underscores).
  518. */
  519. protected function addContextTerm($resource, $term, $sanitize = array()) {
  520. if (!is_a($resource, 'TripalWebServiceResource')) {
  521. throw new Exception('addContextTerm: Please provide a $resource of type TripalWebServiceResource.');
  522. }
  523. if (!$term) {
  524. $backtrace = debug_backtrace();
  525. throw new Exception('addContextTerm: Please provide a non NUll or non empty $term.');
  526. }
  527. if (!$term['name']) {
  528. throw new Exception('addContextTerm: The provided term does not have a name: ' . print_r($term, TRUE));
  529. }
  530. // Make sure the vocab is present
  531. $vocab = $term['vocabulary'];
  532. $this->addContextVocab($resource, $vocab);
  533. // Sanitize the term key
  534. $key_adj = $this->getContextTerm($term, $sanitize);
  535. // Create the compact IRI
  536. $compact_iri = $vocab['short_name'] . ':' . $term['accession'];
  537. // If the full naming is indicated in the service parameters then
  538. // set the full name of the keys to include the vocab short name.
  539. if (array_key_exists('full_keys', $this->params)) {
  540. //$key_adj = $vocab['short_name'] . ':' . $key_adj;
  541. }
  542. $iri = $term['url'];
  543. $resource->addContextItem($key_adj, $compact_iri);
  544. $resource->addContextItem($compact_iri, $iri);
  545. return $key_adj;
  546. }
  547. /**
  548. * Adds a vocabulary to the '@context' section for a given resource.
  549. *
  550. * @param $resource
  551. * A TripalWebServiceResource instance.
  552. * @param $vocab
  553. * The vocabulary array.
  554. */
  555. protected function addContextVocab($resource, $vocab) {
  556. if (!is_a($resource, 'TripalWebServiceResource')) {
  557. throw new Exception('addContextVocab: Please provide a $resource of type TripalWebServiceResource.');
  558. }
  559. $key = $vocab['short_name'];
  560. // The URL here should be the URL prefix not the database home
  561. // page. But unfortunately, the URL prefix can't be guaranteed to be
  562. // a true prefix. Not all databases place by the rules. For this reason,
  563. // we can never use JSON-LD compact IRIs. :-(
  564. $iri = $vocab['url'];
  565. $resource->addContextItem($key, $iri);
  566. }
  567. /**
  568. * Adds a key/value property to the given resource.
  569. *
  570. * @param $resource
  571. * A TripalWebServiceResource instance.
  572. * @param $term
  573. * The term array for the key.
  574. * @param $value
  575. * The value to add.
  576. * @param $santize
  577. * An array of keywords indicating how to santize the key. By default,
  578. * no sanitizing occurs. The two valid types are 'lowercase', and 'spacing'
  579. * where 'lowercase' converts the term name to all lowercase and
  580. * 'spacing' replaces any spaces with underscores.
  581. * @return $key
  582. * The key (the term name but with spaces replaced with underscores).
  583. */
  584. public function addResourceProperty($resource, $term, $value, $sanitize = array()) {
  585. if (!is_a($resource, 'TripalWebServiceResource')) {
  586. throw new Exception('addProperty: Please provide a $resource of type TripalWebServiceResource.');
  587. }
  588. // First add the term
  589. $key = $this->addContextTerm($resource, $term, $sanitize);
  590. // Then add the property.
  591. $resource->addProperty($key, $value);
  592. }
  593. /**
  594. * Sets the type for the given resource.
  595. *
  596. * The type exist in the context of the web service.
  597. *
  598. * @param $resource
  599. * A TripalWebServiceResource instance.
  600. * @param $type
  601. * The type
  602. * @param $santize
  603. * An array of keywords indicating how to santize the key. By default,
  604. * no sanitizing occurs. The two valid types are 'lowercase', and 'spacing'
  605. * where 'lowercase' converts the term name to all lowercase and
  606. * 'spacing' replaces any spaces with underscores.
  607. */
  608. public function setResourceType($resource, $term, $sanitize = array('spacing')) {
  609. if (!is_a($resource, 'TripalWebServiceResource')) {
  610. throw new Exception('addProperty: Please provide a $resource of type TripalWebServiceResource.');
  611. }
  612. // First add the term
  613. $key = $this->addContextTerm($resource, $term, $sanitize);
  614. // Then set the type
  615. $resource->setType($key);
  616. }
  617. }