123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- <?php
- class TripalWebServiceResource {
-
- protected $id;
-
- protected $type;
-
- protected $context;
-
- protected $data;
-
- protected $service_path;
-
- public function __construct($service_path) {
- $this->context = array();
- $this->data = array();
- $this->service_path = $service_path;
-
-
- $this->addContextItem('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
- $this->addContextItem('rdfs', 'http://www.w3.org/2000/01/rdf-schema#');
- $this->addContextItem('hydra', "http://www.w3.org/ns/hydra/core#");
- $vocab = tripal_get_vocabulary_details('dc');
- $this->addContextItem('dc', $vocab['url']);
- $vocab = tripal_get_vocabulary_details('schema');
- $this->addContextItem('schema', $vocab['url']);
- $vocab = tripal_get_vocabulary_details('local');
- $this->addContextItem('local', url($vocab['url'], array('absolute' => TRUE)). '/');
- $this->data['@id'] = $service_path;
- $this->data['@type'] = '';
- }
-
- public function addContextItem($name, $iri) {
-
- $this->context[$name] = $iri;
- }
-
- public function removeContextItem($name, $iri) {
-
- unset($this->context[$name]);
- }
-
- public function setType($type) {
- $this->checkKey($type);
- $this->type = $type;
- $this->data['@type'] = $type;
- }
-
- private function checkKey($key) {
-
- $keys = array_keys($this->context);
-
- if (preg_match('/^(http|https):\/\/.*/', $key)) {
- return;
- }
-
-
- $matches = array();
- if (preg_match('/^(.*?):(.*?)$/', $key, $matches)) {
- $vocab = $matches[1];
- $accession = $matches[2];
-
- if ($vocab == '_') {
- return;
- }
-
- if (!in_array($vocab, $keys)) {
- throw new Exception("The key, '$key', has a vocabulary that has not yet been added to the " .
- "context. Use the addContextItem() function to add the vocabulary prior to adding a value for it.");
- }
- }
- else {
-
- if (!in_array($key, $keys)) {
- throw new Exception("The key, '$key', has not yet been added to the " .
- "context. Use the addContextItem() function to add this key prior to adding a value for it.");
- }
- }
- }
-
- public function setID($id) {
- $this->id = $id;
- $this->data['@id'] = $this->getURI($id);
- }
-
- public function getURI($id) {
-
-
-
- $matches = array();
- if (preg_match('/^(.*?):(.*?)$/', $id, $matches)) {
- $vocab = $matches[1];
- if ($vocab == '_') {
- return $id;
- }
- return $id;
- }
- else {
- return $this->service_path . '/' . $id;
- }
- }
-
- public function getID() {
- return $this->id;
- }
-
- public function getType() {
- return $this->type;
- }
-
- public function addProperty($key, $value) {
- $this->checkKey($key);
-
- if (is_array($value) and count(array_filter(array_keys($value), 'is_int')) == count(array_keys($value))) {
- if (!array_key_exists($key, $this->data)) {
- $this->data[$key] = array();
- }
- foreach ($value as $item) {
- $this->addProperty($key, $item);
- }
- return;
- }
-
-
- if (is_a($value, 'TripalWebServiceResource') or is_subclass_of($value, 'TripalWebServiceResource')) {
-
- $context = $value->getContext();
- foreach ($context as $k => $v) {
- $this->addContextItem($k, $v);
- }
- $value = $value->getData();
- }
-
- if (!array_key_exists($key, $this->data)) {
- $this->data[$key] = $value;
- }
-
-
- else {
-
-
- if (!is_array($this->data[$key]) or count(array_filter(array_keys($this->data[$key]), 'is_string')) > 0) {
- $element = $this->data[$key];
- $this->data[$key] = array();
- $this->data[$key][] = $element;
- }
- $this->data[$key][] = $value;
- }
- }
-
- public function getData() {
- return $this->data;
- }
-
- public function getContext() {
- return $this->context;
- }
-
- public function setContext($resource) {
- if (!is_a($resource, 'TripalWebServiceResource')) {
- throw new Exception("The \$resource argument must be an instance of a TripalWebServiceResource.");
- }
- $this->context = $resource->getContext();
- }
- }
|