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