| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- class Type_ApiRequest {
- var $_data = [];
- static function build($url) { // @return Type_ApiRequest
- // GET https://biuro.biall-net.pl/dev-pl/se-master/wps.php?Request=GetCapabilities&identifier=&Service=WPS&Version=1.0.0
- // $url: /xml/wps?Request=GetCapabilities&identifier=&Service=WPS&Version=1.0.0
- $request = new Type_ApiRequest();
- $request->url = trim($url, '/ ');
- $urlParts = explode('?', $request->url);
- $request->path = $urlParts[0];
- $request->query = [];
- $query = (count($urlParts) > 1)? $urlParts[1] : null;
- if (!empty($query)) {
- $queryArgs = array();
- parse_str($request->query, $queryArgs);
- $request->query = $queryArgs;
- }
- $request->args = $_REQUEST;
- $segments = array_filter(explode('/', $request->path), function ($part) {
- return (!empty($part));
- });
- $request->format = array_shift($segments);
- IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">url (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
- if (empty($segments)) return null;
- $request->segments = $segments;
- IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">responseFormat (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request->format);echo'</pre>';}
- if (!self::checkResponseFormat($request->format)) {
- throw new HttpException("Response format not allowed", 400);
- }
- IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">u (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
- $request->apiRouterName = array_shift($request->segments);
- IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">u (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
- $request->version = V::geti('version', '', $_REQUEST);
- // if (!self::checkVersion($request->version, $request->apiRouterName)) { // version may be set in POST body
- // throw new Exception("{$request->apiRouterName} API Version not supported ({$request->version})", 400);
- // }
- return $request;
- }
- static function checkResponseFormat($format) {
- $allowedTypes = array();
- $allowedTypes[] = 'xml';
- $allowedTypes[] = 'json';
- $allowedTypes[] = 'csv';
- $allowedTypes[] = 'csv_file';
- $allowedTypes[] = 'raw';
- return in_array($format, $allowedTypes);
- }
- static function checkVersion($version, $apiName) {
- switch ($apiName) {
- case 'wps': return self::checkWPSVersion($version);
- case 'wfsQgis': return self::checkWFSQgisVersion($version);
- case 'wfsData': return self::checkWFSDataVersion($version);
- default: return false;
- }
- }
- static function checkWPSVersion($version) {
- switch ($version) {
- case '1.0.0': return true; // QGIS client
- case '2.0.0': return false; // TODO: implement WPS 2.0.0 - need working client to test
- case '2.0.2': return false; // TODO: implement WPS 2.0.0 - need working client to test
- default: return false;
- }
- }
- static function checkWFSQgisVersion($version) {
- return true; // TODO: restrict by version ?
- }
- static function checkWFSDataVersion($version) {
- return true; // TODO: restrict by version ?
- }
- function __isset($name) {
- return (array_key_exists($name, $this->_data));
- }
- function __get($name) {
- if (array_key_exists($name, $this->_data)) {
- return $this->_data[$name];
- }
- return null;
- }
- function __set($name, $value) {
- $this->_data[$name] = $value;
- }
- function toArray() {
- return $this->_data;
- }
- function __toString() {
- return str_replace('"', '',
- str_replace([ '{', '}', '":', ',"' ], [ '{ ', ' }', ': ', ', ' ], json_encode($this->_data))
- );
- }
- }
|