@@ -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,124 +21,124 @@ |
||
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 | - */ |
|
67 | - public static function getApiCodeConstants(): array |
|
68 | - { |
|
69 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
70 | - return (new \ReflectionClass(static::class))->getConstants(); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Returns complete error code to locale string mapping array |
|
75 | - * |
|
76 | - * @return array |
|
77 | - * |
|
78 | - * @throws \RuntimeException Thrown when builder map is not configured. |
|
79 | - */ |
|
80 | - public static function getMap(): array |
|
81 | - { |
|
82 | - $user_map = Config::get(ResponseBuilder::CONF_KEY_MAP, null); |
|
83 | - if ($user_map === null) { |
|
84 | - throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', ResponseBuilder::CONF_KEY_MAP)); |
|
85 | - } |
|
86 | - if (!\is_array($user_map)) { |
|
87 | - throw new \RuntimeException(sprintf('CONFIG: "%s" must be an array', ResponseBuilder::CONF_KEY_MAP)); |
|
88 | - } |
|
89 | - return Util::mergeConfig(BaseApiCodes::getBaseMap(), $user_map); |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Returns locale mappings key for given api code or @null if there's no mapping |
|
94 | - * |
|
95 | - * @param integer $api_code Api code to look for mapped message for. |
|
96 | - * |
|
97 | - * @return string|null |
|
98 | - * |
|
99 | - * @throws \InvalidArgumentException If $code is not in allowed range. |
|
100 | - */ |
|
101 | - public static function getCodeMessageKey(int $api_code): ?string |
|
102 | - { |
|
103 | - if (!static::isCodeValid($api_code)) { |
|
104 | - $min = static::getMinCode(); |
|
105 | - $max = static::getMaxCode(); |
|
106 | - throw new \InvalidArgumentException("API code value ({$api_code}) is out of allowed range {$min}-{$max}"); |
|
107 | - } |
|
108 | - |
|
109 | - $map = static::getMap(); |
|
110 | - |
|
111 | - return $map[ $api_code ] ?? null; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Checks if given API $code can be used in current configuration. |
|
116 | - * |
|
117 | - * @param int $code API code to validate |
|
118 | - * |
|
119 | - * @return bool |
|
120 | - */ |
|
121 | - public static function isCodeValid(int $code): bool |
|
122 | - { |
|
123 | - return ($code === 0) || (($code >= static::getMinCode()) && ($code <= static::getMaxCode())); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Returns final API code for internal code, remapped to configured code range |
|
128 | - * |
|
129 | - * @param int $internal_code |
|
130 | - * |
|
131 | - * @return int |
|
132 | - * |
|
133 | - * @throws \InvalidArgumentException |
|
134 | - */ |
|
135 | - public static function getCodeForInternalOffset(int $internal_code): int |
|
136 | - { |
|
137 | - $min = static::RESERVED_MIN_API_CODE_OFFSET; |
|
138 | - $max = static::RESERVED_MAX_API_CODE_OFFSET; |
|
139 | - Validator::assertIsIntRange('internal_code', $internal_code, $min, $max); |
|
140 | - |
|
141 | - return ($internal_code === 0) ? 0 : $internal_code + static::getMinCode(); |
|
142 | - } |
|
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 | + */ |
|
67 | + public static function getApiCodeConstants(): array |
|
68 | + { |
|
69 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
70 | + return (new \ReflectionClass(static::class))->getConstants(); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Returns complete error code to locale string mapping array |
|
75 | + * |
|
76 | + * @return array |
|
77 | + * |
|
78 | + * @throws \RuntimeException Thrown when builder map is not configured. |
|
79 | + */ |
|
80 | + public static function getMap(): array |
|
81 | + { |
|
82 | + $user_map = Config::get(ResponseBuilder::CONF_KEY_MAP, null); |
|
83 | + if ($user_map === null) { |
|
84 | + throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', ResponseBuilder::CONF_KEY_MAP)); |
|
85 | + } |
|
86 | + if (!\is_array($user_map)) { |
|
87 | + throw new \RuntimeException(sprintf('CONFIG: "%s" must be an array', ResponseBuilder::CONF_KEY_MAP)); |
|
88 | + } |
|
89 | + return Util::mergeConfig(BaseApiCodes::getBaseMap(), $user_map); |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Returns locale mappings key for given api code or @null if there's no mapping |
|
94 | + * |
|
95 | + * @param integer $api_code Api code to look for mapped message for. |
|
96 | + * |
|
97 | + * @return string|null |
|
98 | + * |
|
99 | + * @throws \InvalidArgumentException If $code is not in allowed range. |
|
100 | + */ |
|
101 | + public static function getCodeMessageKey(int $api_code): ?string |
|
102 | + { |
|
103 | + if (!static::isCodeValid($api_code)) { |
|
104 | + $min = static::getMinCode(); |
|
105 | + $max = static::getMaxCode(); |
|
106 | + throw new \InvalidArgumentException("API code value ({$api_code}) is out of allowed range {$min}-{$max}"); |
|
107 | + } |
|
108 | + |
|
109 | + $map = static::getMap(); |
|
110 | + |
|
111 | + return $map[ $api_code ] ?? null; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Checks if given API $code can be used in current configuration. |
|
116 | + * |
|
117 | + * @param int $code API code to validate |
|
118 | + * |
|
119 | + * @return bool |
|
120 | + */ |
|
121 | + public static function isCodeValid(int $code): bool |
|
122 | + { |
|
123 | + return ($code === 0) || (($code >= static::getMinCode()) && ($code <= static::getMaxCode())); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Returns final API code for internal code, remapped to configured code range |
|
128 | + * |
|
129 | + * @param int $internal_code |
|
130 | + * |
|
131 | + * @return int |
|
132 | + * |
|
133 | + * @throws \InvalidArgumentException |
|
134 | + */ |
|
135 | + public static function getCodeForInternalOffset(int $internal_code): int |
|
136 | + { |
|
137 | + $min = static::RESERVED_MIN_API_CODE_OFFSET; |
|
138 | + $max = static::RESERVED_MAX_API_CODE_OFFSET; |
|
139 | + Validator::assertIsIntRange('internal_code', $internal_code, $min, $max); |
|
140 | + |
|
141 | + return ($internal_code === 0) ? 0 : $internal_code + static::getMinCode(); |
|
142 | + } |
|
143 | 143 | |
144 | 144 | } |
@@ -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 | } |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | * @return HttpResponse |
85 | 85 | */ |
86 | 86 | public static function success($data = null, $api_code = null, array $placeholders = null, |
87 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
87 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
88 | 88 | { |
89 | 89 | return ResponseBuilder::asSuccess($api_code) |
90 | 90 | ->withData($data) |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | * @return HttpResponse |
113 | 113 | */ |
114 | 114 | public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
115 | - int $json_opts = null): HttpResponse |
|
115 | + int $json_opts = null): HttpResponse |
|
116 | 116 | { |
117 | 117 | return ResponseBuilder::asError($api_code) |
118 | 118 | ->withPlaceholders($placeholders) |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | public function withHttpCode(int $http_code = null): self |
162 | 162 | { |
163 | 163 | Validator::assertIsType('http_code', $http_code, [Validator::TYPE_INTEGER, |
164 | - Validator::TYPE_NULL]); |
|
164 | + Validator::TYPE_NULL]); |
|
165 | 165 | $this->http_code = $http_code; |
166 | 166 | |
167 | 167 | return $this; |
@@ -175,8 +175,8 @@ discard block |
||
175 | 175 | public function withData($data = null): self |
176 | 176 | { |
177 | 177 | Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY, |
178 | - Validator::TYPE_OBJECT, |
|
179 | - Validator::TYPE_NULL]); |
|
178 | + Validator::TYPE_OBJECT, |
|
179 | + Validator::TYPE_NULL]); |
|
180 | 180 | $this->data = $data; |
181 | 181 | |
182 | 182 | return $this; |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | public function withJsonOptions(int $json_opts = null): self |
191 | 191 | { |
192 | 192 | Validator::assertIsType('json_opts', $json_opts, [Validator::TYPE_INTEGER, |
193 | - Validator::TYPE_NULL]); |
|
193 | + Validator::TYPE_NULL]); |
|
194 | 194 | $this->json_opts = $json_opts; |
195 | 195 | |
196 | 196 | return $this; |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | public function withDebugData(array $debug_data = null): self |
205 | 205 | { |
206 | 206 | Validator::assertIsType('$debug_data', $debug_data, [Validator::TYPE_ARRAY, |
207 | - Validator::TYPE_NULL]); |
|
207 | + Validator::TYPE_NULL]); |
|
208 | 208 | $this->debug_data = $debug_data; |
209 | 209 | |
210 | 210 | return $this; |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | public function withMessage(string $msg = null): self |
219 | 219 | { |
220 | 220 | Validator::assertIsType('message', $msg, [Validator::TYPE_STRING, |
221 | - Validator::TYPE_NULL]); |
|
221 | + Validator::TYPE_NULL]); |
|
222 | 222 | $this->message = $msg; |
223 | 223 | |
224 | 224 | return $this; |
@@ -305,8 +305,8 @@ discard block |
||
305 | 305 | * @noinspection PhpTooManyParametersInspection |
306 | 306 | */ |
307 | 307 | protected function make(bool $success, int $api_code, $msg_or_api_code, $data = null, |
308 | - int $http_code = null, array $placeholders = null, array $http_headers = null, |
|
309 | - int $json_opts = null, array $debug_data = null): HttpResponse |
|
308 | + int $http_code = null, array $placeholders = null, array $http_headers = null, |
|
309 | + int $json_opts = null, array $debug_data = null): HttpResponse |
|
310 | 310 | { |
311 | 311 | $http_headers = $http_headers ?? []; |
312 | 312 | $http_code = $http_code ?? ($success ? ResponseBuilder::DEFAULT_HTTP_CODE_OK : ResponseBuilder::DEFAULT_HTTP_CODE_ERROR); |
@@ -345,8 +345,8 @@ discard block |
||
345 | 345 | * @noinspection PhpTooManyParametersInspection |
346 | 346 | */ |
347 | 347 | protected function buildResponse(bool $success, int $api_code, |
348 | - $msg_or_api_code, array $placeholders = null, |
|
349 | - $data = null, array $debug_data = null): array |
|
348 | + $msg_or_api_code, array $placeholders = null, |
|
349 | + $data = null, array $debug_data = null): array |
|
350 | 350 | { |
351 | 351 | // ensure $data is either @null, array or object of class with configured mapping. |
352 | 352 | $data = (new Converter())->convert($data); |
@@ -352,7 +352,7 @@ discard block |
||
352 | 352 | $data = (new Converter())->convert($data); |
353 | 353 | if ($data !== null && !\is_object($data)) { |
354 | 354 | // ensure we get object in final JSON structure in data node |
355 | - $data = (object)$data; |
|
355 | + $data = (object) $data; |
|
356 | 356 | } |
357 | 357 | |
358 | 358 | // get human readable message for API code or use message string (if given instead of API code) |
@@ -374,7 +374,7 @@ discard block |
||
374 | 374 | |
375 | 375 | if ($debug_data !== null) { |
376 | 376 | $debug_key = Config::get(ResponseBuilder::CONF_KEY_DEBUG_DEBUG_KEY, ResponseBuilder::KEY_DEBUG); |
377 | - $response[ $debug_key ] = $debug_data; |
|
377 | + $response[$debug_key] = $debug_data; |
|
378 | 378 | } |
379 | 379 | |
380 | 380 | return $response; |
@@ -25,56 +25,56 @@ |
||
25 | 25 | |
26 | 26 | class ResponseBuilderServiceProvider extends ServiceProvider |
27 | 27 | { |
28 | - protected $config_files = [ |
|
29 | - 'response_builder.php', |
|
30 | - ]; |
|
28 | + protected $config_files = [ |
|
29 | + 'response_builder.php', |
|
30 | + ]; |
|
31 | 31 | |
32 | - /** |
|
33 | - * Register bindings in the container. |
|
34 | - * |
|
35 | - * @return void |
|
36 | - */ |
|
37 | - public function register() |
|
38 | - { |
|
39 | - foreach ($this->config_files as $file) { |
|
40 | - $this->mergeConfigFrom(__DIR__ . "/../config/{$file}", ResponseBuilder::CONF_CONFIG); |
|
41 | - } |
|
42 | - } |
|
32 | + /** |
|
33 | + * Register bindings in the container. |
|
34 | + * |
|
35 | + * @return void |
|
36 | + */ |
|
37 | + public function register() |
|
38 | + { |
|
39 | + foreach ($this->config_files as $file) { |
|
40 | + $this->mergeConfigFrom(__DIR__ . "/../config/{$file}", ResponseBuilder::CONF_CONFIG); |
|
41 | + } |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Sets up package resources |
|
46 | - * |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - public function boot() |
|
50 | - { |
|
51 | - $this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder'); |
|
44 | + /** |
|
45 | + * Sets up package resources |
|
46 | + * |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + public function boot() |
|
50 | + { |
|
51 | + $this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder'); |
|
52 | 52 | |
53 | - foreach ($this->config_files as $file) { |
|
54 | - $this->publishes([__DIR__ . "/../config/{$file}" => config_path($file)]); |
|
55 | - } |
|
56 | - } |
|
53 | + foreach ($this->config_files as $file) { |
|
54 | + $this->publishes([__DIR__ . "/../config/{$file}" => config_path($file)]); |
|
55 | + } |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Merge the given configuration with the existing configuration. |
|
60 | - * |
|
61 | - * @param string $path |
|
62 | - * @param string $key |
|
63 | - * |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - protected function mergeConfigFrom($path, $key) |
|
67 | - { |
|
68 | - $defaults = require $path; |
|
69 | - $config = $this->app['config']->get($key, []); |
|
58 | + /** |
|
59 | + * Merge the given configuration with the existing configuration. |
|
60 | + * |
|
61 | + * @param string $path |
|
62 | + * @param string $key |
|
63 | + * |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + protected function mergeConfigFrom($path, $key) |
|
67 | + { |
|
68 | + $defaults = require $path; |
|
69 | + $config = $this->app['config']->get($key, []); |
|
70 | 70 | |
71 | - $merged_config = Util::mergeConfig($defaults, $config); |
|
71 | + $merged_config = Util::mergeConfig($defaults, $config); |
|
72 | 72 | |
73 | - if (\array_key_exists('converter', $merged_config)) { |
|
74 | - Util::sortArrayByPri($merged_config['converter']); |
|
75 | - } |
|
73 | + if (\array_key_exists('converter', $merged_config)) { |
|
74 | + Util::sortArrayByPri($merged_config['converter']); |
|
75 | + } |
|
76 | 76 | |
77 | - $this->app['config']->set($key, $merged_config); |
|
78 | - } |
|
77 | + $this->app['config']->set($key, $merged_config); |
|
78 | + } |
|
79 | 79 | |
80 | 80 | } |
@@ -15,58 +15,58 @@ |
||
15 | 15 | */ |
16 | 16 | final class Util |
17 | 17 | { |
18 | - /** |
|
19 | - * Merges the configs together and takes multi-dimensional arrays into account. |
|
20 | - * Support for multi-dimensional config array. Built-in config merge only supports flat arrays. |
|
21 | - * Throws \RuntimeException if arrays stucture causes type conflics (i.e. you want to merge |
|
22 | - * array with int). |
|
23 | - * |
|
24 | - * @param array $original Array to merge other array into. Usually default values to overwrite. |
|
25 | - * @param array $merging Array with items to be merged into $original, overriding (primitives) or merging |
|
26 | - * (arrays) entries in destination array. |
|
27 | - * |
|
28 | - * @return array |
|
29 | - * |
|
30 | - * @throws \RuntimeException |
|
31 | - */ |
|
32 | - public static function mergeConfig(array $original, array $merging): array |
|
33 | - { |
|
34 | - $array = $original; |
|
35 | - foreach ($merging as $m_key => $m_val) { |
|
36 | - if (\array_key_exists($m_key, $original)) { |
|
37 | - $orig_type = \gettype($original[ $m_key ]); |
|
38 | - $m_type = \gettype($m_val); |
|
39 | - if ($orig_type !== $m_type) { |
|
40 | - throw new \RuntimeException( |
|
41 | - "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
|
42 | - } |
|
18 | + /** |
|
19 | + * Merges the configs together and takes multi-dimensional arrays into account. |
|
20 | + * Support for multi-dimensional config array. Built-in config merge only supports flat arrays. |
|
21 | + * Throws \RuntimeException if arrays stucture causes type conflics (i.e. you want to merge |
|
22 | + * array with int). |
|
23 | + * |
|
24 | + * @param array $original Array to merge other array into. Usually default values to overwrite. |
|
25 | + * @param array $merging Array with items to be merged into $original, overriding (primitives) or merging |
|
26 | + * (arrays) entries in destination array. |
|
27 | + * |
|
28 | + * @return array |
|
29 | + * |
|
30 | + * @throws \RuntimeException |
|
31 | + */ |
|
32 | + public static function mergeConfig(array $original, array $merging): array |
|
33 | + { |
|
34 | + $array = $original; |
|
35 | + foreach ($merging as $m_key => $m_val) { |
|
36 | + if (\array_key_exists($m_key, $original)) { |
|
37 | + $orig_type = \gettype($original[ $m_key ]); |
|
38 | + $m_type = \gettype($m_val); |
|
39 | + if ($orig_type !== $m_type) { |
|
40 | + throw new \RuntimeException( |
|
41 | + "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
|
42 | + } |
|
43 | 43 | |
44 | - if (\is_array($merging[ $m_key ])) { |
|
45 | - $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
46 | - } else { |
|
47 | - $array[ $m_key ] = $m_val; |
|
48 | - } |
|
49 | - } else { |
|
50 | - $array[ $m_key ] = $m_val; |
|
51 | - } |
|
52 | - } |
|
44 | + if (\is_array($merging[ $m_key ])) { |
|
45 | + $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
46 | + } else { |
|
47 | + $array[ $m_key ] = $m_val; |
|
48 | + } |
|
49 | + } else { |
|
50 | + $array[ $m_key ] = $m_val; |
|
51 | + } |
|
52 | + } |
|
53 | 53 | |
54 | - return $array; |
|
55 | - } |
|
54 | + return $array; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Sorts array (in place) by value, assuming value is an array and contains `pri` key with integer |
|
59 | - * (positive/negative) value which is used for sorting higher -> lower priority. |
|
60 | - * |
|
61 | - * @param array &$array |
|
62 | - */ |
|
63 | - public static function sortArrayByPri(array &$array): void |
|
64 | - { |
|
65 | - uasort($array, static function(array $array_a, array $array_b) { |
|
66 | - $pri_a = $array_a['pri'] ?? 0; |
|
67 | - $pri_b = $array_b['pri'] ?? 0; |
|
57 | + /** |
|
58 | + * Sorts array (in place) by value, assuming value is an array and contains `pri` key with integer |
|
59 | + * (positive/negative) value which is used for sorting higher -> lower priority. |
|
60 | + * |
|
61 | + * @param array &$array |
|
62 | + */ |
|
63 | + public static function sortArrayByPri(array &$array): void |
|
64 | + { |
|
65 | + uasort($array, static function(array $array_a, array $array_b) { |
|
66 | + $pri_a = $array_a['pri'] ?? 0; |
|
67 | + $pri_b = $array_b['pri'] ?? 0; |
|
68 | 68 | |
69 | - return $pri_b <=> $pri_a; |
|
70 | - }); |
|
71 | - } |
|
69 | + return $pri_b <=> $pri_a; |
|
70 | + }); |
|
71 | + } |
|
72 | 72 | } |
@@ -34,20 +34,20 @@ |
||
34 | 34 | $array = $original; |
35 | 35 | foreach ($merging as $m_key => $m_val) { |
36 | 36 | if (\array_key_exists($m_key, $original)) { |
37 | - $orig_type = \gettype($original[ $m_key ]); |
|
37 | + $orig_type = \gettype($original[$m_key]); |
|
38 | 38 | $m_type = \gettype($m_val); |
39 | 39 | if ($orig_type !== $m_type) { |
40 | 40 | throw new \RuntimeException( |
41 | 41 | "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
42 | 42 | } |
43 | 43 | |
44 | - if (\is_array($merging[ $m_key ])) { |
|
45 | - $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
44 | + if (\is_array($merging[$m_key])) { |
|
45 | + $array[$m_key] = static::mergeConfig($original[$m_key], $m_val); |
|
46 | 46 | } else { |
47 | - $array[ $m_key ] = $m_val; |
|
47 | + $array[$m_key] = $m_val; |
|
48 | 48 | } |
49 | 49 | } else { |
50 | - $array[ $m_key ] = $m_val; |
|
50 | + $array[$m_key] = $m_val; |
|
51 | 51 | } |
52 | 52 | } |
53 | 53 |
@@ -19,19 +19,19 @@ |
||
19 | 19 | |
20 | 20 | final class ToArrayConverter implements ConverterContract |
21 | 21 | { |
22 | - /** |
|
23 | - * Returns array representation of the object. |
|
24 | - * |
|
25 | - * @param object $obj Object to be converted |
|
26 | - * @param array $config Converter config array to be used for this object (based on exact class |
|
27 | - * name match or inheritance). |
|
28 | - * |
|
29 | - * @return array |
|
30 | - */ |
|
31 | - public function convert($obj, /** @scrutinizer ignore-unused */ array $config): array |
|
32 | - { |
|
33 | - Validator::assertIsObject('obj', $obj); |
|
22 | + /** |
|
23 | + * Returns array representation of the object. |
|
24 | + * |
|
25 | + * @param object $obj Object to be converted |
|
26 | + * @param array $config Converter config array to be used for this object (based on exact class |
|
27 | + * name match or inheritance). |
|
28 | + * |
|
29 | + * @return array |
|
30 | + */ |
|
31 | + public function convert($obj, /** @scrutinizer ignore-unused */ array $config): array |
|
32 | + { |
|
33 | + Validator::assertIsObject('obj', $obj); |
|
34 | 34 | |
35 | - return $obj->toArray(); |
|
36 | - } |
|
35 | + return $obj->toArray(); |
|
36 | + } |
|
37 | 37 | } |
@@ -19,19 +19,19 @@ |
||
19 | 19 | |
20 | 20 | final class ArrayableConverter implements ConverterContract |
21 | 21 | { |
22 | - /** |
|
23 | - * Returns array representation of the object implementing Arrayable interface |
|
24 | - * |
|
25 | - * @param Arrayable $obj Object to be converted |
|
26 | - * @param array $config Converter config array to be used for this object (based on exact class |
|
27 | - * name match or inheritance). |
|
28 | - * |
|
29 | - * @return array |
|
30 | - */ |
|
31 | - public function convert($obj, /** @scrutinizer ignore-unused */ array $config): array |
|
32 | - { |
|
33 | - Validator::assertInstanceOf('obj', $obj, Arrayable::class); |
|
22 | + /** |
|
23 | + * Returns array representation of the object implementing Arrayable interface |
|
24 | + * |
|
25 | + * @param Arrayable $obj Object to be converted |
|
26 | + * @param array $config Converter config array to be used for this object (based on exact class |
|
27 | + * name match or inheritance). |
|
28 | + * |
|
29 | + * @return array |
|
30 | + */ |
|
31 | + public function convert($obj, /** @scrutinizer ignore-unused */ array $config): array |
|
32 | + { |
|
33 | + Validator::assertInstanceOf('obj', $obj, Arrayable::class); |
|
34 | 34 | |
35 | - return $obj->toArray(); |
|
36 | - } |
|
35 | + return $obj->toArray(); |
|
36 | + } |
|
37 | 37 | } |
@@ -44,11 +44,11 @@ discard block |
||
44 | 44 | do { |
45 | 45 | if ($cfg === null) { |
46 | 46 | // Default handler MUST be present by design and always return something useful. |
47 | - $cfg = self::getExceptionHandlerConfig()[ ResponseBuilder::KEY_DEFAULT ]; |
|
47 | + $cfg = self::getExceptionHandlerConfig()[ResponseBuilder::KEY_DEFAULT]; |
|
48 | 48 | } |
49 | 49 | |
50 | - $handler = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
51 | - $handler_result = $handler->handle($cfg[ ResponseBuilder::KEY_CONFIG ], $ex); |
|
50 | + $handler = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
51 | + $handler_result = $handler->handle($cfg[ResponseBuilder::KEY_CONFIG], $ex); |
|
52 | 52 | if ($handler_result !== null) { |
53 | 53 | $result = self::processException($ex, $handler_result); |
54 | 54 | } else { |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | $cfg = self::getExceptionHandlerConfig(); |
148 | 148 | |
149 | 149 | // This config entry is guaranted to exist. Enforced by tests. |
150 | - $cfg = $cfg[ HttpException::class ][ ResponseBuilder::KEY_CONFIG ][ HttpResponse::HTTP_UNAUTHORIZED ]; |
|
150 | + $cfg = $cfg[HttpException::class][ResponseBuilder::KEY_CONFIG][HttpResponse::HTTP_UNAUTHORIZED]; |
|
151 | 151 | |
152 | 152 | return static::processException($exception, $cfg, HttpResponse::HTTP_UNAUTHORIZED); |
153 | 153 | } |
@@ -280,13 +280,13 @@ discard block |
||
280 | 280 | |
281 | 281 | // check for exact class name match... |
282 | 282 | if (\array_key_exists($cls, $cfg)) { |
283 | - $result = $cfg[ $cls ]; |
|
283 | + $result = $cfg[$cls]; |
|
284 | 284 | } else { |
285 | 285 | // no exact match, then lets try with `instanceof` |
286 | 286 | // Config entries are already sorted by priority. |
287 | 287 | foreach (\array_keys($cfg) as $class_name) { |
288 | 288 | if ($ex instanceof $class_name) { |
289 | - $result = $cfg[ $class_name ]; |
|
289 | + $result = $cfg[$class_name]; |
|
290 | 290 | break; |
291 | 291 | } |
292 | 292 | } |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | * @return \Symfony\Component\HttpFoundation\Response |
73 | 73 | */ |
74 | 74 | protected static function processException(\Exception $ex, array $ex_cfg, |
75 | - int $fallback_http_code = HttpResponse::HTTP_INTERNAL_SERVER_ERROR) |
|
75 | + int $fallback_http_code = HttpResponse::HTTP_INTERNAL_SERVER_ERROR) |
|
76 | 76 | { |
77 | 77 | $api_code = $ex_cfg['api_code']; |
78 | 78 | $http_code = $ex_cfg['http_code'] ?? $fallback_http_code; |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | * @return HttpResponse |
142 | 142 | */ |
143 | 143 | protected function unauthenticated(/** @scrutinizer ignore-unused */ $request, |
144 | - AuthException $exception): HttpResponse |
|
144 | + AuthException $exception): HttpResponse |
|
145 | 145 | { |
146 | 146 | $cfg = self::getExceptionHandlerConfig(); |
147 | 147 | |
@@ -162,7 +162,7 @@ discard block |
||
162 | 162 | * @return HttpResponse |
163 | 163 | */ |
164 | 164 | protected static function error(Exception $ex, |
165 | - int $api_code, int $http_code = null, string $error_message = null): HttpResponse |
|
165 | + int $api_code, int $http_code = null, string $error_message = null): HttpResponse |
|
166 | 166 | { |
167 | 167 | $ex_http_code = ($ex instanceof HttpException) ? $ex->getStatusCode() : $ex->getCode(); |
168 | 168 | $http_code = $http_code ?? $ex_http_code; |