@@ -112,7 +112,7 @@ |
||
112 | 112 | |
113 | 113 | $map = static::getMap(); |
114 | 114 | |
115 | - return $map[ $api_code ] ?? null; |
|
115 | + return $map[$api_code] ?? null; |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | /** |
@@ -21,125 +21,125 @@ |
||
21 | 21 | */ |
22 | 22 | trait ApiCodesHelpers |
23 | 23 | { |
24 | - /** |
|
25 | - * Returns lowest allowed error code for this module |
|
26 | - * |
|
27 | - * @return integer |
|
28 | - * |
|
29 | - * @throws \RuntimeException Throws exception if no min_code set up |
|
30 | - */ |
|
31 | - public static function getMinCode(): int |
|
32 | - { |
|
33 | - $key = ResponseBuilder::CONF_KEY_MIN_CODE; |
|
34 | - $min_code = Config::get($key, null); |
|
35 | - |
|
36 | - if ($min_code === null) { |
|
37 | - throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
38 | - } |
|
39 | - |
|
40 | - return $min_code; |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Returns highest allowed error code for this module |
|
45 | - * |
|
46 | - * @return integer |
|
47 | - * |
|
48 | - * @throws \RuntimeException Throws exception if no max_code set up |
|
49 | - */ |
|
50 | - public static function getMaxCode(): int |
|
51 | - { |
|
52 | - $key = ResponseBuilder::CONF_KEY_MAX_CODE; |
|
53 | - $max_code = Config::get($key, null); |
|
54 | - |
|
55 | - if ($max_code === null) { |
|
56 | - throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
57 | - } |
|
58 | - |
|
59 | - return $max_code; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Returns array of error code constants defined in this class. Used mainly for debugging/tests |
|
64 | - * |
|
65 | - * @return array |
|
66 | - * @throws \ReflectionException |
|
67 | - */ |
|
68 | - public static function getApiCodeConstants(): array |
|
69 | - { |
|
70 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
71 | - return (new \ReflectionClass(static::class))->getConstants(); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Returns complete error code to locale string mapping array |
|
76 | - * |
|
77 | - * @return array |
|
78 | - * |
|
79 | - * @throws \RuntimeException Thrown when builder map is not configured. |
|
80 | - */ |
|
81 | - public static function getMap(): array |
|
82 | - { |
|
83 | - $user_map = Config::get(ResponseBuilder::CONF_KEY_MAP, null); |
|
84 | - if ($user_map === null) { |
|
85 | - throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', ResponseBuilder::CONF_KEY_MAP)); |
|
86 | - } |
|
87 | - if (!\is_array($user_map)) { |
|
88 | - throw new \RuntimeException(sprintf('CONFIG: "%s" must be an array', ResponseBuilder::CONF_KEY_MAP)); |
|
89 | - } |
|
90 | - return Util::mergeConfig(BaseApiCodes::getBaseMap(), $user_map); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Returns locale mappings key for given api code or @null if there's no mapping |
|
95 | - * |
|
96 | - * @param integer $api_code Api code to look for mapped message for. |
|
97 | - * |
|
98 | - * @return string|null |
|
99 | - * |
|
100 | - * @throws \InvalidArgumentException If $code is not in allowed range. |
|
101 | - */ |
|
102 | - public static function getCodeMessageKey(int $api_code): ?string |
|
103 | - { |
|
104 | - if (!static::isCodeValid($api_code)) { |
|
105 | - $min = static::getMinCode(); |
|
106 | - $max = static::getMaxCode(); |
|
107 | - throw new \InvalidArgumentException("API code value ({$api_code}) is out of allowed range {$min}-{$max}"); |
|
108 | - } |
|
109 | - |
|
110 | - $map = static::getMap(); |
|
111 | - |
|
112 | - return $map[ $api_code ] ?? null; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Checks if given API $code can be used in current configuration. |
|
117 | - * |
|
118 | - * @param int $code API code to validate |
|
119 | - * |
|
120 | - * @return bool |
|
121 | - */ |
|
122 | - public static function isCodeValid(int $code): bool |
|
123 | - { |
|
124 | - return ($code === 0) || (($code >= static::getMinCode()) && ($code <= static::getMaxCode())); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Returns final API code for internal code, remapped to configured code range |
|
129 | - * |
|
130 | - * @param int $internal_code |
|
131 | - * |
|
132 | - * @return int |
|
133 | - * |
|
134 | - * @throws \InvalidArgumentException |
|
135 | - */ |
|
136 | - public static function getCodeForInternalOffset(int $internal_code): int |
|
137 | - { |
|
138 | - $min = static::RESERVED_MIN_API_CODE_OFFSET; |
|
139 | - $max = static::RESERVED_MAX_API_CODE_OFFSET; |
|
140 | - Validator::assertIsIntRange('internal_code', $internal_code, $min, $max); |
|
141 | - |
|
142 | - return ($internal_code === 0) ? 0 : $internal_code + static::getMinCode(); |
|
143 | - } |
|
24 | + /** |
|
25 | + * Returns lowest allowed error code for this module |
|
26 | + * |
|
27 | + * @return integer |
|
28 | + * |
|
29 | + * @throws \RuntimeException Throws exception if no min_code set up |
|
30 | + */ |
|
31 | + public static function getMinCode(): int |
|
32 | + { |
|
33 | + $key = ResponseBuilder::CONF_KEY_MIN_CODE; |
|
34 | + $min_code = Config::get($key, null); |
|
35 | + |
|
36 | + if ($min_code === null) { |
|
37 | + throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
38 | + } |
|
39 | + |
|
40 | + return $min_code; |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Returns highest allowed error code for this module |
|
45 | + * |
|
46 | + * @return integer |
|
47 | + * |
|
48 | + * @throws \RuntimeException Throws exception if no max_code set up |
|
49 | + */ |
|
50 | + public static function getMaxCode(): int |
|
51 | + { |
|
52 | + $key = ResponseBuilder::CONF_KEY_MAX_CODE; |
|
53 | + $max_code = Config::get($key, null); |
|
54 | + |
|
55 | + if ($max_code === null) { |
|
56 | + throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
57 | + } |
|
58 | + |
|
59 | + return $max_code; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Returns array of error code constants defined in this class. Used mainly for debugging/tests |
|
64 | + * |
|
65 | + * @return array |
|
66 | + * @throws \ReflectionException |
|
67 | + */ |
|
68 | + public static function getApiCodeConstants(): array |
|
69 | + { |
|
70 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
71 | + return (new \ReflectionClass(static::class))->getConstants(); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Returns complete error code to locale string mapping array |
|
76 | + * |
|
77 | + * @return array |
|
78 | + * |
|
79 | + * @throws \RuntimeException Thrown when builder map is not configured. |
|
80 | + */ |
|
81 | + public static function getMap(): array |
|
82 | + { |
|
83 | + $user_map = Config::get(ResponseBuilder::CONF_KEY_MAP, null); |
|
84 | + if ($user_map === null) { |
|
85 | + throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', ResponseBuilder::CONF_KEY_MAP)); |
|
86 | + } |
|
87 | + if (!\is_array($user_map)) { |
|
88 | + throw new \RuntimeException(sprintf('CONFIG: "%s" must be an array', ResponseBuilder::CONF_KEY_MAP)); |
|
89 | + } |
|
90 | + return Util::mergeConfig(BaseApiCodes::getBaseMap(), $user_map); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Returns locale mappings key for given api code or @null if there's no mapping |
|
95 | + * |
|
96 | + * @param integer $api_code Api code to look for mapped message for. |
|
97 | + * |
|
98 | + * @return string|null |
|
99 | + * |
|
100 | + * @throws \InvalidArgumentException If $code is not in allowed range. |
|
101 | + */ |
|
102 | + public static function getCodeMessageKey(int $api_code): ?string |
|
103 | + { |
|
104 | + if (!static::isCodeValid($api_code)) { |
|
105 | + $min = static::getMinCode(); |
|
106 | + $max = static::getMaxCode(); |
|
107 | + throw new \InvalidArgumentException("API code value ({$api_code}) is out of allowed range {$min}-{$max}"); |
|
108 | + } |
|
109 | + |
|
110 | + $map = static::getMap(); |
|
111 | + |
|
112 | + return $map[ $api_code ] ?? null; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Checks if given API $code can be used in current configuration. |
|
117 | + * |
|
118 | + * @param int $code API code to validate |
|
119 | + * |
|
120 | + * @return bool |
|
121 | + */ |
|
122 | + public static function isCodeValid(int $code): bool |
|
123 | + { |
|
124 | + return ($code === 0) || (($code >= static::getMinCode()) && ($code <= static::getMaxCode())); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Returns final API code for internal code, remapped to configured code range |
|
129 | + * |
|
130 | + * @param int $internal_code |
|
131 | + * |
|
132 | + * @return int |
|
133 | + * |
|
134 | + * @throws \InvalidArgumentException |
|
135 | + */ |
|
136 | + public static function getCodeForInternalOffset(int $internal_code): int |
|
137 | + { |
|
138 | + $min = static::RESERVED_MIN_API_CODE_OFFSET; |
|
139 | + $max = static::RESERVED_MAX_API_CODE_OFFSET; |
|
140 | + Validator::assertIsIntRange('internal_code', $internal_code, $min, $max); |
|
141 | + |
|
142 | + return ($internal_code === 0) ? 0 : $internal_code + static::getMinCode(); |
|
143 | + } |
|
144 | 144 | |
145 | 145 | } |
@@ -10,54 +10,54 @@ |
||
10 | 10 | */ |
11 | 11 | return [ |
12 | 12 | |
13 | - 'ok' => 'OK', |
|
14 | - 'no_error_message' => 'Błąd #:api_code', |
|
13 | + 'ok' => 'OK', |
|
14 | + 'no_error_message' => 'Błąd #:api_code', |
|
15 | 15 | |
16 | - // Used by Exception Handler Helper (when used) |
|
17 | - 'uncaught_exception' => 'Nieprzechwycony wyjątek: :message', |
|
18 | - 'http_exception' => 'Wyjątek HTTP: :message', |
|
16 | + // Used by Exception Handler Helper (when used) |
|
17 | + 'uncaught_exception' => 'Nieprzechwycony wyjątek: :message', |
|
18 | + 'http_exception' => 'Wyjątek HTTP: :message', |
|
19 | 19 | |
20 | - // HttpException handler (added in 6.4.0) |
|
21 | - // Error messages for HttpException caught w/o custom messages |
|
22 | - // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
23 | - 'http_400' => 'Bad Request', |
|
24 | - 'http_401' => 'Unauthorized', |
|
25 | - 'http_402' => 'Payment Required', |
|
26 | - 'http_403' => 'Forbidden', |
|
27 | - 'http_404' => 'Not Found', |
|
28 | - 'http_405' => 'Method Not Allowed', |
|
29 | - 'http_406' => 'Not Acceptable', |
|
30 | - 'http_407' => 'Proxy Authentication Required', |
|
31 | - 'http_408' => 'Request Timeout', |
|
32 | - 'http_409' => 'Conflict', |
|
33 | - 'http_410' => 'Gone', |
|
34 | - 'http_411' => 'Length Required', |
|
35 | - 'http_412' => 'Precondition Failed', |
|
36 | - 'http_413' => 'Payload Too Large', |
|
37 | - 'http_414' => 'URI Too Long', |
|
38 | - 'http_415' => 'Unsupported Media Type', |
|
39 | - 'http_416' => 'Range Not Satisfiable', |
|
40 | - 'http_417' => 'Expectation Failed', |
|
41 | - 'http_421' => 'Misdirected Request', |
|
42 | - 'http_422' => 'Unprocessable Entity', |
|
43 | - 'http_423' => 'Locked', |
|
44 | - 'http_424' => 'Failed Dependency', |
|
45 | - 'http_425' => 'Too Early', |
|
46 | - 'http_426' => 'Upgrade Required', |
|
47 | - 'http_428' => 'Precondition Required', |
|
48 | - 'http_429' => 'Too Many Requests', |
|
49 | - 'http_431' => 'Request Header Fields Too Large', |
|
50 | - 'http_451' => 'Unavailable For Legal Reasons', |
|
51 | - 'http_500' => 'Internal Server Error', |
|
52 | - 'http_501' => 'Not Implemented', |
|
53 | - 'http_502' => 'Bad Gateway', |
|
54 | - 'http_503' => 'Service Unavailable', |
|
55 | - 'http_504' => 'Gateway Timeout', |
|
56 | - 'http_505' => 'HTTP Version Not Supported', |
|
57 | - 'http_506' => 'Variant Also Negotiates', |
|
58 | - 'http_507' => 'Insufficient Storage', |
|
59 | - 'http_508' => 'Loop Detected', |
|
60 | - 'http_509' => 'Unassigned', |
|
61 | - 'http_510' => 'Not Extended', |
|
62 | - 'http_511' => 'Network Authentication Required', |
|
20 | + // HttpException handler (added in 6.4.0) |
|
21 | + // Error messages for HttpException caught w/o custom messages |
|
22 | + // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
23 | + 'http_400' => 'Bad Request', |
|
24 | + 'http_401' => 'Unauthorized', |
|
25 | + 'http_402' => 'Payment Required', |
|
26 | + 'http_403' => 'Forbidden', |
|
27 | + 'http_404' => 'Not Found', |
|
28 | + 'http_405' => 'Method Not Allowed', |
|
29 | + 'http_406' => 'Not Acceptable', |
|
30 | + 'http_407' => 'Proxy Authentication Required', |
|
31 | + 'http_408' => 'Request Timeout', |
|
32 | + 'http_409' => 'Conflict', |
|
33 | + 'http_410' => 'Gone', |
|
34 | + 'http_411' => 'Length Required', |
|
35 | + 'http_412' => 'Precondition Failed', |
|
36 | + 'http_413' => 'Payload Too Large', |
|
37 | + 'http_414' => 'URI Too Long', |
|
38 | + 'http_415' => 'Unsupported Media Type', |
|
39 | + 'http_416' => 'Range Not Satisfiable', |
|
40 | + 'http_417' => 'Expectation Failed', |
|
41 | + 'http_421' => 'Misdirected Request', |
|
42 | + 'http_422' => 'Unprocessable Entity', |
|
43 | + 'http_423' => 'Locked', |
|
44 | + 'http_424' => 'Failed Dependency', |
|
45 | + 'http_425' => 'Too Early', |
|
46 | + 'http_426' => 'Upgrade Required', |
|
47 | + 'http_428' => 'Precondition Required', |
|
48 | + 'http_429' => 'Too Many Requests', |
|
49 | + 'http_431' => 'Request Header Fields Too Large', |
|
50 | + 'http_451' => 'Unavailable For Legal Reasons', |
|
51 | + 'http_500' => 'Internal Server Error', |
|
52 | + 'http_501' => 'Not Implemented', |
|
53 | + 'http_502' => 'Bad Gateway', |
|
54 | + 'http_503' => 'Service Unavailable', |
|
55 | + 'http_504' => 'Gateway Timeout', |
|
56 | + 'http_505' => 'HTTP Version Not Supported', |
|
57 | + 'http_506' => 'Variant Also Negotiates', |
|
58 | + 'http_507' => 'Insufficient Storage', |
|
59 | + 'http_508' => 'Loop Detected', |
|
60 | + 'http_509' => 'Unassigned', |
|
61 | + 'http_510' => 'Not Extended', |
|
62 | + 'http_511' => 'Network Authentication Required', |
|
63 | 63 | ]; |
@@ -10,55 +10,55 @@ |
||
10 | 10 | */ |
11 | 11 | return [ |
12 | 12 | |
13 | - 'ok' => 'OK', |
|
14 | - 'no_error_message' => 'Error #:api_code', |
|
13 | + 'ok' => 'OK', |
|
14 | + 'no_error_message' => 'Error #:api_code', |
|
15 | 15 | |
16 | - // Used by Exception Handler Helper (when used) |
|
17 | - 'uncaught_exception' => 'Uncaught exception: :message', |
|
18 | - 'http_exception' => 'HTTP exception: :message', |
|
16 | + // Used by Exception Handler Helper (when used) |
|
17 | + 'uncaught_exception' => 'Uncaught exception: :message', |
|
18 | + 'http_exception' => 'HTTP exception: :message', |
|
19 | 19 | |
20 | - // HttpException handler (added in 6.4.0) |
|
21 | - // Error messages for HttpException caught w/o custom messages |
|
22 | - // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
23 | - 'http_400' => 'Bad Request', |
|
24 | - 'http_401' => 'Unauthorized', |
|
25 | - 'http_402' => 'Payment Required', |
|
26 | - 'http_403' => 'Forbidden', |
|
27 | - 'http_404' => 'Not Found', |
|
28 | - 'http_405' => 'Method Not Allowed', |
|
29 | - 'http_406' => 'Not Acceptable', |
|
30 | - 'http_407' => 'Proxy Authentication Required', |
|
31 | - 'http_408' => 'Request Timeout', |
|
32 | - 'http_409' => 'Conflict', |
|
33 | - 'http_410' => 'Gone', |
|
34 | - 'http_411' => 'Length Required', |
|
35 | - 'http_412' => 'Precondition Failed', |
|
36 | - 'http_413' => 'Payload Too Large', |
|
37 | - 'http_414' => 'URI Too Long', |
|
38 | - 'http_415' => 'Unsupported Media Type', |
|
39 | - 'http_416' => 'Range Not Satisfiable', |
|
40 | - 'http_417' => 'Expectation Failed', |
|
41 | - 'http_421' => 'Misdirected Request', |
|
42 | - 'http_422' => 'Unprocessable Entity', |
|
43 | - 'http_423' => 'Locked', |
|
44 | - 'http_424' => 'Failed Dependency', |
|
45 | - 'http_425' => 'Too Early', |
|
46 | - 'http_426' => 'Upgrade Required', |
|
47 | - 'http_428' => 'Precondition Required', |
|
48 | - 'http_429' => 'Too Many Requests', |
|
49 | - 'http_431' => 'Request Header Fields Too Large', |
|
50 | - 'http_451' => 'Unavailable For Legal Reasons', |
|
51 | - 'http_500' => 'Internal Server Error', |
|
52 | - 'http_501' => 'Not Implemented', |
|
53 | - 'http_502' => 'Bad Gateway', |
|
54 | - 'http_503' => 'Service Unavailable', |
|
55 | - 'http_504' => 'Gateway Timeout', |
|
56 | - 'http_505' => 'HTTP Version Not Supported', |
|
57 | - 'http_506' => 'Variant Also Negotiates', |
|
58 | - 'http_507' => 'Insufficient Storage', |
|
59 | - 'http_508' => 'Loop Detected', |
|
60 | - 'http_509' => 'Unassigned', |
|
61 | - 'http_510' => 'Not Extended', |
|
62 | - 'http_511' => 'Network Authentication Required', |
|
20 | + // HttpException handler (added in 6.4.0) |
|
21 | + // Error messages for HttpException caught w/o custom messages |
|
22 | + // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
23 | + 'http_400' => 'Bad Request', |
|
24 | + 'http_401' => 'Unauthorized', |
|
25 | + 'http_402' => 'Payment Required', |
|
26 | + 'http_403' => 'Forbidden', |
|
27 | + 'http_404' => 'Not Found', |
|
28 | + 'http_405' => 'Method Not Allowed', |
|
29 | + 'http_406' => 'Not Acceptable', |
|
30 | + 'http_407' => 'Proxy Authentication Required', |
|
31 | + 'http_408' => 'Request Timeout', |
|
32 | + 'http_409' => 'Conflict', |
|
33 | + 'http_410' => 'Gone', |
|
34 | + 'http_411' => 'Length Required', |
|
35 | + 'http_412' => 'Precondition Failed', |
|
36 | + 'http_413' => 'Payload Too Large', |
|
37 | + 'http_414' => 'URI Too Long', |
|
38 | + 'http_415' => 'Unsupported Media Type', |
|
39 | + 'http_416' => 'Range Not Satisfiable', |
|
40 | + 'http_417' => 'Expectation Failed', |
|
41 | + 'http_421' => 'Misdirected Request', |
|
42 | + 'http_422' => 'Unprocessable Entity', |
|
43 | + 'http_423' => 'Locked', |
|
44 | + 'http_424' => 'Failed Dependency', |
|
45 | + 'http_425' => 'Too Early', |
|
46 | + 'http_426' => 'Upgrade Required', |
|
47 | + 'http_428' => 'Precondition Required', |
|
48 | + 'http_429' => 'Too Many Requests', |
|
49 | + 'http_431' => 'Request Header Fields Too Large', |
|
50 | + 'http_451' => 'Unavailable For Legal Reasons', |
|
51 | + 'http_500' => 'Internal Server Error', |
|
52 | + 'http_501' => 'Not Implemented', |
|
53 | + 'http_502' => 'Bad Gateway', |
|
54 | + 'http_503' => 'Service Unavailable', |
|
55 | + 'http_504' => 'Gateway Timeout', |
|
56 | + 'http_505' => 'HTTP Version Not Supported', |
|
57 | + 'http_506' => 'Variant Also Negotiates', |
|
58 | + 'http_507' => 'Insufficient Storage', |
|
59 | + 'http_508' => 'Loop Detected', |
|
60 | + 'http_509' => 'Unassigned', |
|
61 | + 'http_510' => 'Not Extended', |
|
62 | + 'http_511' => 'Network Authentication Required', |
|
63 | 63 | ]; |
64 | 64 |
@@ -10,56 +10,56 @@ |
||
10 | 10 | */ |
11 | 11 | return [ |
12 | 12 | |
13 | - 'ok' => 'موفق', |
|
14 | - 'no_error_message' => 'خطای شماره :api_code', |
|
13 | + 'ok' => 'موفق', |
|
14 | + 'no_error_message' => 'خطای شماره :api_code', |
|
15 | 15 | |
16 | - // can be used by Exception Handler (if enabled) |
|
17 | - 'uncaught_exception' => 'استثناء مدیریت نشده :message', |
|
18 | - // we talking API call here, hence we have http_not_found |
|
19 | - 'http_exception' => 'استثناء HTTP :message', |
|
16 | + // can be used by Exception Handler (if enabled) |
|
17 | + 'uncaught_exception' => 'استثناء مدیریت نشده :message', |
|
18 | + // we talking API call here, hence we have http_not_found |
|
19 | + 'http_exception' => 'استثناء HTTP :message', |
|
20 | 20 | |
21 | - // HttpException handler (added in 6.4.0) |
|
22 | - // Error messages for HttpException caught w/o custom messages |
|
23 | - // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
24 | - 'http_400' => 'Bad Request', |
|
25 | - 'http_401' => 'اجازه دسترسی ندارید', |
|
26 | - 'http_402' => 'Payment Required', |
|
27 | - 'http_403' => 'Forbidden', |
|
28 | - 'http_404' => 'Not Found', |
|
29 | - 'http_405' => 'Method Not Allowed', |
|
30 | - 'http_406' => 'Not Acceptable', |
|
31 | - 'http_407' => 'Proxy Authentication Required', |
|
32 | - 'http_408' => 'Request Timeout', |
|
33 | - 'http_409' => 'Conflict', |
|
34 | - 'http_410' => 'Gone', |
|
35 | - 'http_411' => 'Length Required', |
|
36 | - 'http_412' => 'Precondition Failed', |
|
37 | - 'http_413' => 'Payload Too Large', |
|
38 | - 'http_414' => 'URI Too Long', |
|
39 | - 'http_415' => 'Unsupported Media Type', |
|
40 | - 'http_416' => 'Range Not Satisfiable', |
|
41 | - 'http_417' => 'Expectation Failed', |
|
42 | - 'http_421' => 'Misdirected Request', |
|
43 | - 'http_422' => 'داده معتبر نیست', |
|
44 | - 'http_423' => 'Locked', |
|
45 | - 'http_424' => 'Failed Dependency', |
|
46 | - 'http_425' => 'Too Early', |
|
47 | - 'http_426' => 'Upgrade Required', |
|
48 | - 'http_428' => 'Precondition Required', |
|
49 | - 'http_429' => 'Too Many Requests', |
|
50 | - 'http_431' => 'Request Header Fields Too Large', |
|
51 | - 'http_451' => 'Unavailable For Legal Reasons', |
|
52 | - 'http_500' => 'Internal Server Error', |
|
53 | - 'http_501' => 'Not Implemented', |
|
54 | - 'http_502' => 'Bad Gateway', |
|
55 | - 'http_503' => 'Service Unavailable', |
|
56 | - 'http_504' => 'Gateway Timeout', |
|
57 | - 'http_505' => 'HTTP Version Not Supported', |
|
58 | - 'http_506' => 'Variant Also Negotiates', |
|
59 | - 'http_507' => 'Insufficient Storage', |
|
60 | - 'http_508' => 'Loop Detected', |
|
61 | - 'http_509' => 'Unassigned', |
|
62 | - 'http_510' => 'Not Extended', |
|
63 | - 'http_511' => 'Network Authentication Required', |
|
21 | + // HttpException handler (added in 6.4.0) |
|
22 | + // Error messages for HttpException caught w/o custom messages |
|
23 | + // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
24 | + 'http_400' => 'Bad Request', |
|
25 | + 'http_401' => 'اجازه دسترسی ندارید', |
|
26 | + 'http_402' => 'Payment Required', |
|
27 | + 'http_403' => 'Forbidden', |
|
28 | + 'http_404' => 'Not Found', |
|
29 | + 'http_405' => 'Method Not Allowed', |
|
30 | + 'http_406' => 'Not Acceptable', |
|
31 | + 'http_407' => 'Proxy Authentication Required', |
|
32 | + 'http_408' => 'Request Timeout', |
|
33 | + 'http_409' => 'Conflict', |
|
34 | + 'http_410' => 'Gone', |
|
35 | + 'http_411' => 'Length Required', |
|
36 | + 'http_412' => 'Precondition Failed', |
|
37 | + 'http_413' => 'Payload Too Large', |
|
38 | + 'http_414' => 'URI Too Long', |
|
39 | + 'http_415' => 'Unsupported Media Type', |
|
40 | + 'http_416' => 'Range Not Satisfiable', |
|
41 | + 'http_417' => 'Expectation Failed', |
|
42 | + 'http_421' => 'Misdirected Request', |
|
43 | + 'http_422' => 'داده معتبر نیست', |
|
44 | + 'http_423' => 'Locked', |
|
45 | + 'http_424' => 'Failed Dependency', |
|
46 | + 'http_425' => 'Too Early', |
|
47 | + 'http_426' => 'Upgrade Required', |
|
48 | + 'http_428' => 'Precondition Required', |
|
49 | + 'http_429' => 'Too Many Requests', |
|
50 | + 'http_431' => 'Request Header Fields Too Large', |
|
51 | + 'http_451' => 'Unavailable For Legal Reasons', |
|
52 | + 'http_500' => 'Internal Server Error', |
|
53 | + 'http_501' => 'Not Implemented', |
|
54 | + 'http_502' => 'Bad Gateway', |
|
55 | + 'http_503' => 'Service Unavailable', |
|
56 | + 'http_504' => 'Gateway Timeout', |
|
57 | + 'http_505' => 'HTTP Version Not Supported', |
|
58 | + 'http_506' => 'Variant Also Negotiates', |
|
59 | + 'http_507' => 'Insufficient Storage', |
|
60 | + 'http_508' => 'Loop Detected', |
|
61 | + 'http_509' => 'Unassigned', |
|
62 | + 'http_510' => 'Not Extended', |
|
63 | + 'http_511' => 'Network Authentication Required', |
|
64 | 64 | ]; |
65 | 65 |
@@ -15,14 +15,14 @@ |
||
15 | 15 | */ |
16 | 16 | interface ConverterContract |
17 | 17 | { |
18 | - /** |
|
19 | - * Returns array representation of the object. |
|
20 | - * |
|
21 | - * @param object $obj Object to be converted |
|
22 | - * @param array $config Converter config array to be used for this object (based on exact class |
|
23 | - * name match or inheritance). |
|
24 | - * |
|
25 | - * @return array |
|
26 | - */ |
|
27 | - public function convert($obj, array $config): array; |
|
18 | + /** |
|
19 | + * Returns array representation of the object. |
|
20 | + * |
|
21 | + * @param object $obj Object to be converted |
|
22 | + * @param array $config Converter config array to be used for this object (based on exact class |
|
23 | + * name match or inheritance). |
|
24 | + * |
|
25 | + * @return array |
|
26 | + */ |
|
27 | + public function convert($obj, array $config): array; |
|
28 | 28 | } |
@@ -19,81 +19,81 @@ |
||
19 | 19 | |
20 | 20 | abstract class ResponseBuilderBase |
21 | 21 | { |
22 | - /** |
|
23 | - * Default HTTP code to be used with success responses |
|
24 | - * |
|
25 | - * @var int |
|
26 | - */ |
|
27 | - public const DEFAULT_HTTP_CODE_OK = HttpResponse::HTTP_OK; |
|
22 | + /** |
|
23 | + * Default HTTP code to be used with success responses |
|
24 | + * |
|
25 | + * @var int |
|
26 | + */ |
|
27 | + public const DEFAULT_HTTP_CODE_OK = HttpResponse::HTTP_OK; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Default HTTP code to be used with error responses |
|
31 | - * |
|
32 | - * @var int |
|
33 | - */ |
|
34 | - public const DEFAULT_HTTP_CODE_ERROR = HttpResponse::HTTP_BAD_REQUEST; |
|
29 | + /** |
|
30 | + * Default HTTP code to be used with error responses |
|
31 | + * |
|
32 | + * @var int |
|
33 | + */ |
|
34 | + public const DEFAULT_HTTP_CODE_ERROR = HttpResponse::HTTP_BAD_REQUEST; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Min allowed HTTP code for errorXXX() |
|
38 | - * |
|
39 | - * @var int |
|
40 | - */ |
|
41 | - public const ERROR_HTTP_CODE_MIN = 400; |
|
36 | + /** |
|
37 | + * Min allowed HTTP code for errorXXX() |
|
38 | + * |
|
39 | + * @var int |
|
40 | + */ |
|
41 | + public const ERROR_HTTP_CODE_MIN = 400; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Max allowed HTTP code for errorXXX() |
|
45 | - * |
|
46 | - * @var int |
|
47 | - */ |
|
48 | - public const ERROR_HTTP_CODE_MAX = 599; |
|
43 | + /** |
|
44 | + * Max allowed HTTP code for errorXXX() |
|
45 | + * |
|
46 | + * @var int |
|
47 | + */ |
|
48 | + public const ERROR_HTTP_CODE_MAX = 599; |
|
49 | 49 | |
50 | - /** |
|
51 | - * Configuration keys |
|
52 | - */ |
|
53 | - public const CONF_CONFIG = 'response_builder'; |
|
54 | - public const CONF_KEY_DEBUG_DEBUG_KEY = self::CONF_CONFIG . '.debug.debug_key'; |
|
55 | - public const CONF_KEY_DEBUG_EX_TRACE_ENABLED = self::CONF_CONFIG . '.debug.exception_handler.trace_enabled'; |
|
56 | - public const CONF_KEY_DEBUG_EX_TRACE_KEY = self::CONF_CONFIG . '.debug.exception_handler.trace_key'; |
|
57 | - public const CONF_KEY_MAP = self::CONF_CONFIG . '.map'; |
|
58 | - public const CONF_KEY_ENCODING_OPTIONS = self::CONF_CONFIG . '.encoding_options'; |
|
59 | - public const CONF_KEY_CONVERTER = self::CONF_CONFIG . '.converter'; |
|
60 | - public const CONF_KEY_MIN_CODE = self::CONF_CONFIG . '.min_code'; |
|
61 | - public const CONF_KEY_MAX_CODE = self::CONF_CONFIG . '.max_code'; |
|
62 | - public const CONF_KEY_EXCEPTION_HANDLER = self::CONF_CONFIG . '.exception_handler'; |
|
50 | + /** |
|
51 | + * Configuration keys |
|
52 | + */ |
|
53 | + public const CONF_CONFIG = 'response_builder'; |
|
54 | + public const CONF_KEY_DEBUG_DEBUG_KEY = self::CONF_CONFIG . '.debug.debug_key'; |
|
55 | + public const CONF_KEY_DEBUG_EX_TRACE_ENABLED = self::CONF_CONFIG . '.debug.exception_handler.trace_enabled'; |
|
56 | + public const CONF_KEY_DEBUG_EX_TRACE_KEY = self::CONF_CONFIG . '.debug.exception_handler.trace_key'; |
|
57 | + public const CONF_KEY_MAP = self::CONF_CONFIG . '.map'; |
|
58 | + public const CONF_KEY_ENCODING_OPTIONS = self::CONF_CONFIG . '.encoding_options'; |
|
59 | + public const CONF_KEY_CONVERTER = self::CONF_CONFIG . '.converter'; |
|
60 | + public const CONF_KEY_MIN_CODE = self::CONF_CONFIG . '.min_code'; |
|
61 | + public const CONF_KEY_MAX_CODE = self::CONF_CONFIG . '.max_code'; |
|
62 | + public const CONF_KEY_EXCEPTION_HANDLER = self::CONF_CONFIG . '.exception_handler'; |
|
63 | 63 | |
64 | - /** |
|
65 | - * Default keys to be used by exception handler while adding debug information |
|
66 | - */ |
|
67 | - public const KEY_DEBUG = 'debug'; |
|
68 | - public const KEY_TRACE = 'trace'; |
|
69 | - public const KEY_CLASS = 'class'; |
|
70 | - public const KEY_FILE = 'file'; |
|
71 | - public const KEY_LINE = 'line'; |
|
72 | - public const KEY_KEY = 'key'; |
|
73 | - public const KEY_PRI = 'pri'; |
|
74 | - public const KEY_HANDLER = 'handler'; |
|
75 | - public const KEY_SUCCESS = 'success'; |
|
76 | - public const KEY_CODE = 'code'; |
|
77 | - public const KEY_LOCALE = 'locale'; |
|
78 | - public const KEY_MESSAGE = 'message'; |
|
79 | - public const KEY_DATA = 'data'; |
|
64 | + /** |
|
65 | + * Default keys to be used by exception handler while adding debug information |
|
66 | + */ |
|
67 | + public const KEY_DEBUG = 'debug'; |
|
68 | + public const KEY_TRACE = 'trace'; |
|
69 | + public const KEY_CLASS = 'class'; |
|
70 | + public const KEY_FILE = 'file'; |
|
71 | + public const KEY_LINE = 'line'; |
|
72 | + public const KEY_KEY = 'key'; |
|
73 | + public const KEY_PRI = 'pri'; |
|
74 | + public const KEY_HANDLER = 'handler'; |
|
75 | + public const KEY_SUCCESS = 'success'; |
|
76 | + public const KEY_CODE = 'code'; |
|
77 | + public const KEY_LOCALE = 'locale'; |
|
78 | + public const KEY_MESSAGE = 'message'; |
|
79 | + public const KEY_DATA = 'data'; |
|
80 | 80 | |
81 | - /** |
|
82 | - * Default key to be used by exception handler while processing ValidationException |
|
83 | - * to return all the error messages |
|
84 | - * |
|
85 | - * @var string |
|
86 | - */ |
|
87 | - public const KEY_MESSAGES = 'messages'; |
|
81 | + /** |
|
82 | + * Default key to be used by exception handler while processing ValidationException |
|
83 | + * to return all the error messages |
|
84 | + * |
|
85 | + * @var string |
|
86 | + */ |
|
87 | + public const KEY_MESSAGES = 'messages'; |
|
88 | 88 | |
89 | - /** |
|
90 | - * Default JSON encoding options. Must be specified as final value (i.e. 271) and NOT |
|
91 | - * PHP expression i.e. `JSON_HEX_TAG|JSON_HEX_APOS|...` as such syntax is not yet supported |
|
92 | - * by PHP. |
|
93 | - * |
|
94 | - * 271 = JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_AMP|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE |
|
95 | - * |
|
96 | - * @var int |
|
97 | - */ |
|
98 | - public const DEFAULT_ENCODING_OPTIONS = 271; |
|
89 | + /** |
|
90 | + * Default JSON encoding options. Must be specified as final value (i.e. 271) and NOT |
|
91 | + * PHP expression i.e. `JSON_HEX_TAG|JSON_HEX_APOS|...` as such syntax is not yet supported |
|
92 | + * by PHP. |
|
93 | + * |
|
94 | + * 271 = JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_AMP|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE |
|
95 | + * |
|
96 | + * @var int |
|
97 | + */ |
|
98 | + public const DEFAULT_ENCODING_OPTIONS = 271; |
|
99 | 99 | } |
@@ -21,194 +21,194 @@ |
||
21 | 21 | */ |
22 | 22 | class BaseApiCodes |
23 | 23 | { |
24 | - use ApiCodesHelpers; |
|
25 | - |
|
26 | - /** |
|
27 | - * protected code range - lowest code for reserved range. |
|
28 | - * |
|
29 | - * @var int |
|
30 | - */ |
|
31 | - public const RESERVED_MIN_API_CODE_OFFSET = 0; |
|
32 | - |
|
33 | - /** |
|
34 | - * protected code range - highest code for reserved range |
|
35 | - * |
|
36 | - * @var int |
|
37 | - */ |
|
38 | - public const RESERVED_MAX_API_CODE_OFFSET = 19; |
|
39 | - |
|
40 | - /** |
|
41 | - * built-in codes: OK |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - protected const OK_OFFSET = 0; |
|
46 | - /** |
|
47 | - * built-in code for fallback message mapping |
|
48 | - * |
|
49 | - * @var int |
|
50 | - */ |
|
51 | - protected const NO_ERROR_MESSAGE_OFFSET = 1; |
|
52 | - /** |
|
53 | - * built-in error code for HTTP_NOT_FOUND exception |
|
54 | - * |
|
55 | - * @var int |
|
56 | - */ |
|
57 | - protected const EX_HTTP_NOT_FOUND_OFFSET = 10; |
|
58 | - /** |
|
59 | - * built-in error code for HTTP_SERVICE_UNAVAILABLE exception |
|
60 | - * |
|
61 | - * @var int |
|
62 | - */ |
|
63 | - protected const EX_HTTP_SERVICE_UNAVAILABLE_OFFSET = 11; |
|
64 | - /** |
|
65 | - * built-in error code for HTTP_EXCEPTION |
|
66 | - * |
|
67 | - * @var int |
|
68 | - */ |
|
69 | - protected const EX_HTTP_EXCEPTION_OFFSET = 12; |
|
70 | - /** |
|
71 | - * built-in error code for UNCAUGHT_EXCEPTION |
|
72 | - * |
|
73 | - * @var int |
|
74 | - */ |
|
75 | - protected const EX_UNCAUGHT_EXCEPTION_OFFSET = 13; |
|
76 | - |
|
77 | - /** |
|
78 | - * built-in error code for \Illuminate\Auth\AuthenticationException |
|
79 | - * |
|
80 | - * @var int |
|
81 | - */ |
|
82 | - protected const EX_AUTHENTICATION_EXCEPTION_OFFSET = 14; |
|
83 | - |
|
84 | - /** |
|
85 | - * built-in error code for \Illuminate\Auth\AuthenticationException |
|
86 | - * |
|
87 | - * @var int |
|
88 | - */ |
|
89 | - protected const EX_VALIDATION_EXCEPTION_OFFSET = 15; |
|
90 | - |
|
91 | - /** |
|
92 | - * Returns base code mapping array |
|
93 | - * |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - protected static function getBaseMap(): array |
|
97 | - { |
|
98 | - $tpl = 'response-builder::builder.http_%d'; |
|
99 | - |
|
100 | - return [ |
|
101 | - /** @scrutinizer ignore-deprecated */ |
|
102 | - self::OK() => 'response-builder::builder.ok', |
|
103 | - /** @scrutinizer ignore-deprecated */ |
|
104 | - self::NO_ERROR_MESSAGE() => 'response-builder::builder.no_error_message', |
|
105 | - /** @scrutinizer ignore-deprecated */ |
|
106 | - self::EX_HTTP_EXCEPTION() => 'response-builder::builder.http_exception', |
|
107 | - /** @scrutinizer ignore-deprecated */ |
|
108 | - self::EX_UNCAUGHT_EXCEPTION() => 'response-builder::builder.uncaught_exception', |
|
109 | - /** @scrutinizer ignore-deprecated */ |
|
110 | - self::EX_HTTP_NOT_FOUND() => sprintf($tpl, HttpResponse::HTTP_NOT_FOUND), |
|
111 | - /** @scrutinizer ignore-deprecated */ |
|
112 | - self::EX_HTTP_SERVICE_UNAVAILABLE() => sprintf($tpl, HttpResponse::HTTP_SERVICE_UNAVAILABLE), |
|
113 | - /** @scrutinizer ignore-deprecated */ |
|
114 | - self::EX_AUTHENTICATION_EXCEPTION() => sprintf($tpl, HttpResponse::HTTP_UNAUTHORIZED), |
|
115 | - /** @scrutinizer ignore-deprecated */ |
|
116 | - self::EX_VALIDATION_EXCEPTION() => sprintf($tpl, HttpResponse::HTTP_UNPROCESSABLE_ENTITY), |
|
117 | - ]; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Returns API code for internal code OK |
|
122 | - * |
|
123 | - * @return int valid API code in current range |
|
124 | - * |
|
125 | - * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
126 | - */ |
|
127 | - public static function OK(): int |
|
128 | - { |
|
129 | - return static::getCodeForInternalOffset(static::OK_OFFSET); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Returns API code for internal code NO_ERROR_MESSAGE |
|
134 | - * |
|
135 | - * @return int valid API code in current range |
|
136 | - */ |
|
137 | - public static function NO_ERROR_MESSAGE(): int |
|
138 | - { |
|
139 | - return static::getCodeForInternalOffset(static::NO_ERROR_MESSAGE_OFFSET); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Returns API code for internal code EX_HTTP_NOT_FOUND |
|
144 | - * |
|
145 | - * @return int valid API code in current range |
|
146 | - * |
|
147 | - * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
148 | - */ |
|
149 | - public static function EX_HTTP_NOT_FOUND(): int |
|
150 | - { |
|
151 | - return static::getCodeForInternalOffset(static::EX_HTTP_NOT_FOUND_OFFSET); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Returns API code for internal code EX_HTTP_EXCEPTION |
|
156 | - * |
|
157 | - * @return int valid API code in current range |
|
158 | - * |
|
159 | - * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
160 | - */ |
|
161 | - public static function EX_HTTP_EXCEPTION(): int |
|
162 | - { |
|
163 | - return static::getCodeForInternalOffset(static::EX_HTTP_EXCEPTION_OFFSET); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Returns API code for internal code EX_UNCAUGHT_EXCEPTION |
|
168 | - * |
|
169 | - * @return int valid API code in current range |
|
170 | - * |
|
171 | - * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
172 | - */ |
|
173 | - public static function EX_UNCAUGHT_EXCEPTION(): int |
|
174 | - { |
|
175 | - return static::getCodeForInternalOffset(static::EX_UNCAUGHT_EXCEPTION_OFFSET); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Returns API code for internal code EX_AUTHENTICATION_EXCEPTION |
|
180 | - * |
|
181 | - * @return int valid API code in current range |
|
182 | - * |
|
183 | - * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
184 | - */ |
|
185 | - public static function EX_AUTHENTICATION_EXCEPTION(): int |
|
186 | - { |
|
187 | - return static::getCodeForInternalOffset(static::EX_AUTHENTICATION_EXCEPTION_OFFSET); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Returns API code for internal code EX_VALIDATION_EXCEPTION |
|
192 | - * |
|
193 | - * @return int valid API code in current range |
|
194 | - * |
|
195 | - * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
196 | - */ |
|
197 | - public static function EX_VALIDATION_EXCEPTION(): int |
|
198 | - { |
|
199 | - return static::getCodeForInternalOffset(static::EX_VALIDATION_EXCEPTION_OFFSET); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Returns API code for internal code EX_HTTP_SERVICE_UNAVAILABLE |
|
204 | - * |
|
205 | - * @return int valid API code in current range |
|
206 | - * |
|
207 | - * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
208 | - */ |
|
209 | - public static function EX_HTTP_SERVICE_UNAVAILABLE(): int |
|
210 | - { |
|
211 | - return static::getCodeForInternalOffset(static::EX_HTTP_SERVICE_UNAVAILABLE_OFFSET); |
|
212 | - } |
|
24 | + use ApiCodesHelpers; |
|
25 | + |
|
26 | + /** |
|
27 | + * protected code range - lowest code for reserved range. |
|
28 | + * |
|
29 | + * @var int |
|
30 | + */ |
|
31 | + public const RESERVED_MIN_API_CODE_OFFSET = 0; |
|
32 | + |
|
33 | + /** |
|
34 | + * protected code range - highest code for reserved range |
|
35 | + * |
|
36 | + * @var int |
|
37 | + */ |
|
38 | + public const RESERVED_MAX_API_CODE_OFFSET = 19; |
|
39 | + |
|
40 | + /** |
|
41 | + * built-in codes: OK |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + protected const OK_OFFSET = 0; |
|
46 | + /** |
|
47 | + * built-in code for fallback message mapping |
|
48 | + * |
|
49 | + * @var int |
|
50 | + */ |
|
51 | + protected const NO_ERROR_MESSAGE_OFFSET = 1; |
|
52 | + /** |
|
53 | + * built-in error code for HTTP_NOT_FOUND exception |
|
54 | + * |
|
55 | + * @var int |
|
56 | + */ |
|
57 | + protected const EX_HTTP_NOT_FOUND_OFFSET = 10; |
|
58 | + /** |
|
59 | + * built-in error code for HTTP_SERVICE_UNAVAILABLE exception |
|
60 | + * |
|
61 | + * @var int |
|
62 | + */ |
|
63 | + protected const EX_HTTP_SERVICE_UNAVAILABLE_OFFSET = 11; |
|
64 | + /** |
|
65 | + * built-in error code for HTTP_EXCEPTION |
|
66 | + * |
|
67 | + * @var int |
|
68 | + */ |
|
69 | + protected const EX_HTTP_EXCEPTION_OFFSET = 12; |
|
70 | + /** |
|
71 | + * built-in error code for UNCAUGHT_EXCEPTION |
|
72 | + * |
|
73 | + * @var int |
|
74 | + */ |
|
75 | + protected const EX_UNCAUGHT_EXCEPTION_OFFSET = 13; |
|
76 | + |
|
77 | + /** |
|
78 | + * built-in error code for \Illuminate\Auth\AuthenticationException |
|
79 | + * |
|
80 | + * @var int |
|
81 | + */ |
|
82 | + protected const EX_AUTHENTICATION_EXCEPTION_OFFSET = 14; |
|
83 | + |
|
84 | + /** |
|
85 | + * built-in error code for \Illuminate\Auth\AuthenticationException |
|
86 | + * |
|
87 | + * @var int |
|
88 | + */ |
|
89 | + protected const EX_VALIDATION_EXCEPTION_OFFSET = 15; |
|
90 | + |
|
91 | + /** |
|
92 | + * Returns base code mapping array |
|
93 | + * |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + protected static function getBaseMap(): array |
|
97 | + { |
|
98 | + $tpl = 'response-builder::builder.http_%d'; |
|
99 | + |
|
100 | + return [ |
|
101 | + /** @scrutinizer ignore-deprecated */ |
|
102 | + self::OK() => 'response-builder::builder.ok', |
|
103 | + /** @scrutinizer ignore-deprecated */ |
|
104 | + self::NO_ERROR_MESSAGE() => 'response-builder::builder.no_error_message', |
|
105 | + /** @scrutinizer ignore-deprecated */ |
|
106 | + self::EX_HTTP_EXCEPTION() => 'response-builder::builder.http_exception', |
|
107 | + /** @scrutinizer ignore-deprecated */ |
|
108 | + self::EX_UNCAUGHT_EXCEPTION() => 'response-builder::builder.uncaught_exception', |
|
109 | + /** @scrutinizer ignore-deprecated */ |
|
110 | + self::EX_HTTP_NOT_FOUND() => sprintf($tpl, HttpResponse::HTTP_NOT_FOUND), |
|
111 | + /** @scrutinizer ignore-deprecated */ |
|
112 | + self::EX_HTTP_SERVICE_UNAVAILABLE() => sprintf($tpl, HttpResponse::HTTP_SERVICE_UNAVAILABLE), |
|
113 | + /** @scrutinizer ignore-deprecated */ |
|
114 | + self::EX_AUTHENTICATION_EXCEPTION() => sprintf($tpl, HttpResponse::HTTP_UNAUTHORIZED), |
|
115 | + /** @scrutinizer ignore-deprecated */ |
|
116 | + self::EX_VALIDATION_EXCEPTION() => sprintf($tpl, HttpResponse::HTTP_UNPROCESSABLE_ENTITY), |
|
117 | + ]; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Returns API code for internal code OK |
|
122 | + * |
|
123 | + * @return int valid API code in current range |
|
124 | + * |
|
125 | + * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
126 | + */ |
|
127 | + public static function OK(): int |
|
128 | + { |
|
129 | + return static::getCodeForInternalOffset(static::OK_OFFSET); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Returns API code for internal code NO_ERROR_MESSAGE |
|
134 | + * |
|
135 | + * @return int valid API code in current range |
|
136 | + */ |
|
137 | + public static function NO_ERROR_MESSAGE(): int |
|
138 | + { |
|
139 | + return static::getCodeForInternalOffset(static::NO_ERROR_MESSAGE_OFFSET); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Returns API code for internal code EX_HTTP_NOT_FOUND |
|
144 | + * |
|
145 | + * @return int valid API code in current range |
|
146 | + * |
|
147 | + * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
148 | + */ |
|
149 | + public static function EX_HTTP_NOT_FOUND(): int |
|
150 | + { |
|
151 | + return static::getCodeForInternalOffset(static::EX_HTTP_NOT_FOUND_OFFSET); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Returns API code for internal code EX_HTTP_EXCEPTION |
|
156 | + * |
|
157 | + * @return int valid API code in current range |
|
158 | + * |
|
159 | + * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
160 | + */ |
|
161 | + public static function EX_HTTP_EXCEPTION(): int |
|
162 | + { |
|
163 | + return static::getCodeForInternalOffset(static::EX_HTTP_EXCEPTION_OFFSET); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Returns API code for internal code EX_UNCAUGHT_EXCEPTION |
|
168 | + * |
|
169 | + * @return int valid API code in current range |
|
170 | + * |
|
171 | + * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
172 | + */ |
|
173 | + public static function EX_UNCAUGHT_EXCEPTION(): int |
|
174 | + { |
|
175 | + return static::getCodeForInternalOffset(static::EX_UNCAUGHT_EXCEPTION_OFFSET); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Returns API code for internal code EX_AUTHENTICATION_EXCEPTION |
|
180 | + * |
|
181 | + * @return int valid API code in current range |
|
182 | + * |
|
183 | + * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
184 | + */ |
|
185 | + public static function EX_AUTHENTICATION_EXCEPTION(): int |
|
186 | + { |
|
187 | + return static::getCodeForInternalOffset(static::EX_AUTHENTICATION_EXCEPTION_OFFSET); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Returns API code for internal code EX_VALIDATION_EXCEPTION |
|
192 | + * |
|
193 | + * @return int valid API code in current range |
|
194 | + * |
|
195 | + * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
196 | + */ |
|
197 | + public static function EX_VALIDATION_EXCEPTION(): int |
|
198 | + { |
|
199 | + return static::getCodeForInternalOffset(static::EX_VALIDATION_EXCEPTION_OFFSET); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Returns API code for internal code EX_HTTP_SERVICE_UNAVAILABLE |
|
204 | + * |
|
205 | + * @return int valid API code in current range |
|
206 | + * |
|
207 | + * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
208 | + */ |
|
209 | + public static function EX_HTTP_SERVICE_UNAVAILABLE(): int |
|
210 | + { |
|
211 | + return static::getCodeForInternalOffset(static::EX_HTTP_SERVICE_UNAVAILABLE_OFFSET); |
|
212 | + } |
|
213 | 213 | |
214 | 214 | } |
@@ -27,228 +27,228 @@ |
||
27 | 27 | */ |
28 | 28 | class ExceptionHandlerHelper |
29 | 29 | { |
30 | - /** |
|
31 | - * Render an exception into valid API response. |
|
32 | - * |
|
33 | - * @param \Illuminate\Http\Request $request Request object |
|
34 | - * @param \Exception $ex Exception |
|
35 | - * |
|
36 | - * @return HttpResponse |
|
37 | - */ |
|
38 | - public static function render(/** @scrutinizer ignore-unused */ $request, Exception $ex): HttpResponse |
|
39 | - { |
|
40 | - $result = null; |
|
41 | - $cfg = static::getExceptionHandlerConfig()['map']; |
|
42 | - |
|
43 | - if ($ex instanceof HttpException) { |
|
44 | - // Check if we have any exception configuration for this particular HTTP status code. |
|
45 | - // This confing entry is guaranted to exist (at least 'default'). Enforced by tests. |
|
46 | - $http_code = $ex->getStatusCode(); |
|
47 | - $ex_cfg = $cfg[ HttpException::class ][ $http_code ] ?? null; |
|
48 | - $ex_cfg = $ex_cfg ?? $cfg[ HttpException::class ]['default']; |
|
49 | - $result = self::processException($ex, /** @scrutinizer ignore-type */ $ex_cfg, $http_code); |
|
50 | - } elseif ($ex instanceof ValidationException) { |
|
51 | - // This entry is guaranted to exist. Enforced by tests. |
|
52 | - $http_code = HttpResponse::HTTP_UNPROCESSABLE_ENTITY; |
|
53 | - $result = self::processException($ex, $cfg[ HttpException::class ][ $http_code ], $http_code); |
|
54 | - } |
|
55 | - |
|
56 | - if ($result === null) { |
|
57 | - // This entry is guaranted to exist. Enforced by tests. |
|
58 | - $result = self::processException($ex, $cfg['default'], HttpResponse::HTTP_INTERNAL_SERVER_ERROR); |
|
59 | - } |
|
60 | - |
|
61 | - return $result; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Handles given exception and produces valid HTTP response object. |
|
66 | - * |
|
67 | - * @param \Exception $ex Exception to be handled. |
|
68 | - * @param array $ex_cfg ExceptionHandler's config excerpt related to $ex exception type. |
|
69 | - * @param int $fallback_http_code HTTP code to be assigned to produced $ex related response in |
|
70 | - * case configuration array lacks own `http_code` value. |
|
71 | - * |
|
72 | - * @return \Symfony\Component\HttpFoundation\Response |
|
73 | - */ |
|
74 | - protected static function processException(\Exception $ex, array $ex_cfg, int $fallback_http_code) |
|
75 | - { |
|
76 | - $api_code = $ex_cfg['api_code']; |
|
77 | - $http_code = $ex_cfg['http_code'] ?? $fallback_http_code; |
|
78 | - $msg_key = $ex_cfg['msg_key'] ?? null; |
|
79 | - $msg_enforce = $ex_cfg['msg_enforce'] ?? false; |
|
80 | - |
|
81 | - // No message key, let's get exception message and if there's nothing useful, fallback to built-in one. |
|
82 | - $msg = $ex->getMessage(); |
|
83 | - $placeholders = [ |
|
84 | - 'api_code' => $api_code, |
|
85 | - 'message' => ($msg !== '') ? $msg : '???', |
|
86 | - ]; |
|
87 | - |
|
88 | - // shall we enforce error message? |
|
89 | - if ($msg_enforce) { |
|
90 | - // yes, please. |
|
91 | - if ($msg_key === null) { |
|
92 | - // there's no msg_key configured for this exact code, so let's obtain our default message |
|
93 | - $msg = ($msg_key === null) ? static::getErrorMessageForException($ex, $http_code, $placeholders) |
|
94 | - : Lang::get($msg_key, $placeholders); |
|
95 | - } |
|
96 | - } else { |
|
97 | - // nothing enforced, handling pipeline: ex_message -> user_defined_msg -> http_ex -> default |
|
98 | - if ($msg === '') { |
|
99 | - $msg = ($msg_key === null) ? static::getErrorMessageForException($ex, $http_code, $placeholders) |
|
100 | - : Lang::get($msg_key, $placeholders); |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - // Lets' try to build the error response with what we have now |
|
105 | - return static::error($ex, $api_code, $http_code, $msg); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Returns error message for given exception. If exception message is empty, then falls back to |
|
110 | - * `default` handler either for HttpException (if $ex is instance of it), or generic `default` |
|
111 | - * config. |
|
112 | - * |
|
113 | - * @param \Exception $ex |
|
114 | - * @param int $http_code |
|
115 | - * @param array $placeholders |
|
116 | - * |
|
117 | - * @return string |
|
118 | - */ |
|
119 | - protected static function getErrorMessageForException(\Exception $ex, int $http_code, array $placeholders): string |
|
120 | - { |
|
121 | - // exception message is uselss, lets go deeper |
|
122 | - if ($ex instanceof HttpException) { |
|
123 | - $error_message = Lang::get("response-builder::builder.http_{$http_code}", $placeholders); |
|
124 | - } else { |
|
125 | - // Still got nothing? Fall back to built-in generic message for this type of exception. |
|
126 | - $key = BaseApiCodes::getCodeMessageKey(($ex instanceof HttpException) |
|
127 | - ? /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_HTTP_EXCEPTION() |
|
128 | - : /** @scrutinizer ignore-deprecated */ BaseApiCodes::NO_ERROR_MESSAGE()); |
|
129 | - $error_message = Lang::get($key, $placeholders); |
|
130 | - } |
|
131 | - |
|
132 | - return $error_message; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Convert an authentication exception into an unauthenticated response. |
|
137 | - * |
|
138 | - * @param \Illuminate\Http\Request $request |
|
139 | - * @param \Illuminate\Auth\AuthenticationException $exception |
|
140 | - * |
|
141 | - * @return HttpResponse |
|
142 | - */ |
|
143 | - protected function unauthenticated(/** @scrutinizer ignore-unused */ $request, |
|
144 | - AuthException $exception): HttpResponse |
|
145 | - { |
|
146 | - // This entry is guaranted to exist. Enforced by tests. |
|
147 | - $http_code = HttpResponse::HTTP_UNAUTHORIZED; |
|
148 | - $cfg = static::getExceptionHandlerConfig()['map'][ HttpException::class ][ $http_code ]; |
|
149 | - |
|
150 | - return static::processException($exception, $cfg, $http_code); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Process single error and produce valid API response. |
|
155 | - * |
|
156 | - * @param Exception $ex Exception to be handled. |
|
157 | - * @param integer $api_code |
|
158 | - * @param integer $http_code |
|
159 | - * |
|
160 | - * @return HttpResponse |
|
161 | - */ |
|
162 | - protected static function error(Exception $ex, |
|
163 | - int $api_code, int $http_code = null, string $error_message): HttpResponse |
|
164 | - { |
|
165 | - $ex_http_code = ($ex instanceof HttpException) ? $ex->getStatusCode() : $ex->getCode(); |
|
166 | - $http_code = $http_code ?? $ex_http_code; |
|
167 | - |
|
168 | - // Check if we now have valid HTTP error code for this case or need to make one up. |
|
169 | - // We cannot throw any exception if codes are invalid because we are in Exception Handler already. |
|
170 | - if ($http_code < ResponseBuilder::ERROR_HTTP_CODE_MIN) { |
|
171 | - // Not a valid code, let's try to get the exception status. |
|
172 | - $http_code = $ex_http_code; |
|
173 | - } |
|
174 | - // Can it be considered a valid HTTP error code? |
|
175 | - if ($http_code < ResponseBuilder::ERROR_HTTP_CODE_MIN) { |
|
176 | - // We now handle uncaught exception, so we cannot throw another one if there's |
|
177 | - // something wrong with the configuration, so we try to recover and use built-in |
|
178 | - // codes instead. |
|
179 | - // FIXME: We should log this event as (warning or error?) |
|
180 | - $http_code = ResponseBuilder::DEFAULT_HTTP_CODE_ERROR; |
|
181 | - } |
|
182 | - |
|
183 | - // If we have trace data debugging enabled, let's gather some debug info and add to the response. |
|
184 | - $debug_data = null; |
|
185 | - if (Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_ENABLED, false)) { |
|
186 | - $debug_data = [ |
|
187 | - Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_KEY, ResponseBuilder::KEY_TRACE) => [ |
|
188 | - ResponseBuilder::KEY_CLASS => get_class($ex), |
|
189 | - ResponseBuilder::KEY_FILE => $ex->getFile(), |
|
190 | - ResponseBuilder::KEY_LINE => $ex->getLine(), |
|
191 | - ], |
|
192 | - ]; |
|
193 | - } |
|
194 | - |
|
195 | - // If this is ValidationException, add all the messages from MessageBag to the data node. |
|
196 | - $data = null; |
|
197 | - if ($ex instanceof ValidationException) { |
|
198 | - /** @var ValidationException $ex */ |
|
199 | - $data = [ResponseBuilder::KEY_MESSAGES => $ex->validator->errors()->messages()]; |
|
200 | - } |
|
201 | - |
|
202 | - return ResponseBuilder::asError($api_code) |
|
203 | - ->withMessage($error_message) |
|
204 | - ->withHttpCode($http_code) |
|
205 | - ->withData($data) |
|
206 | - ->withDebugData($debug_data) |
|
207 | - ->build(); |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Returns default (built-in) exception handler config array. |
|
212 | - * |
|
213 | - * @return array |
|
214 | - */ |
|
215 | - protected static function getExceptionHandlerDefaultConfig(): array |
|
216 | - { |
|
217 | - return [ |
|
218 | - 'map' => [ |
|
219 | - HttpException::class => [ |
|
220 | - // used by unauthenticated() to obtain api and http code for the exception |
|
221 | - HttpResponse::HTTP_UNAUTHORIZED => [ |
|
222 | - 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_AUTHENTICATION_EXCEPTION(), |
|
223 | - ], |
|
224 | - // Required by ValidationException handler |
|
225 | - HttpResponse::HTTP_UNPROCESSABLE_ENTITY => [ |
|
226 | - 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_VALIDATION_EXCEPTION(), |
|
227 | - ], |
|
228 | - // default handler is mandatory. `default` entry MUST have both `api_code` and `http_code` set. |
|
229 | - 'default' => [ |
|
230 | - 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_HTTP_EXCEPTION(), |
|
231 | - 'http_code' => HttpResponse::HTTP_BAD_REQUEST, |
|
232 | - ], |
|
233 | - ], |
|
234 | - // default handler is mandatory. `default` entry MUST have both `api_code` and `http_code` set. |
|
235 | - 'default' => [ |
|
236 | - 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_UNCAUGHT_EXCEPTION(), |
|
237 | - 'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR, |
|
238 | - ], |
|
239 | - ], |
|
240 | - ]; |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Returns ExceptionHandlerHelper configration array with user configuration merged into built-in defaults. |
|
245 | - * |
|
246 | - * @return array |
|
247 | - */ |
|
248 | - protected static function getExceptionHandlerConfig(): array |
|
249 | - { |
|
250 | - return Util::mergeConfig(static::getExceptionHandlerDefaultConfig(), |
|
251 | - \Config::get(ResponseBuilder::CONF_KEY_EXCEPTION_HANDLER, [])); |
|
252 | - } |
|
30 | + /** |
|
31 | + * Render an exception into valid API response. |
|
32 | + * |
|
33 | + * @param \Illuminate\Http\Request $request Request object |
|
34 | + * @param \Exception $ex Exception |
|
35 | + * |
|
36 | + * @return HttpResponse |
|
37 | + */ |
|
38 | + public static function render(/** @scrutinizer ignore-unused */ $request, Exception $ex): HttpResponse |
|
39 | + { |
|
40 | + $result = null; |
|
41 | + $cfg = static::getExceptionHandlerConfig()['map']; |
|
42 | + |
|
43 | + if ($ex instanceof HttpException) { |
|
44 | + // Check if we have any exception configuration for this particular HTTP status code. |
|
45 | + // This confing entry is guaranted to exist (at least 'default'). Enforced by tests. |
|
46 | + $http_code = $ex->getStatusCode(); |
|
47 | + $ex_cfg = $cfg[ HttpException::class ][ $http_code ] ?? null; |
|
48 | + $ex_cfg = $ex_cfg ?? $cfg[ HttpException::class ]['default']; |
|
49 | + $result = self::processException($ex, /** @scrutinizer ignore-type */ $ex_cfg, $http_code); |
|
50 | + } elseif ($ex instanceof ValidationException) { |
|
51 | + // This entry is guaranted to exist. Enforced by tests. |
|
52 | + $http_code = HttpResponse::HTTP_UNPROCESSABLE_ENTITY; |
|
53 | + $result = self::processException($ex, $cfg[ HttpException::class ][ $http_code ], $http_code); |
|
54 | + } |
|
55 | + |
|
56 | + if ($result === null) { |
|
57 | + // This entry is guaranted to exist. Enforced by tests. |
|
58 | + $result = self::processException($ex, $cfg['default'], HttpResponse::HTTP_INTERNAL_SERVER_ERROR); |
|
59 | + } |
|
60 | + |
|
61 | + return $result; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Handles given exception and produces valid HTTP response object. |
|
66 | + * |
|
67 | + * @param \Exception $ex Exception to be handled. |
|
68 | + * @param array $ex_cfg ExceptionHandler's config excerpt related to $ex exception type. |
|
69 | + * @param int $fallback_http_code HTTP code to be assigned to produced $ex related response in |
|
70 | + * case configuration array lacks own `http_code` value. |
|
71 | + * |
|
72 | + * @return \Symfony\Component\HttpFoundation\Response |
|
73 | + */ |
|
74 | + protected static function processException(\Exception $ex, array $ex_cfg, int $fallback_http_code) |
|
75 | + { |
|
76 | + $api_code = $ex_cfg['api_code']; |
|
77 | + $http_code = $ex_cfg['http_code'] ?? $fallback_http_code; |
|
78 | + $msg_key = $ex_cfg['msg_key'] ?? null; |
|
79 | + $msg_enforce = $ex_cfg['msg_enforce'] ?? false; |
|
80 | + |
|
81 | + // No message key, let's get exception message and if there's nothing useful, fallback to built-in one. |
|
82 | + $msg = $ex->getMessage(); |
|
83 | + $placeholders = [ |
|
84 | + 'api_code' => $api_code, |
|
85 | + 'message' => ($msg !== '') ? $msg : '???', |
|
86 | + ]; |
|
87 | + |
|
88 | + // shall we enforce error message? |
|
89 | + if ($msg_enforce) { |
|
90 | + // yes, please. |
|
91 | + if ($msg_key === null) { |
|
92 | + // there's no msg_key configured for this exact code, so let's obtain our default message |
|
93 | + $msg = ($msg_key === null) ? static::getErrorMessageForException($ex, $http_code, $placeholders) |
|
94 | + : Lang::get($msg_key, $placeholders); |
|
95 | + } |
|
96 | + } else { |
|
97 | + // nothing enforced, handling pipeline: ex_message -> user_defined_msg -> http_ex -> default |
|
98 | + if ($msg === '') { |
|
99 | + $msg = ($msg_key === null) ? static::getErrorMessageForException($ex, $http_code, $placeholders) |
|
100 | + : Lang::get($msg_key, $placeholders); |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + // Lets' try to build the error response with what we have now |
|
105 | + return static::error($ex, $api_code, $http_code, $msg); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Returns error message for given exception. If exception message is empty, then falls back to |
|
110 | + * `default` handler either for HttpException (if $ex is instance of it), or generic `default` |
|
111 | + * config. |
|
112 | + * |
|
113 | + * @param \Exception $ex |
|
114 | + * @param int $http_code |
|
115 | + * @param array $placeholders |
|
116 | + * |
|
117 | + * @return string |
|
118 | + */ |
|
119 | + protected static function getErrorMessageForException(\Exception $ex, int $http_code, array $placeholders): string |
|
120 | + { |
|
121 | + // exception message is uselss, lets go deeper |
|
122 | + if ($ex instanceof HttpException) { |
|
123 | + $error_message = Lang::get("response-builder::builder.http_{$http_code}", $placeholders); |
|
124 | + } else { |
|
125 | + // Still got nothing? Fall back to built-in generic message for this type of exception. |
|
126 | + $key = BaseApiCodes::getCodeMessageKey(($ex instanceof HttpException) |
|
127 | + ? /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_HTTP_EXCEPTION() |
|
128 | + : /** @scrutinizer ignore-deprecated */ BaseApiCodes::NO_ERROR_MESSAGE()); |
|
129 | + $error_message = Lang::get($key, $placeholders); |
|
130 | + } |
|
131 | + |
|
132 | + return $error_message; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Convert an authentication exception into an unauthenticated response. |
|
137 | + * |
|
138 | + * @param \Illuminate\Http\Request $request |
|
139 | + * @param \Illuminate\Auth\AuthenticationException $exception |
|
140 | + * |
|
141 | + * @return HttpResponse |
|
142 | + */ |
|
143 | + protected function unauthenticated(/** @scrutinizer ignore-unused */ $request, |
|
144 | + AuthException $exception): HttpResponse |
|
145 | + { |
|
146 | + // This entry is guaranted to exist. Enforced by tests. |
|
147 | + $http_code = HttpResponse::HTTP_UNAUTHORIZED; |
|
148 | + $cfg = static::getExceptionHandlerConfig()['map'][ HttpException::class ][ $http_code ]; |
|
149 | + |
|
150 | + return static::processException($exception, $cfg, $http_code); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Process single error and produce valid API response. |
|
155 | + * |
|
156 | + * @param Exception $ex Exception to be handled. |
|
157 | + * @param integer $api_code |
|
158 | + * @param integer $http_code |
|
159 | + * |
|
160 | + * @return HttpResponse |
|
161 | + */ |
|
162 | + protected static function error(Exception $ex, |
|
163 | + int $api_code, int $http_code = null, string $error_message): HttpResponse |
|
164 | + { |
|
165 | + $ex_http_code = ($ex instanceof HttpException) ? $ex->getStatusCode() : $ex->getCode(); |
|
166 | + $http_code = $http_code ?? $ex_http_code; |
|
167 | + |
|
168 | + // Check if we now have valid HTTP error code for this case or need to make one up. |
|
169 | + // We cannot throw any exception if codes are invalid because we are in Exception Handler already. |
|
170 | + if ($http_code < ResponseBuilder::ERROR_HTTP_CODE_MIN) { |
|
171 | + // Not a valid code, let's try to get the exception status. |
|
172 | + $http_code = $ex_http_code; |
|
173 | + } |
|
174 | + // Can it be considered a valid HTTP error code? |
|
175 | + if ($http_code < ResponseBuilder::ERROR_HTTP_CODE_MIN) { |
|
176 | + // We now handle uncaught exception, so we cannot throw another one if there's |
|
177 | + // something wrong with the configuration, so we try to recover and use built-in |
|
178 | + // codes instead. |
|
179 | + // FIXME: We should log this event as (warning or error?) |
|
180 | + $http_code = ResponseBuilder::DEFAULT_HTTP_CODE_ERROR; |
|
181 | + } |
|
182 | + |
|
183 | + // If we have trace data debugging enabled, let's gather some debug info and add to the response. |
|
184 | + $debug_data = null; |
|
185 | + if (Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_ENABLED, false)) { |
|
186 | + $debug_data = [ |
|
187 | + Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_KEY, ResponseBuilder::KEY_TRACE) => [ |
|
188 | + ResponseBuilder::KEY_CLASS => get_class($ex), |
|
189 | + ResponseBuilder::KEY_FILE => $ex->getFile(), |
|
190 | + ResponseBuilder::KEY_LINE => $ex->getLine(), |
|
191 | + ], |
|
192 | + ]; |
|
193 | + } |
|
194 | + |
|
195 | + // If this is ValidationException, add all the messages from MessageBag to the data node. |
|
196 | + $data = null; |
|
197 | + if ($ex instanceof ValidationException) { |
|
198 | + /** @var ValidationException $ex */ |
|
199 | + $data = [ResponseBuilder::KEY_MESSAGES => $ex->validator->errors()->messages()]; |
|
200 | + } |
|
201 | + |
|
202 | + return ResponseBuilder::asError($api_code) |
|
203 | + ->withMessage($error_message) |
|
204 | + ->withHttpCode($http_code) |
|
205 | + ->withData($data) |
|
206 | + ->withDebugData($debug_data) |
|
207 | + ->build(); |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Returns default (built-in) exception handler config array. |
|
212 | + * |
|
213 | + * @return array |
|
214 | + */ |
|
215 | + protected static function getExceptionHandlerDefaultConfig(): array |
|
216 | + { |
|
217 | + return [ |
|
218 | + 'map' => [ |
|
219 | + HttpException::class => [ |
|
220 | + // used by unauthenticated() to obtain api and http code for the exception |
|
221 | + HttpResponse::HTTP_UNAUTHORIZED => [ |
|
222 | + 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_AUTHENTICATION_EXCEPTION(), |
|
223 | + ], |
|
224 | + // Required by ValidationException handler |
|
225 | + HttpResponse::HTTP_UNPROCESSABLE_ENTITY => [ |
|
226 | + 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_VALIDATION_EXCEPTION(), |
|
227 | + ], |
|
228 | + // default handler is mandatory. `default` entry MUST have both `api_code` and `http_code` set. |
|
229 | + 'default' => [ |
|
230 | + 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_HTTP_EXCEPTION(), |
|
231 | + 'http_code' => HttpResponse::HTTP_BAD_REQUEST, |
|
232 | + ], |
|
233 | + ], |
|
234 | + // default handler is mandatory. `default` entry MUST have both `api_code` and `http_code` set. |
|
235 | + 'default' => [ |
|
236 | + 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_UNCAUGHT_EXCEPTION(), |
|
237 | + 'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR, |
|
238 | + ], |
|
239 | + ], |
|
240 | + ]; |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Returns ExceptionHandlerHelper configration array with user configuration merged into built-in defaults. |
|
245 | + * |
|
246 | + * @return array |
|
247 | + */ |
|
248 | + protected static function getExceptionHandlerConfig(): array |
|
249 | + { |
|
250 | + return Util::mergeConfig(static::getExceptionHandlerDefaultConfig(), |
|
251 | + \Config::get(ResponseBuilder::CONF_KEY_EXCEPTION_HANDLER, [])); |
|
252 | + } |
|
253 | 253 | |
254 | 254 | } |
@@ -44,13 +44,13 @@ discard block |
||
44 | 44 | // Check if we have any exception configuration for this particular HTTP status code. |
45 | 45 | // This confing entry is guaranted to exist (at least 'default'). Enforced by tests. |
46 | 46 | $http_code = $ex->getStatusCode(); |
47 | - $ex_cfg = $cfg[ HttpException::class ][ $http_code ] ?? null; |
|
48 | - $ex_cfg = $ex_cfg ?? $cfg[ HttpException::class ]['default']; |
|
47 | + $ex_cfg = $cfg[HttpException::class][$http_code] ?? null; |
|
48 | + $ex_cfg = $ex_cfg ?? $cfg[HttpException::class]['default']; |
|
49 | 49 | $result = self::processException($ex, /** @scrutinizer ignore-type */ $ex_cfg, $http_code); |
50 | 50 | } elseif ($ex instanceof ValidationException) { |
51 | 51 | // This entry is guaranted to exist. Enforced by tests. |
52 | 52 | $http_code = HttpResponse::HTTP_UNPROCESSABLE_ENTITY; |
53 | - $result = self::processException($ex, $cfg[ HttpException::class ][ $http_code ], $http_code); |
|
53 | + $result = self::processException($ex, $cfg[HttpException::class][$http_code], $http_code); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | if ($result === null) { |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | { |
146 | 146 | // This entry is guaranted to exist. Enforced by tests. |
147 | 147 | $http_code = HttpResponse::HTTP_UNAUTHORIZED; |
148 | - $cfg = static::getExceptionHandlerConfig()['map'][ HttpException::class ][ $http_code ]; |
|
148 | + $cfg = static::getExceptionHandlerConfig()['map'][HttpException::class][$http_code]; |
|
149 | 149 | |
150 | 150 | return static::processException($exception, $cfg, $http_code); |
151 | 151 | } |
@@ -24,274 +24,274 @@ |
||
24 | 24 | */ |
25 | 25 | class ResponseBuilderLegacy extends ResponseBuilderBase |
26 | 26 | { |
27 | - /** |
|
28 | - * Returns success |
|
29 | - * |
|
30 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
31 | - * of the JSON response, single supported object or @null if there's no |
|
32 | - * to be returned. |
|
33 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
34 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
35 | - * substitution or @null if none. |
|
36 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
37 | - * for default DEFAULT_HTTP_CODE_OK. |
|
38 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
39 | - * options or pass @null to use value from your config (or defaults). |
|
40 | - * |
|
41 | - * @return HttpResponse |
|
42 | - */ |
|
43 | - public static function success($data = null, $api_code = null, array $placeholders = null, |
|
44 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
45 | - { |
|
46 | - return ResponseBuilder::asSuccess($api_code) |
|
47 | - ->withData($data) |
|
48 | - ->withPlaceholders($placeholders) |
|
49 | - ->withHttpCode($http_code) |
|
50 | - ->withJsonOptions($json_opts) |
|
51 | - ->build(); |
|
52 | - } |
|
27 | + /** |
|
28 | + * Returns success |
|
29 | + * |
|
30 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
31 | + * of the JSON response, single supported object or @null if there's no |
|
32 | + * to be returned. |
|
33 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
34 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
35 | + * substitution or @null if none. |
|
36 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
37 | + * for default DEFAULT_HTTP_CODE_OK. |
|
38 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
39 | + * options or pass @null to use value from your config (or defaults). |
|
40 | + * |
|
41 | + * @return HttpResponse |
|
42 | + */ |
|
43 | + public static function success($data = null, $api_code = null, array $placeholders = null, |
|
44 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
45 | + { |
|
46 | + return ResponseBuilder::asSuccess($api_code) |
|
47 | + ->withData($data) |
|
48 | + ->withPlaceholders($placeholders) |
|
49 | + ->withHttpCode($http_code) |
|
50 | + ->withJsonOptions($json_opts) |
|
51 | + ->build(); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Returns success |
|
56 | - * |
|
57 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
58 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
59 | - * substitution or @null if none. |
|
60 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
61 | - * for default DEFAULT_HTTP_CODE_OK. |
|
62 | - * |
|
63 | - * @return HttpResponse |
|
64 | - * |
|
65 | - * @deprecated Please use Builder class. |
|
66 | - */ |
|
67 | - public static function successWithCode(int $api_code = null, array $placeholders = null, |
|
68 | - int $http_code = null): HttpResponse |
|
69 | - { |
|
70 | - return ResponseBuilder::asSuccess($api_code) |
|
71 | - ->withPlaceholders($placeholders) |
|
72 | - ->withHttpCode($http_code) |
|
73 | - ->build(); |
|
74 | - } |
|
54 | + /** |
|
55 | + * Returns success |
|
56 | + * |
|
57 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
58 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
59 | + * substitution or @null if none. |
|
60 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
61 | + * for default DEFAULT_HTTP_CODE_OK. |
|
62 | + * |
|
63 | + * @return HttpResponse |
|
64 | + * |
|
65 | + * @deprecated Please use Builder class. |
|
66 | + */ |
|
67 | + public static function successWithCode(int $api_code = null, array $placeholders = null, |
|
68 | + int $http_code = null): HttpResponse |
|
69 | + { |
|
70 | + return ResponseBuilder::asSuccess($api_code) |
|
71 | + ->withPlaceholders($placeholders) |
|
72 | + ->withHttpCode($http_code) |
|
73 | + ->build(); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * @param string $message Custom message to be returned as part of the response. |
|
78 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
79 | - * of the JSON response, single supported object or @null if there's no |
|
80 | - * to be returned. |
|
81 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
82 | - * for default DEFAULT_HTTP_CODE_OK. |
|
83 | - * |
|
84 | - * @return HttpResponse |
|
85 | - * |
|
86 | - * @deprecated Please use Builder class. |
|
87 | - */ |
|
88 | - public static function successWithMessage(string $message, $data = null, int $api_code = null, |
|
89 | - int $http_code = null): HttpResponse |
|
90 | - { |
|
91 | - return ResponseBuilder::asSuccess($api_code) |
|
92 | - ->withMessage($message) |
|
93 | - ->withData($data) |
|
94 | - ->withHttpCode($http_code) |
|
95 | - ->build(); |
|
96 | - } |
|
76 | + /** |
|
77 | + * @param string $message Custom message to be returned as part of the response. |
|
78 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
79 | + * of the JSON response, single supported object or @null if there's no |
|
80 | + * to be returned. |
|
81 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
82 | + * for default DEFAULT_HTTP_CODE_OK. |
|
83 | + * |
|
84 | + * @return HttpResponse |
|
85 | + * |
|
86 | + * @deprecated Please use Builder class. |
|
87 | + */ |
|
88 | + public static function successWithMessage(string $message, $data = null, int $api_code = null, |
|
89 | + int $http_code = null): HttpResponse |
|
90 | + { |
|
91 | + return ResponseBuilder::asSuccess($api_code) |
|
92 | + ->withMessage($message) |
|
93 | + ->withData($data) |
|
94 | + ->withHttpCode($http_code) |
|
95 | + ->build(); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node. |
|
100 | - * of the JSON response, single supported object or @null if there's no |
|
101 | - * to be returned. |
|
102 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
103 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
104 | - * substitution or @null if none. |
|
105 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
106 | - * for default DEFAULT_HTTP_CODE_OK. |
|
107 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
108 | - * options or pass @null to use value from your config (or defaults). |
|
109 | - * |
|
110 | - * @return HttpResponse |
|
111 | - * |
|
112 | - * @deprecated Please use Builder class. |
|
113 | - */ |
|
114 | - public static function successWithHttpCode(int $http_code = null): HttpResponse |
|
115 | - { |
|
116 | - return ResponseBuilder::asSuccess() |
|
117 | - ->withHttpCode($http_code) |
|
118 | - ->build(); |
|
119 | - } |
|
98 | + /** |
|
99 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node. |
|
100 | + * of the JSON response, single supported object or @null if there's no |
|
101 | + * to be returned. |
|
102 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
103 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
104 | + * substitution or @null if none. |
|
105 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
106 | + * for default DEFAULT_HTTP_CODE_OK. |
|
107 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
108 | + * options or pass @null to use value from your config (or defaults). |
|
109 | + * |
|
110 | + * @return HttpResponse |
|
111 | + * |
|
112 | + * @deprecated Please use Builder class. |
|
113 | + */ |
|
114 | + public static function successWithHttpCode(int $http_code = null): HttpResponse |
|
115 | + { |
|
116 | + return ResponseBuilder::asSuccess() |
|
117 | + ->withHttpCode($http_code) |
|
118 | + ->build(); |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error |
|
123 | - * message uses placeholders as well as return data payload |
|
124 | - * |
|
125 | - * @param integer $api_code Your API code to be returned with the response object. |
|
126 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
127 | - * substitution or @null if none. |
|
128 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
129 | - * of the JSON response, single supported object or @null if there's no |
|
130 | - * to be returned. |
|
131 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
132 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
133 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
134 | - * options or pass @null to use value from your config (or defaults). |
|
135 | - * |
|
136 | - * @return HttpResponse |
|
137 | - */ |
|
138 | - public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
|
139 | - int $json_opts = null): HttpResponse |
|
140 | - { |
|
141 | - return ResponseBuilder::asError($api_code) |
|
142 | - ->withPlaceholders($placeholders) |
|
143 | - ->withData($data) |
|
144 | - ->withHttpCode($http_code) |
|
145 | - ->withJsonOptions($json_opts) |
|
146 | - ->build(); |
|
147 | - } |
|
121 | + /** |
|
122 | + * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error |
|
123 | + * message uses placeholders as well as return data payload |
|
124 | + * |
|
125 | + * @param integer $api_code Your API code to be returned with the response object. |
|
126 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
127 | + * substitution or @null if none. |
|
128 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
129 | + * of the JSON response, single supported object or @null if there's no |
|
130 | + * to be returned. |
|
131 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
132 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
133 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
134 | + * options or pass @null to use value from your config (or defaults). |
|
135 | + * |
|
136 | + * @return HttpResponse |
|
137 | + */ |
|
138 | + public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
|
139 | + int $json_opts = null): HttpResponse |
|
140 | + { |
|
141 | + return ResponseBuilder::asError($api_code) |
|
142 | + ->withPlaceholders($placeholders) |
|
143 | + ->withData($data) |
|
144 | + ->withHttpCode($http_code) |
|
145 | + ->withJsonOptions($json_opts) |
|
146 | + ->build(); |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * @param integer $api_code Your API code to be returned with the response object. |
|
151 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
152 | - * of the JSON response, single supported object or @null if there's no |
|
153 | - * to be returned. |
|
154 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
155 | - * substitution or @null if none. |
|
156 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
157 | - * options or pass @null to use value from your config (or defaults). |
|
158 | - * |
|
159 | - * @return HttpResponse |
|
160 | - * |
|
161 | - * @deprecated Please use Builder class. |
|
162 | - */ |
|
163 | - public static function errorWithData(int $api_code, $data, array $placeholders = null, |
|
164 | - int $json_opts = null): HttpResponse |
|
165 | - { |
|
166 | - return ResponseBuilder::asError($api_code) |
|
167 | - ->withData($data) |
|
168 | - ->withPlaceholders($placeholders) |
|
169 | - ->withJsonOptions($json_opts) |
|
170 | - ->build(); |
|
171 | - } |
|
149 | + /** |
|
150 | + * @param integer $api_code Your API code to be returned with the response object. |
|
151 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
152 | + * of the JSON response, single supported object or @null if there's no |
|
153 | + * to be returned. |
|
154 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
155 | + * substitution or @null if none. |
|
156 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
157 | + * options or pass @null to use value from your config (or defaults). |
|
158 | + * |
|
159 | + * @return HttpResponse |
|
160 | + * |
|
161 | + * @deprecated Please use Builder class. |
|
162 | + */ |
|
163 | + public static function errorWithData(int $api_code, $data, array $placeholders = null, |
|
164 | + int $json_opts = null): HttpResponse |
|
165 | + { |
|
166 | + return ResponseBuilder::asError($api_code) |
|
167 | + ->withData($data) |
|
168 | + ->withPlaceholders($placeholders) |
|
169 | + ->withJsonOptions($json_opts) |
|
170 | + ->build(); |
|
171 | + } |
|
172 | 172 | |
173 | - /** |
|
174 | - * @param integer $api_code Your API code to be returned with the response object. |
|
175 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
176 | - * of the JSON response, single supported object or @null if there's no |
|
177 | - * to be returned. |
|
178 | - * @param integer $http_code HTTP code to be used for HttpResponse sent. |
|
179 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
180 | - * substitution or @null if none. |
|
181 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
182 | - * options or pass @null to use value from your config (or defaults). |
|
183 | - * |
|
184 | - * @return HttpResponse |
|
185 | - * |
|
186 | - * @throws \InvalidArgumentException if http_code is @null |
|
187 | - * |
|
188 | - * @deprecated Please use Builder class. |
|
189 | - */ |
|
190 | - public static function errorWithDataAndHttpCode(int $api_code, $data, int $http_code, array $placeholders = null, |
|
191 | - int $json_opts = null): HttpResponse |
|
192 | - { |
|
193 | - return ResponseBuilder::asError($api_code) |
|
194 | - ->withData($data) |
|
195 | - ->withHttpCode($http_code) |
|
196 | - ->withPlaceholders($placeholders) |
|
197 | - ->withJsonOptions($json_opts) |
|
198 | - ->build(); |
|
199 | - } |
|
173 | + /** |
|
174 | + * @param integer $api_code Your API code to be returned with the response object. |
|
175 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
176 | + * of the JSON response, single supported object or @null if there's no |
|
177 | + * to be returned. |
|
178 | + * @param integer $http_code HTTP code to be used for HttpResponse sent. |
|
179 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
180 | + * substitution or @null if none. |
|
181 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
182 | + * options or pass @null to use value from your config (or defaults). |
|
183 | + * |
|
184 | + * @return HttpResponse |
|
185 | + * |
|
186 | + * @throws \InvalidArgumentException if http_code is @null |
|
187 | + * |
|
188 | + * @deprecated Please use Builder class. |
|
189 | + */ |
|
190 | + public static function errorWithDataAndHttpCode(int $api_code, $data, int $http_code, array $placeholders = null, |
|
191 | + int $json_opts = null): HttpResponse |
|
192 | + { |
|
193 | + return ResponseBuilder::asError($api_code) |
|
194 | + ->withData($data) |
|
195 | + ->withHttpCode($http_code) |
|
196 | + ->withPlaceholders($placeholders) |
|
197 | + ->withJsonOptions($json_opts) |
|
198 | + ->build(); |
|
199 | + } |
|
200 | 200 | |
201 | - /** |
|
202 | - * @param integer $api_code Your API code to be returned with the response object. |
|
203 | - * @param integer $http_code HTTP code to be used for HttpResponse sent or @null |
|
204 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
205 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
206 | - * substitution or @null if none. |
|
207 | - * |
|
208 | - * @return HttpResponse |
|
209 | - * |
|
210 | - * @throws \InvalidArgumentException if http_code is @null |
|
211 | - * |
|
212 | - * @deprecated Please use Builder class. |
|
213 | - */ |
|
214 | - public static function errorWithHttpCode(int $api_code, int $http_code, array $placeholders = null): HttpResponse |
|
215 | - { |
|
216 | - return ResponseBuilder::asError($api_code) |
|
217 | - ->withHttpCode($http_code) |
|
218 | - ->withPlaceholders($placeholders) |
|
219 | - ->build(); |
|
220 | - } |
|
201 | + /** |
|
202 | + * @param integer $api_code Your API code to be returned with the response object. |
|
203 | + * @param integer $http_code HTTP code to be used for HttpResponse sent or @null |
|
204 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
205 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
206 | + * substitution or @null if none. |
|
207 | + * |
|
208 | + * @return HttpResponse |
|
209 | + * |
|
210 | + * @throws \InvalidArgumentException if http_code is @null |
|
211 | + * |
|
212 | + * @deprecated Please use Builder class. |
|
213 | + */ |
|
214 | + public static function errorWithHttpCode(int $api_code, int $http_code, array $placeholders = null): HttpResponse |
|
215 | + { |
|
216 | + return ResponseBuilder::asError($api_code) |
|
217 | + ->withHttpCode($http_code) |
|
218 | + ->withPlaceholders($placeholders) |
|
219 | + ->build(); |
|
220 | + } |
|
221 | 221 | |
222 | - /** |
|
223 | - * @param integer $api_code Your API code to be returned with the response object. |
|
224 | - * @param string $message Custom message to be returned as part of error response |
|
225 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
226 | - * of the JSON response, single supported object or @null if there's no |
|
227 | - * to be returned. |
|
228 | - * @param integer|null $http_code Optional HTTP status code to be used for HttpResponse sent |
|
229 | - * or @null for DEFAULT_HTTP_CODE_ERROR |
|
230 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
231 | - * options or pass @null to use value from your config (or defaults). |
|
232 | - * |
|
233 | - * @return HttpResponse |
|
234 | - * |
|
235 | - * @deprecated Please use Builder class. |
|
236 | - */ |
|
237 | - public static function errorWithMessageAndData(int $api_code, string $message, $data, |
|
238 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
239 | - { |
|
240 | - return ResponseBuilder::asError($api_code) |
|
241 | - ->withMessage($message) |
|
242 | - ->withData($data) |
|
243 | - ->withHttpCode($http_code) |
|
244 | - ->withJsonOptions($json_opts) |
|
245 | - ->build(); |
|
246 | - } |
|
222 | + /** |
|
223 | + * @param integer $api_code Your API code to be returned with the response object. |
|
224 | + * @param string $message Custom message to be returned as part of error response |
|
225 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
226 | + * of the JSON response, single supported object or @null if there's no |
|
227 | + * to be returned. |
|
228 | + * @param integer|null $http_code Optional HTTP status code to be used for HttpResponse sent |
|
229 | + * or @null for DEFAULT_HTTP_CODE_ERROR |
|
230 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
231 | + * options or pass @null to use value from your config (or defaults). |
|
232 | + * |
|
233 | + * @return HttpResponse |
|
234 | + * |
|
235 | + * @deprecated Please use Builder class. |
|
236 | + */ |
|
237 | + public static function errorWithMessageAndData(int $api_code, string $message, $data, |
|
238 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
239 | + { |
|
240 | + return ResponseBuilder::asError($api_code) |
|
241 | + ->withMessage($message) |
|
242 | + ->withData($data) |
|
243 | + ->withHttpCode($http_code) |
|
244 | + ->withJsonOptions($json_opts) |
|
245 | + ->build(); |
|
246 | + } |
|
247 | 247 | |
248 | - /** |
|
249 | - * @param integer $api_code Your API code to be returned with the response object. |
|
250 | - * @param string $message custom message to be returned as part of error response |
|
251 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
252 | - * of the JSON response, single supported object or @null if there's no |
|
253 | - * to be returned. |
|
254 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
255 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
256 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
257 | - * options or pass @null to use value from your config (or defaults). |
|
258 | - * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
259 | - * |
|
260 | - * @return HttpResponse |
|
261 | - * |
|
262 | - * @deprecated Please use Builder class. |
|
263 | - * |
|
264 | - * @noinspection PhpTooManyParametersInspection |
|
265 | - */ |
|
266 | - public static function errorWithMessageAndDataAndDebug(int $api_code, string $message, $data, |
|
267 | - int $http_code = null, int $json_opts = null, |
|
268 | - array $debug_data = null): HttpResponse |
|
269 | - { |
|
270 | - return ResponseBuilder::asError($api_code) |
|
271 | - ->withMessage($message) |
|
272 | - ->withData($data) |
|
273 | - ->withHttpCode($http_code) |
|
274 | - ->withJsonOptions($json_opts) |
|
275 | - ->withDebugData($debug_data) |
|
276 | - ->build(); |
|
277 | - } |
|
248 | + /** |
|
249 | + * @param integer $api_code Your API code to be returned with the response object. |
|
250 | + * @param string $message custom message to be returned as part of error response |
|
251 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
252 | + * of the JSON response, single supported object or @null if there's no |
|
253 | + * to be returned. |
|
254 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
255 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
256 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
257 | + * options or pass @null to use value from your config (or defaults). |
|
258 | + * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
259 | + * |
|
260 | + * @return HttpResponse |
|
261 | + * |
|
262 | + * @deprecated Please use Builder class. |
|
263 | + * |
|
264 | + * @noinspection PhpTooManyParametersInspection |
|
265 | + */ |
|
266 | + public static function errorWithMessageAndDataAndDebug(int $api_code, string $message, $data, |
|
267 | + int $http_code = null, int $json_opts = null, |
|
268 | + array $debug_data = null): HttpResponse |
|
269 | + { |
|
270 | + return ResponseBuilder::asError($api_code) |
|
271 | + ->withMessage($message) |
|
272 | + ->withData($data) |
|
273 | + ->withHttpCode($http_code) |
|
274 | + ->withJsonOptions($json_opts) |
|
275 | + ->withDebugData($debug_data) |
|
276 | + ->build(); |
|
277 | + } |
|
278 | 278 | |
279 | - /** |
|
280 | - * @param integer $api_code Your API code to be returned with the response object. |
|
281 | - * @param string $message Custom message to be returned as part of error response |
|
282 | - * @param integer|null $http_code HTTP code to be used with final response sent or @null |
|
283 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
284 | - * |
|
285 | - * @return HttpResponse |
|
286 | - * |
|
287 | - * @deprecated Please use Builder class. |
|
288 | - */ |
|
289 | - public static function errorWithMessage(int $api_code, string $message, int $http_code = null): HttpResponse |
|
290 | - { |
|
291 | - return ResponseBuilder::asError($api_code) |
|
292 | - ->withMessage($message) |
|
293 | - ->withHttpCode($http_code) |
|
294 | - ->build(); |
|
295 | - } |
|
279 | + /** |
|
280 | + * @param integer $api_code Your API code to be returned with the response object. |
|
281 | + * @param string $message Custom message to be returned as part of error response |
|
282 | + * @param integer|null $http_code HTTP code to be used with final response sent or @null |
|
283 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
284 | + * |
|
285 | + * @return HttpResponse |
|
286 | + * |
|
287 | + * @deprecated Please use Builder class. |
|
288 | + */ |
|
289 | + public static function errorWithMessage(int $api_code, string $message, int $http_code = null): HttpResponse |
|
290 | + { |
|
291 | + return ResponseBuilder::asError($api_code) |
|
292 | + ->withMessage($message) |
|
293 | + ->withHttpCode($http_code) |
|
294 | + ->build(); |
|
295 | + } |
|
296 | 296 | |
297 | 297 | } |