@@ -42,17 +42,17 @@ |
||
42 | 42 | $config = \array_replace($default_config, $user_config); |
43 | 43 | |
44 | 44 | $http_code = $ex->getStatusCode(); |
45 | - $result = $config[ $http_code ] ?? null; |
|
45 | + $result = $config[$http_code] ?? null; |
|
46 | 46 | |
47 | 47 | if ($result === null) { |
48 | - $result = $config[ ResponseBuilder::KEY_DEFAULT ]; |
|
48 | + $result = $config[ResponseBuilder::KEY_DEFAULT]; |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | if (!\array_key_exists(ResponseBuilder::KEY_HTTP_CODE, $result)) { |
52 | - $result[ ResponseBuilder::KEY_HTTP_CODE ] = $http_code; |
|
52 | + $result[ResponseBuilder::KEY_HTTP_CODE] = $http_code; |
|
53 | 53 | } |
54 | 54 | if (\array_key_exists(ResponseBuilder::KEY_MSG_KEY, $result)) { |
55 | - $result[ ResponseBuilder::KEY_MSG_KEY ] = \sprintf('response-builder::builder.http_%d', $http_code); |
|
55 | + $result[ResponseBuilder::KEY_MSG_KEY] = \sprintf('response-builder::builder.http_%d', $http_code); |
|
56 | 56 | } |
57 | 57 | return $result; |
58 | 58 | } |
@@ -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 | } |
@@ -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; |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | * @return HttpResponse |
143 | 143 | */ |
144 | 144 | protected function unauthenticated(/** @scrutinizer ignore-unused */ $request, |
145 | - AuthException $exception): HttpResponse |
|
145 | + AuthException $exception): HttpResponse |
|
146 | 146 | { |
147 | 147 | $cfg = self::getExceptionHandlerConfig(); |
148 | 148 | |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | * @return HttpResponse |
164 | 164 | */ |
165 | 165 | protected static function error(Exception $ex, |
166 | - int $api_code, int $http_code = null, string $error_message): HttpResponse |
|
166 | + int $api_code, int $http_code = null, string $error_message): HttpResponse |
|
167 | 167 | { |
168 | 168 | $ex_http_code = ($ex instanceof HttpException) ? $ex->getStatusCode() : $ex->getCode(); |
169 | 169 | $http_code = $http_code ?? $ex_http_code; |
@@ -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 | } |
@@ -15,60 +15,60 @@ |
||
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 | |
73 | 73 | /** |
74 | 74 | * Prints content of given array in compacted form. |
@@ -12,78 +12,78 @@ discard block |
||
12 | 12 | */ |
13 | 13 | |
14 | 14 | return [ |
15 | - /* |
|
15 | + /* |
|
16 | 16 | |----------------------------------------------------------------------------------------------------------- |
17 | 17 | | Code range settings |
18 | 18 | |----------------------------------------------------------------------------------------------------------- |
19 | 19 | */ |
20 | - 'min_code' => 100, |
|
21 | - 'max_code' => 1024, |
|
20 | + 'min_code' => 100, |
|
21 | + 'max_code' => 1024, |
|
22 | 22 | |
23 | - /* |
|
23 | + /* |
|
24 | 24 | |----------------------------------------------------------------------------------------------------------- |
25 | 25 | | Error code to message mapping |
26 | 26 | |----------------------------------------------------------------------------------------------------------- |
27 | 27 | | |
28 | 28 | */ |
29 | - 'map' => [ |
|
30 | - // YOUR_API_CODE => '<MESSAGE_KEY>', |
|
31 | - ], |
|
29 | + 'map' => [ |
|
30 | + // YOUR_API_CODE => '<MESSAGE_KEY>', |
|
31 | + ], |
|
32 | 32 | |
33 | - /* |
|
33 | + /* |
|
34 | 34 | |----------------------------------------------------------------------------------------------------------- |
35 | 35 | | Response Builder data converter |
36 | 36 | |----------------------------------------------------------------------------------------------------------- |
37 | 37 | | |
38 | 38 | */ |
39 | - 'converter' => [ |
|
40 | - \Illuminate\Database\Eloquent\Model::class => [ |
|
41 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
42 | - // 'key' => 'item', |
|
43 | - 'pri' => 0, |
|
44 | - ], |
|
45 | - \Illuminate\Support\Collection::class => [ |
|
46 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
47 | - // 'key' => 'item', |
|
48 | - 'pri' => 0, |
|
49 | - ], |
|
50 | - \Illuminate\Database\Eloquent\Collection::class => [ |
|
51 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
52 | - // 'key' => 'item', |
|
53 | - 'pri' => 0, |
|
54 | - ], |
|
55 | - \Illuminate\Http\Resources\Json\JsonResource::class => [ |
|
56 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
57 | - // 'key' => 'item', |
|
58 | - 'pri' => 0, |
|
59 | - ], |
|
39 | + 'converter' => [ |
|
40 | + \Illuminate\Database\Eloquent\Model::class => [ |
|
41 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
42 | + // 'key' => 'item', |
|
43 | + 'pri' => 0, |
|
44 | + ], |
|
45 | + \Illuminate\Support\Collection::class => [ |
|
46 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
47 | + // 'key' => 'item', |
|
48 | + 'pri' => 0, |
|
49 | + ], |
|
50 | + \Illuminate\Database\Eloquent\Collection::class => [ |
|
51 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
52 | + // 'key' => 'item', |
|
53 | + 'pri' => 0, |
|
54 | + ], |
|
55 | + \Illuminate\Http\Resources\Json\JsonResource::class => [ |
|
56 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
57 | + // 'key' => 'item', |
|
58 | + 'pri' => 0, |
|
59 | + ], |
|
60 | 60 | |
61 | - /* |
|
61 | + /* |
|
62 | 62 | |----------------------------------------------------------------------------------------------------------- |
63 | 63 | | Generic converters should have lower pri to allow dedicated ones to kick in first when class matches |
64 | 64 | |----------------------------------------------------------------------------------------------------------- |
65 | 65 | */ |
66 | - \JsonSerializable::class => [ |
|
67 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class, |
|
68 | - // 'key' => 'item', |
|
69 | - 'pri' => -10, |
|
70 | - ], |
|
71 | - \Illuminate\Contracts\Support\Arrayable::class => [ |
|
72 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class, |
|
73 | - // 'key' => 'item', |
|
74 | - 'pri' => -10, |
|
75 | - ], |
|
66 | + \JsonSerializable::class => [ |
|
67 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class, |
|
68 | + // 'key' => 'item', |
|
69 | + 'pri' => -10, |
|
70 | + ], |
|
71 | + \Illuminate\Contracts\Support\Arrayable::class => [ |
|
72 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class, |
|
73 | + // 'key' => 'item', |
|
74 | + 'pri' => -10, |
|
75 | + ], |
|
76 | 76 | |
77 | - ], |
|
77 | + ], |
|
78 | 78 | |
79 | - /* |
|
79 | + /* |
|
80 | 80 | |----------------------------------------------------------------------------------------------------------- |
81 | 81 | | Exception handler error codes |
82 | 82 | |----------------------------------------------------------------------------------------------------------- |
83 | 83 | | |
84 | 84 | */ |
85 | - 'exception_handler' => [ |
|
86 | - /* |
|
85 | + 'exception_handler' => [ |
|
86 | + /* |
|
87 | 87 | * The following keys are supported for each handler specified. |
88 | 88 | * `handler` |
89 | 89 | * `pri` |
@@ -103,17 +103,17 @@ discard block |
||
103 | 103 | * message ($ex->getMessage()). |
104 | 104 | */ |
105 | 105 | |
106 | - Illuminate\Validation\ValidationException::class => [ |
|
107 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class, |
|
108 | - 'pri' => -100, |
|
109 | - 'config' => [ |
|
110 | - ], |
|
111 | - ], |
|
106 | + Illuminate\Validation\ValidationException::class => [ |
|
107 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class, |
|
108 | + 'pri' => -100, |
|
109 | + 'config' => [ |
|
110 | + ], |
|
111 | + ], |
|
112 | 112 | |
113 | 113 | \Symfony\Component\HttpKernel\Exception\HttpException::class => [ |
114 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
115 | - 'pri' => -100, |
|
116 | - 'config' => [ |
|
114 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
115 | + 'pri' => -100, |
|
116 | + 'config' => [ |
|
117 | 117 | // HttpException::class => [ |
118 | 118 | // // used by unauthenticated() to obtain api and http code for the exception |
119 | 119 | // HttpResponse::HTTP_UNAUTHORIZED => [ |
@@ -129,41 +129,41 @@ discard block |
||
129 | 129 | // 'http_code' => HttpResponse::HTTP_BAD_REQUEST, |
130 | 130 | // ], |
131 | 131 | // ], |
132 | - ], |
|
132 | + ], |
|
133 | 133 | // // This is final exception handler. If ex is not dealt with yet this is its last stop. |
134 | - // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
|
134 | + // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
|
135 | 135 | |
136 | - 'default' => [ |
|
137 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
138 | - 'pri' => -127, |
|
139 | - 'config' => [ |
|
136 | + 'default' => [ |
|
137 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
138 | + 'pri' => -127, |
|
139 | + 'config' => [ |
|
140 | 140 | // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_UNHANDLED_EXCEPTION, |
141 | 141 | // 'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR, |
142 | - ], |
|
143 | - ], |
|
144 | - ], |
|
145 | - ], |
|
142 | + ], |
|
143 | + ], |
|
144 | + ], |
|
145 | + ], |
|
146 | 146 | |
147 | - /* |
|
147 | + /* |
|
148 | 148 | |----------------------------------------------------------------------------------------------------------- |
149 | 149 | | data-to-json encoding options |
150 | 150 | |----------------------------------------------------------------------------------------------------------- |
151 | 151 | | |
152 | 152 | */ |
153 | - 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
153 | + 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
154 | 154 | |
155 | - /* |
|
155 | + /* |
|
156 | 156 | |----------------------------------------------------------------------------------------------------------- |
157 | 157 | | Debug config |
158 | 158 | |----------------------------------------------------------------------------------------------------------- |
159 | 159 | | |
160 | 160 | */ |
161 | - 'debug' => [ |
|
162 | - 'debug_key' => 'debug', |
|
163 | - 'exception_handler' => [ |
|
164 | - 'trace_key' => 'trace', |
|
165 | - 'trace_enabled' => env('APP_DEBUG', false), |
|
166 | - ], |
|
167 | - ], |
|
161 | + 'debug' => [ |
|
162 | + 'debug_key' => 'debug', |
|
163 | + 'exception_handler' => [ |
|
164 | + 'trace_key' => 'trace', |
|
165 | + 'trace_enabled' => env('APP_DEBUG', false), |
|
166 | + ], |
|
167 | + ], |
|
168 | 168 | |
169 | 169 | ]; |