WpsHelper.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. Lib::loadClass('Type_WpsProcess');
  3. Lib::loadClass('Api_OwsException');
  4. class Api_WpsHelper {
  5. static function getUserWpsProcessList($userLogin = null) {
  6. // TODO: $userLogin
  7. $defaultWpsProcessList = self::getDefaultWpsProcessList();
  8. if (array_key_exists($identifier, $defaultWpsProcessList)) return Type_WpsProcess::build( $defaultWpsProcessList[$identifier] );
  9. $wpsProcessList = [];
  10. // TODO: fetch from DB - `CRM_#CACHE_ACL_FUNCTIONS` or object like UrlActions
  11. $wpsProcessList = array_merge($defaultWpsProcessList, $wpsProcessList);
  12. return array_map(function ($wpsProcess, $identifier) {
  13. return (object)[ 'identifier' => $identifier, 'title' => $wpsProcess['title'], 'description' => $wpsProcess['description'] ];
  14. }, $wpsProcessList, array_keys($wpsProcessList));
  15. return $wpsProcessList;
  16. }
  17. static function getUserWpsProcess($identifier, $userLogin = null) {
  18. // TODO: $userLogin
  19. $defaultWpsProcessList = self::getDefaultWpsProcessList();
  20. if (array_key_exists($identifier, $defaultWpsProcessList)) return Type_WpsProcess::build( $defaultWpsProcessList[$identifier] );
  21. // TODO: fetch from DB - `CRM_#CACHE_ACL_FUNCTIONS` or object like UrlActions
  22. throw new Exception("Forbidden - wps function not exists or access denied", 403);
  23. }
  24. static function getDefaultWpsProcessList() {
  25. $defaultWpsProcessList = [];
  26. {
  27. $selectFeatureBase = [
  28. 'dataInputs' => [
  29. [
  30. 'maxOccurs' => "1",
  31. 'minOccurs' => "1",
  32. 'identifier' => 'typeName',
  33. 'title' => "Feature typeName",
  34. 'description' => "Feature typeName eg. default_db/PROBLEMS",
  35. 'type' => 'literal',
  36. ],
  37. [
  38. 'maxOccurs' => "unbounded",
  39. 'minOccurs' => "1",
  40. 'identifier' => 'primaryKey',
  41. 'title' => "Feature primaryKey",
  42. 'description' => "Feature primaryKey",
  43. 'type' => 'literal',
  44. ],
  45. ],
  46. // 'MimeType' => text/xml, 'Encoding' => base64, 'Schema' => http://foo.bar/gml/3.1.0/polygon.xsd
  47. 'defaultOutput' => [ 'MimeType' => "text/xml; subtype=wfs-collection/1.0" ], // TODO: result type - like Insert Wfs Transaction result - list of: Success (primaryKey = 123), ...
  48. 'supportedOutput' => [
  49. [ 'MimeType' => "text/xml; subtype=wfs-collection/1.0" ],
  50. [ 'MimeType' => "text/xml; subtype=wfs-collection/1.1" ],
  51. [ 'MimeType' => "application/json" ],
  52. // [ 'MimeType' => "application/wfs-collection-1.0" ],
  53. // [ 'MimeType' => "application/wfs-collection-1.1" ],
  54. // [ 'MimeType' => "application/zip" ],
  55. ]
  56. ];
  57. $defaultWpsProcessList['p5:selectFeature'] = array_merge([ 'title' => "Select feature", 'descriptin' => "Select feature (set attribute @selected to true)" ], $selectFeatureBase);
  58. $defaultWpsProcessList['p5:unselectFeature'] = array_merge([ 'title' => "Unselect feature", 'descriptin' => "Unselect feature (set attribute @selected to false)" ], $selectFeatureBase);
  59. }
  60. array_walk($defaultWpsProcessList, function (&$wpsFunction, $identifier) {
  61. $wpsFunction['identifier'] = $identifier;
  62. });
  63. return $defaultWpsProcessList;
  64. }
  65. static function validateProcessArgs($wpsProcess, $args) {
  66. foreach ($wpsProcess->dataInputs as $input) {
  67. $identifier = $input->identifier;
  68. // TODO: required? minOccurs > 0
  69. if ($input->minOccurs > 0 && empty($args[ $identifier ])) throw new Api_OwsException("Missing value for '{$identifier}'", 501, null, 'MissingParameterValue', 'request');
  70. $totalValues = count($args[ $identifier ]);
  71. if ($totalValues > 1) {
  72. if ( 'unbounded' !== $input->maxOccurs && $totalValues > $input->maxOccurs ) {
  73. throw new Api_OwsException("Too many values for '{$identifier}'", 501, null, 'TooManyParameterValues', 'request');
  74. }
  75. }
  76. // TODO: validate values with type definition - _xsdType
  77. }
  78. }
  79. static function executeProcess($wpsProcess, $args, $responseForm = 'json') {
  80. $className = implode("_", array_merge(
  81. [
  82. 'Api',
  83. 'Process',
  84. ], array_map('ucfirst', explode(':', $wpsProcess->identifier))
  85. ) );
  86. if (!Lib::tryLoadClass($className)) {
  87. throw new Api_OwsException("Api Process not found '{$wpsProcess->identifier}'", 500, null, 'ApiProcessNotFound', 'request');
  88. }
  89. $className::run($args, $responseForm);
  90. }
  91. }