1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
-
- namespace Tqdev\PhpCrudApi\Record;
-
- use Tqdev\PhpCrudApi\ResponseFactory;
-
- class ErrorCode
- {
- private $code;
- private $message;
- private $status;
-
- public const ERROR_NOT_FOUND = 9999;
- public const ROUTE_NOT_FOUND = 1000;
- public const TABLE_NOT_FOUND = 1001;
- public const ARGUMENT_COUNT_MISMATCH = 1002;
- public const RECORD_NOT_FOUND = 1003;
- public const ORIGIN_FORBIDDEN = 1004;
- public const COLUMN_NOT_FOUND = 1005;
- public const TABLE_ALREADY_EXISTS = 1006;
- public const COLUMN_ALREADY_EXISTS = 1007;
- public const HTTP_MESSAGE_NOT_READABLE = 1008;
- public const DUPLICATE_KEY_EXCEPTION = 1009;
- public const DATA_INTEGRITY_VIOLATION = 1010;
- public const AUTHENTICATION_REQUIRED = 1011;
- public const AUTHENTICATION_FAILED = 1012;
- public const INPUT_VALIDATION_FAILED = 1013;
- public const OPERATION_FORBIDDEN = 1014;
- public const OPERATION_NOT_SUPPORTED = 1015;
- public const TEMPORARY_OR_PERMANENTLY_BLOCKED = 1016;
- public const BAD_OR_MISSING_XSRF_TOKEN = 1017;
- public const ONLY_AJAX_REQUESTS_ALLOWED = 1018;
- public const PAGINATION_FORBIDDEN = 1019;
-
- private $values = [
- 9999 => ["%s", ResponseFactory::INTERNAL_SERVER_ERROR],
- 1000 => ["Route '%s' not found", ResponseFactory::NOT_FOUND],
- 1001 => ["Table '%s' not found", ResponseFactory::NOT_FOUND],
- 1002 => ["Argument count mismatch in '%s'", ResponseFactory::UNPROCESSABLE_ENTITY],
- 1003 => ["Record '%s' not found", ResponseFactory::NOT_FOUND],
- 1004 => ["Origin '%s' is forbidden", ResponseFactory::FORBIDDEN],
- 1005 => ["Column '%s' not found", ResponseFactory::NOT_FOUND],
- 1006 => ["Table '%s' already exists", ResponseFactory::CONFLICT],
- 1007 => ["Column '%s' already exists", ResponseFactory::CONFLICT],
- 1008 => ["Cannot read HTTP message", ResponseFactory::UNPROCESSABLE_ENTITY],
- 1009 => ["Duplicate key exception", ResponseFactory::CONFLICT],
- 1010 => ["Data integrity violation", ResponseFactory::CONFLICT],
- 1011 => ["Authentication required", ResponseFactory::UNAUTHORIZED],
- 1012 => ["Authentication failed for '%s'", ResponseFactory::FORBIDDEN],
- 1013 => ["Input validation failed for '%s'", ResponseFactory::UNPROCESSABLE_ENTITY],
- 1014 => ["Operation forbidden", ResponseFactory::FORBIDDEN],
- 1015 => ["Operation '%s' not supported", ResponseFactory::METHOD_NOT_ALLOWED],
- 1016 => ["Temporary or permanently blocked", ResponseFactory::FORBIDDEN],
- 1017 => ["Bad or missing XSRF token", ResponseFactory::FORBIDDEN],
- 1018 => ["Only AJAX requests allowed for '%s'", ResponseFactory::FORBIDDEN],
- 1019 => ["Pagination forbidden", ResponseFactory::FORBIDDEN],
- ];
-
- public function __construct(int $code)
- {
- if (!isset($this->values[$code])) {
- $code = 9999;
- }
- $this->code = $code;
- $this->message = $this->values[$code][0];
- $this->status = $this->values[$code][1];
- }
-
- public function getCode(): int
- {
- return $this->code;
- }
-
- public function getMessage(string $argument): string
- {
- return sprintf($this->message, $argument);
- }
-
- public function getStatus(): int
- {
- return $this->status;
- }
- }
|