WpsHelper.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. $selectFeatureArgTypeName = [
  28. 'maxOccurs' => "1",
  29. 'minOccurs' => "1",
  30. 'identifier' => 'typeName',
  31. 'title' => "Feature typeName",
  32. 'description' => "Feature typeName eg. default_db/PROBLEMS",
  33. 'type' => 'literal',
  34. ];
  35. $selectFeatureArgPrimaryKey = [
  36. 'maxOccurs' => "unbounded",
  37. 'minOccurs' => "1",
  38. 'identifier' => 'primaryKey',
  39. 'title' => "Feature primaryKey",
  40. 'description' => "Feature primaryKey",
  41. 'type' => 'literal',
  42. ];
  43. $selectFeatureBase = [
  44. 'dataInputs' => [
  45. $selectFeatureArgTypeName,
  46. $selectFeatureArgPrimaryKey,
  47. ],
  48. // 'MimeType' => text/xml, 'Encoding' => base64, 'Schema' => http://foo.bar/gml/3.1.0/polygon.xsd
  49. 'defaultOutput' => [ 'MimeType' => "application/json" ], // TODO: result type - like Insert Wfs Transaction result - list of: Success (primaryKey = 123), ...
  50. 'supportedOutput' => [
  51. [ 'MimeType' => "application/json" ],
  52. [ 'MimeType' => "text/xml; subtype=wfs-collection/1.0" ],
  53. [ 'MimeType' => "text/xml; subtype=wfs-collection/1.1" ],
  54. // [ 'MimeType' => "application/wfs-collection-1.0" ],
  55. // [ 'MimeType' => "application/wfs-collection-1.1" ],
  56. // [ 'MimeType' => "application/zip" ],
  57. ]
  58. ];
  59. $defaultWpsProcessList['p5:selectFeature'] = array_merge([ 'title' => "Select feature", 'descriptin' => "Select feature (set attribute @selected to true)" ], $selectFeatureBase);
  60. $defaultWpsProcessList['p5:unselectFeature'] = array_merge([ 'title' => "Unselect feature", 'descriptin' => "Unselect feature (set attribute @selected to false)" ], $selectFeatureBase);
  61. $defaultWpsProcessList['p5:getSelectedFeatures'] = array_merge([ 'title' => "Get selected features", 'descriptin' => "Get list of selected features (with attribute @selected = true)" ], $selectFeatureBase);
  62. $defaultWpsProcessList['p5:unselectAllFeatures'] = array_merge([ 'title' => "Unselect all features", 'descriptin' => "Unselect all features (set attribute @selected to false)" ], $selectFeatureBase, [
  63. 'dataInputs' => [
  64. $selectFeatureArgTypeName
  65. ]
  66. ]);
  67. }
  68. array_walk($defaultWpsProcessList, function (&$wpsFunction, $identifier) {
  69. $wpsFunction['identifier'] = $identifier;
  70. });
  71. return $defaultWpsProcessList;
  72. }
  73. static function validateProcessArgs($wpsProcess, $args) {
  74. foreach ($wpsProcess->dataInputs as $input) {
  75. $identifier = $input->identifier;
  76. // TODO: required? minOccurs > 0
  77. if ($input->minOccurs > 0 && empty($args[ $identifier ])) throw new Api_OwsException("Missing value for '{$identifier}'", 501, null, 'MissingParameterValue', 'request');
  78. $totalValues = count($args[ $identifier ]);
  79. if ($totalValues > 1) {
  80. if ( 'unbounded' !== $input->maxOccurs && $totalValues > $input->maxOccurs ) {
  81. throw new Api_OwsException("Too many values for '{$identifier}'", 501, null, 'TooManyParameterValues', 'request');
  82. }
  83. }
  84. // TODO: validate values with type definition - _xsdType
  85. }
  86. }
  87. static function executeProcess($wpsProcess, $args, $responseForm = 'json') {
  88. $className = implode("_", array_merge(
  89. [
  90. 'Api',
  91. 'Process',
  92. ], array_map('ucfirst', explode(':', $wpsProcess->identifier))
  93. ) );
  94. if (!Lib::tryLoadClass($className)) {
  95. throw new Api_OwsException("Api Process not found '{$wpsProcess->identifier}'", 500, null, 'ApiProcessNotFound', 'request');
  96. }
  97. $className::run($args, $responseForm);
  98. }
  99. }