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.

ErrorCode.php 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Record;
  3. use Tqdev\PhpCrudApi\ResponseFactory;
  4. class ErrorCode
  5. {
  6. private $code;
  7. private $message;
  8. private $status;
  9. const ERROR_NOT_FOUND = 9999;
  10. const ROUTE_NOT_FOUND = 1000;
  11. const TABLE_NOT_FOUND = 1001;
  12. const ARGUMENT_COUNT_MISMATCH = 1002;
  13. const RECORD_NOT_FOUND = 1003;
  14. const ORIGIN_FORBIDDEN = 1004;
  15. const COLUMN_NOT_FOUND = 1005;
  16. const TABLE_ALREADY_EXISTS = 1006;
  17. const COLUMN_ALREADY_EXISTS = 1007;
  18. const HTTP_MESSAGE_NOT_READABLE = 1008;
  19. const DUPLICATE_KEY_EXCEPTION = 1009;
  20. const DATA_INTEGRITY_VIOLATION = 1010;
  21. const AUTHENTICATION_REQUIRED = 1011;
  22. const AUTHENTICATION_FAILED = 1012;
  23. const INPUT_VALIDATION_FAILED = 1013;
  24. const OPERATION_FORBIDDEN = 1014;
  25. const OPERATION_NOT_SUPPORTED = 1015;
  26. const TEMPORARY_OR_PERMANENTLY_BLOCKED = 1016;
  27. const BAD_OR_MISSING_XSRF_TOKEN = 1017;
  28. const ONLY_AJAX_REQUESTS_ALLOWED = 1018;
  29. const PAGINATION_FORBIDDEN = 1019;
  30. const USER_ALREADY_EXIST = 1020;
  31. const PASSWORD_TOO_SHORT = 1021;
  32. private $values = [
  33. 0000 => ["Success", ResponseFactory::OK],
  34. 1000 => ["Route '%s' not found", ResponseFactory::NOT_FOUND],
  35. 1001 => ["Table '%s' not found", ResponseFactory::NOT_FOUND],
  36. 1002 => ["Argument count mismatch in '%s'", ResponseFactory::UNPROCESSABLE_ENTITY],
  37. 1003 => ["Record '%s' not found", ResponseFactory::NOT_FOUND],
  38. 1004 => ["Origin '%s' is forbidden", ResponseFactory::FORBIDDEN],
  39. 1005 => ["Column '%s' not found", ResponseFactory::NOT_FOUND],
  40. 1006 => ["Table '%s' already exists", ResponseFactory::CONFLICT],
  41. 1007 => ["Column '%s' already exists", ResponseFactory::CONFLICT],
  42. 1008 => ["Cannot read HTTP message", ResponseFactory::UNPROCESSABLE_ENTITY],
  43. 1009 => ["Duplicate key exception", ResponseFactory::CONFLICT],
  44. 1010 => ["Data integrity violation", ResponseFactory::CONFLICT],
  45. 1011 => ["Authentication required", ResponseFactory::UNAUTHORIZED],
  46. 1012 => ["Authentication failed for '%s'", ResponseFactory::FORBIDDEN],
  47. 1013 => ["Input validation failed for '%s'", ResponseFactory::UNPROCESSABLE_ENTITY],
  48. 1014 => ["Operation forbidden", ResponseFactory::FORBIDDEN],
  49. 1015 => ["Operation '%s' not supported", ResponseFactory::METHOD_NOT_ALLOWED],
  50. 1016 => ["Temporary or permanently blocked", ResponseFactory::FORBIDDEN],
  51. 1017 => ["Bad or missing XSRF token", ResponseFactory::FORBIDDEN],
  52. 1018 => ["Only AJAX requests allowed for '%s'", ResponseFactory::FORBIDDEN],
  53. 1019 => ["Pagination forbidden", ResponseFactory::FORBIDDEN],
  54. 1020 => ["User '%s' already exists", ResponseFactory::CONFLICT],
  55. 1021 => ["Password too short (<%d characters)", ResponseFactory::UNPROCESSABLE_ENTITY],
  56. 9999 => ["%s", ResponseFactory::INTERNAL_SERVER_ERROR],
  57. ];
  58. public function __construct(int $code)
  59. {
  60. if (!isset($this->values[$code])) {
  61. $code = 9999;
  62. }
  63. $this->code = $code;
  64. $this->message = $this->values[$code][0];
  65. $this->status = $this->values[$code][1];
  66. }
  67. public function getCode(): int
  68. {
  69. return $this->code;
  70. }
  71. public function getMessage(string $argument): string
  72. {
  73. return sprintf($this->message, $argument);
  74. }
  75. public function getStatus(): int
  76. {
  77. return $this->status;
  78. }
  79. }