ApiRequest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. class Type_ApiRequest {
  3. var $_data = [];
  4. static function build($url) { // @return Type_ApiRequest
  5. // GET https://biuro.biall-net.pl/dev-pl/se-master/wps.php?Request=GetCapabilities&identifier=&Service=WPS&Version=1.0.0
  6. // $url: /xml/wps?Request=GetCapabilities&identifier=&Service=WPS&Version=1.0.0
  7. $request = new Type_ApiRequest();
  8. $request->url = trim($url, '/ ');
  9. $urlParts = explode('?', $request->url);
  10. $request->path = $urlParts[0];
  11. $request->query = [];
  12. $query = (count($urlParts) > 1)? $urlParts[1] : null;
  13. if (!empty($query)) {
  14. $queryArgs = array();
  15. parse_str($request->query, $queryArgs);
  16. $request->query = $queryArgs;
  17. }
  18. $request->args = $_REQUEST;
  19. $segments = array_filter(explode('/', $request->path), function ($part) {
  20. return (!empty($part));
  21. });
  22. $request->format = array_shift($segments);
  23. 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>';}
  24. if (empty($segments)) return null;
  25. $request->segments = $segments;
  26. 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>';}
  27. if (!self::checkResponseFormat($request->format)) {
  28. throw new HttpException("Response format not allowed", 400);
  29. }
  30. 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>';}
  31. $request->apiRouterName = array_shift($request->segments);
  32. 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>';}
  33. $request->version = V::geti('version', '', $_REQUEST);
  34. // if (!self::checkVersion($request->version, $request->apiRouterName)) { // version may be set in POST body
  35. // throw new Exception("{$request->apiRouterName} API Version not supported ({$request->version})", 400);
  36. // }
  37. return $request;
  38. }
  39. static function checkResponseFormat($format) {
  40. $allowedTypes = array();
  41. $allowedTypes[] = 'xml';
  42. $allowedTypes[] = 'json';
  43. $allowedTypes[] = 'csv';
  44. $allowedTypes[] = 'csv_file';
  45. $allowedTypes[] = 'raw';
  46. return in_array($format, $allowedTypes);
  47. }
  48. static function checkVersion($version, $apiName) {
  49. switch ($apiName) {
  50. case 'wps': return self::checkWPSVersion($version);
  51. case 'wfsQgis': return self::checkWFSQgisVersion($version);
  52. case 'wfsData': return self::checkWFSDataVersion($version);
  53. default: return false;
  54. }
  55. }
  56. static function checkWPSVersion($version) {
  57. switch ($version) {
  58. case '1.0.0': return true; // QGIS client
  59. case '2.0.0': return false; // TODO: implement WPS 2.0.0 - need working client to test
  60. case '2.0.2': return false; // TODO: implement WPS 2.0.0 - need working client to test
  61. default: return false;
  62. }
  63. }
  64. static function checkWFSQgisVersion($version) {
  65. return true; // TODO: restrict by version ?
  66. }
  67. static function checkWFSDataVersion($version) {
  68. return true; // TODO: restrict by version ?
  69. }
  70. function __isset($name) {
  71. return (array_key_exists($name, $this->_data));
  72. }
  73. function __get($name) {
  74. if (array_key_exists($name, $this->_data)) {
  75. return $this->_data[$name];
  76. }
  77. return null;
  78. }
  79. function __set($name, $value) {
  80. $this->_data[$name] = $value;
  81. }
  82. function toArray() {
  83. return $this->_data;
  84. }
  85. function __toString() {
  86. return str_replace('"', '',
  87. str_replace([ '{', '}', '":', ',"' ], [ '{ ', ' }', ': ', ', ' ], json_encode($this->_data))
  88. );
  89. }
  90. }