123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- <?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;
-
-
- $vocab = tripal_get_vocabulary_details('rdf');
- $this->addContextItem('rdf', $vocab['sw_url']);
- $vocab = tripal_get_vocabulary_details('rdfs');
- $this->addContextItem('rdfs', $vocab['sw_url']);
- $vocab = tripal_get_vocabulary_details('hydra');
- $this->addContextItem('hydra', $vocab['sw_url']);
- $vocab = tripal_get_vocabulary_details('dc');
- $this->addContextItem('dc', $vocab['sw_url']);
- $vocab = tripal_get_vocabulary_details('schema');
- $this->addContextItem('schema', $vocab['sw_url']);
- $vocab = tripal_get_vocabulary_details('local');
- $this->addContextItem('local', url($vocab['sw_url'], array('absolute' => TRUE)));
- $this->data['@id'] = $service_path;
- $this->data['@type'] = '';
- }
-
- public function addContextItem($name, $iri) {
- if (array_key_exists($name, $this->context)) {
- return;
- }
- $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(t("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.", array('!key' => $key)));
- }
- }
- else {
-
- if (!in_array($key, $keys)) {
- throw new Exception(t("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.", array('!key' => $key)));
- }
- }
- }
-
- private function checkValue(&$value) {
- if (is_array($value)) {
- foreach ($value as $k => $v) {
-
-
-
- if (preg_match('/^\d+$/', $k)) {
- $this->checkValue($value[$k]);
- }
- else {
- if ($k != '@id' and $k != '@type') {
- $this->checkKey($k);
- }
- $this->checkValue($value[$k]);
- }
- }
- }
- else {
-
-
- 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();
- }
- }
- }
-
- 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);
- $this->checkValue($value);
-
- 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 (!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;
- }
- }
-
- function getProperty($key) {
- return $this->data[$key];
- }
-
- 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 provided to the TripalWebServiceResource::setContext() function must be an instance of a TripalWebServiceResource.");
- }
- $this->context = $resource->getContext();
- }
- }
|