123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- <?php
- class TripalWebService {
-
-
-
-
-
-
-
-
- public static $label = 'Base WebService';
-
- public static $description = 'This is the base class for Tripal web services as is not meant to be used on it\'s own';
-
- public static $type = 'services';
-
-
-
-
- protected $resource;
-
- protected $path;
-
- protected $params;
-
- protected $base_path;
-
-
-
-
- public function __construct($base_path) {
- if (!$base_path) {
- throw new Exception('Please provide a $base_path argument when creating a new TripalWebService.');
- }
-
- $this->resource = new TripalWebServiceResource($base_path);
-
- $this->path = array();
- $this->params = array();
- $this->base_path = $base_path;
- }
-
-
-
-
- public function handleRequest() {
-
-
- }
-
-
-
-
- public function setPath($path) {
- $this->path = $path;
- }
-
- public function setParams($params) {
- $this->params = $params;
- }
-
- public function getVersion($sanatize = FALSE) {
- $class = get_class($this);
- $major_version = '';
- $minor_version = '';
- if (preg_match('/v(\d+)_(\d+)$/', $class, $matches)) {
- $major_version = $matches[1];
- $minor_version = $matches[2];
- return 'v' . $major_version . '.' . $minor_version;
- }
- return '';
- }
-
- public function getContext() {
- return $this->resource->getContext();
- }
-
- public function getResponse() {
- $context = $this->resource ? $this->resource->getContext() : array();
- $type = $this->resource ? $this->resource->getType() : 'unknown';
- $json_ld = array(
- '@context' => $context,
- '@id' => '',
- '@type' => $type,
- );
- $data = $this->getData();
- return array_merge($json_ld, $data);
- }
-
- public function getServicePath() {
- $class = get_class($this);
- $version = $this->getVersion();
- $type = $class::$type;
- return $this->base_path . '/' . $type . '/' . $version;
- }
-
- public function getData() {
- if ($this->resource) {
- return $this->resource->getData();
- }
- return array();
- }
-
- public function setResource($resource) {
-
- if (!is_a($resource, 'TripalWebServiceResource')) {
- throw new Exception("Cannot add a new resource to this web service as it is not a TripalWebServiceResource.");
- }
- $this->resource = $resource;
- }
-
- public function setError($message) {
- $this->resource = new TripalWebServiceResource($this->base_path);
- $this->resource->setID('error');
- $this->resource->addContextItem('error', 'rdfs:error');
- $this->resource->setType('error');
- $this->resource->addProperty('error', $message);
- }
-
- public function getSupportedClasses() {
- $supported_classes = array();
- return $supported_classes;
- }
-
- public function addContextTerm($resource, $term, $sanitize = array()) {
- if (!is_a($resource, 'TripalWebServiceResource')) {
- throw new Exception('addContextTerm: Please provide a $resource of type TripalWebServiceResource.');
- }
- if (!$term) {
- $backtrace = debug_backtrace();
- throw new Exception('addContextTerm: Please provide a non NUll or non empty $term.');
- }
- if (!$term['name']) {
- throw new Exception('addContextTerm: The provided term does not have a name: ' . print_r($term, TRUE));
- }
-
- $vocab = $term['vocabulary'];
- $this->addContextVocab($resource, $vocab);
-
- $key = $term['name'];
- $key_adj = $key;
- if (in_array('spacing', $sanitize)) {
- $key_adj = preg_replace('/ /', '_', $key_adj);
- }
- if (in_array('lowercase', $sanitize)) {
- $key_adj = strtolower($key_adj);
- }
-
- $compact_iri = $vocab['short_name'] . ':' . $term['accession'];
-
-
- if (array_key_exists('full_keys', $this->params)) {
-
- }
- $iri = $term['url'];
- $resource->addContextItem($key_adj, $compact_iri);
- $resource->addContextItem($compact_iri, $iri);
- if ($key_adj == 'clause subject') {
-
- }
- return $key_adj;
- }
-
- public function addContextVocab($resource, $vocab) {
- if (!is_a($resource, 'TripalWebServiceResource')) {
- throw new Exception('addContextVocab: Please provide a $resource of type TripalWebServiceResource.');
- }
- $key = $vocab['short_name'];
-
-
-
-
- $iri = $vocab['url'];
- $resource->addContextItem($key, $iri);
- }
-
- public function addResourceProperty($resource, $term, $value, $sanitize = array()) {
- if (!is_a($resource, 'TripalWebServiceResource')) {
- throw new Exception('addProperty: Please provide a $resource of type TripalWebServiceResource.');
- }
-
- $key = $this->addContextTerm($resource, $term, $sanitize);
-
- $resource->addProperty($key, $value);
- }
-
- public function setResourceType($resource, $term, $sanitize = array('spacing')) {
- if (!is_a($resource, 'TripalWebServiceResource')) {
- throw new Exception('addProperty: Please provide a $resource of type TripalWebServiceResource.');
- }
-
- $key = $this->addContextTerm($resource, $term, $sanitize);
-
- $resource->setType($key);
- }
- }
|