api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GeoJsonController.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Controller;
  3. use Psr\Http\Message\ResponseInterface;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use Tqdev\PhpCrudApi\GeoJson\GeoJsonService;
  6. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  7. use Tqdev\PhpCrudApi\Record\ErrorCode;
  8. use Tqdev\PhpCrudApi\RequestUtils;
  9. class GeoJsonController
  10. {
  11. private $service;
  12. private $responder;
  13. private $geoJsonConverter;
  14. public function __construct(Router $router, Responder $responder, GeoJsonService $service)
  15. {
  16. $router->register('GET', '/geojson/*', array($this, '_list'));
  17. $router->register('GET', '/geojson/*/*', array($this, 'read'));
  18. $this->service = $service;
  19. $this->responder = $responder;
  20. }
  21. public function _list(ServerRequestInterface $request): ResponseInterface
  22. {
  23. $table = RequestUtils::getPathSegment($request, 2);
  24. $params = RequestUtils::getParams($request);
  25. if (!$this->service->hasTable($table)) {
  26. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  27. }
  28. return $this->responder->success($this->service->_list($table, $params));
  29. }
  30. public function read(ServerRequestInterface $request): ResponseInterface
  31. {
  32. $table = RequestUtils::getPathSegment($request, 2);
  33. if (!$this->service->hasTable($table)) {
  34. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  35. }
  36. if ($this->service->getType($table) != 'table') {
  37. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  38. }
  39. $id = RequestUtils::getPathSegment($request, 3);
  40. $params = RequestUtils::getParams($request);
  41. if (strpos($id, ',') !== false) {
  42. $ids = explode(',', $id);
  43. $result = (object) array('type' => 'FeatureCollection', 'features' => array());
  44. for ($i = 0; $i < count($ids); $i++) {
  45. array_push($result->features, $this->service->read($table, $ids[$i], $params));
  46. }
  47. return $this->responder->success($result);
  48. } else {
  49. $response = $this->service->read($table, $id, $params);
  50. if ($response === null) {
  51. return $this->responder->error(ErrorCode::RECORD_NOT_FOUND, $id);
  52. }
  53. return $this->responder->success($response);
  54. }
  55. }
  56. }