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.

RequestUtils.php 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Tqdev\PhpCrudApi;
  3. use Psr\Http\Message\ServerRequestInterface;
  4. use Tqdev\PhpCrudApi\Column\ReflectionService;
  5. class RequestUtils
  6. {
  7. public static function setParams(ServerRequestInterface $request, array $params): ServerRequestInterface
  8. {
  9. $query = preg_replace('|%5B[0-9]+%5D=|', '=', http_build_query($params));
  10. return $request->withUri($request->getUri()->withQuery($query));
  11. }
  12. public static function getHeader(ServerRequestInterface $request, string $header): string
  13. {
  14. $headers = $request->getHeader($header);
  15. return isset($headers[0]) ? $headers[0] : '';
  16. }
  17. public static function getParams(ServerRequestInterface $request): array
  18. {
  19. $params = array();
  20. $query = $request->getUri()->getQuery();
  21. $query = str_replace('][]=', ']=', str_replace('=', '[]=', $query));
  22. parse_str($query, $params);
  23. return $params;
  24. }
  25. public static function getPathSegment(ServerRequestInterface $request, int $part): string
  26. {
  27. $path = $request->getUri()->getPath();
  28. $pathSegments = explode('/', rtrim($path, '/'));
  29. if ($part < 0 || $part >= count($pathSegments)) {
  30. return '';
  31. }
  32. return urldecode($pathSegments[$part]);
  33. }
  34. public static function getOperation(ServerRequestInterface $request): string
  35. {
  36. $method = $request->getMethod();
  37. $path = RequestUtils::getPathSegment($request, 1);
  38. $hasPk = RequestUtils::getPathSegment($request, 3) != '';
  39. switch ($path) {
  40. case 'openapi':
  41. return 'document';
  42. case 'columns':
  43. return $method == 'get' ? 'reflect' : 'remodel';
  44. case 'records':
  45. switch ($method) {
  46. case 'POST':
  47. return 'create';
  48. case 'GET':
  49. return $hasPk ? 'read' : 'list';
  50. case 'PUT':
  51. return 'update';
  52. case 'DELETE':
  53. return 'delete';
  54. case 'PATCH':
  55. return 'increment';
  56. }
  57. }
  58. return 'unknown';
  59. }
  60. private static function getJoinTables(string $tableName, array $parameters): array
  61. {
  62. $uniqueTableNames = array();
  63. $uniqueTableNames[$tableName] = true;
  64. if (isset($parameters['join'])) {
  65. foreach ($parameters['join'] as $parameter) {
  66. $tableNames = explode(',', trim($parameter));
  67. foreach ($tableNames as $tableName) {
  68. $uniqueTableNames[$tableName] = true;
  69. }
  70. }
  71. }
  72. return array_keys($uniqueTableNames);
  73. }
  74. public static function getTableNames(ServerRequestInterface $request, ReflectionService $reflection): array
  75. {
  76. $path = RequestUtils::getPathSegment($request, 1);
  77. $tableName = RequestUtils::getPathSegment($request, 2);
  78. $allTableNames = $reflection->getTableNames();
  79. switch ($path) {
  80. case 'openapi':
  81. return $allTableNames;
  82. case 'columns':
  83. return $tableName ? [$tableName] : $allTableNames;
  84. case 'records':
  85. return self::getJoinTables($tableName, RequestUtils::getParams($request));
  86. }
  87. return $allTableNames;
  88. }
  89. }