@@ -132,7 +132,7 @@ |
||
132 | 132 | * @throws \InvalidArgumentException |
133 | 133 | */ |
134 | 134 | public static function assertIsType(string $var_name, $value, array $allowed_types, |
135 | - string $ex_class = Ex\InvalidTypeException::class): void |
|
135 | + string $ex_class = Ex\InvalidTypeException::class): void |
|
136 | 136 | { |
137 | 137 | $type = \gettype($value); |
138 | 138 | if (!\in_array($type, $allowed_types, true)) { |
@@ -78,7 +78,7 @@ |
||
78 | 78 | |
79 | 79 | Util::sortArrayByPri($merged_config['converter']['classes']); |
80 | 80 | |
81 | - $this->app['config']->set($key, $merged_config); |
|
82 | - } |
|
81 | + $this->app['config']->set($key, $merged_config); |
|
82 | + } |
|
83 | 83 | |
84 | 84 | } |
@@ -22,19 +22,19 @@ |
||
22 | 22 | */ |
23 | 23 | final class ToArrayConverter implements ConverterContract |
24 | 24 | { |
25 | - /** |
|
26 | - * Returns array representation of the object. |
|
27 | - * |
|
28 | - * @param object $obj Object to be converted |
|
29 | - * @param array $config Converter config array to be used for this object (based on exact class |
|
30 | - * name match or inheritance). |
|
31 | - * |
|
32 | - * @return array |
|
33 | - */ |
|
34 | - public function convert(object $obj, /** @scrutinizer ignore-unused */ array $config): array |
|
35 | - { |
|
36 | - Validator::assertIsObject('obj', $obj); |
|
25 | + /** |
|
26 | + * Returns array representation of the object. |
|
27 | + * |
|
28 | + * @param object $obj Object to be converted |
|
29 | + * @param array $config Converter config array to be used for this object (based on exact class |
|
30 | + * name match or inheritance). |
|
31 | + * |
|
32 | + * @return array |
|
33 | + */ |
|
34 | + public function convert(object $obj, /** @scrutinizer ignore-unused */ array $config): array |
|
35 | + { |
|
36 | + Validator::assertIsObject('obj', $obj); |
|
37 | 37 | |
38 | - return $obj->toArray(); |
|
39 | - } |
|
38 | + return $obj->toArray(); |
|
39 | + } |
|
40 | 40 | } |
@@ -40,20 +40,20 @@ |
||
40 | 40 | $array = $original; |
41 | 41 | foreach ($merging as $m_key => $m_val) { |
42 | 42 | if (\array_key_exists($m_key, $original)) { |
43 | - $orig_type = \gettype($original[ $m_key ]); |
|
43 | + $orig_type = \gettype($original[$m_key]); |
|
44 | 44 | $m_type = \gettype($m_val); |
45 | 45 | if ($orig_type !== $m_type) { |
46 | 46 | throw new Ex\IncompatibleTypeException( |
47 | 47 | "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
48 | 48 | } |
49 | 49 | |
50 | - if (\is_array($merging[ $m_key ])) { |
|
51 | - $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
50 | + if (\is_array($merging[$m_key])) { |
|
51 | + $array[$m_key] = static::mergeConfig($original[$m_key], $m_val); |
|
52 | 52 | } else { |
53 | - $array[ $m_key ] = $m_val; |
|
53 | + $array[$m_key] = $m_val; |
|
54 | 54 | } |
55 | 55 | } else { |
56 | - $array[ $m_key ] = $m_val; |
|
56 | + $array[$m_key] = $m_val; |
|
57 | 57 | } |
58 | 58 | } |
59 | 59 |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | $worker = new $cfg[ RB::KEY_HANDLER ](); |
148 | 148 | $result = $worker->convert($data, $cfg); |
149 | 149 | $result = $cfg[ RB::KEY_KEY ] === null ? $result : [$cfg[ RB::KEY_KEY ] => $result]; |
150 | - } |
|
150 | + } |
|
151 | 151 | |
152 | 152 | if ($result === null && \is_array($data)) { |
153 | 153 | $cfg = $this->getPrimitiveMappingConfigOrThrow($data); |
@@ -225,7 +225,7 @@ discard block |
||
225 | 225 | "Missing '{$key_name}' entry in '{$class_name}' class mapping config."); |
226 | 226 | } |
227 | 227 | |
228 | - Validator::assertIsType(RB::KEY_KEY, $class_config[$key_name], $allowed_types); |
|
228 | + Validator::assertIsType(RB::KEY_KEY, $class_config[$key_name], $allowed_types); |
|
229 | 229 | } |
230 | 230 | } |
231 | 231 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | $result = null; |
61 | 61 | |
62 | 62 | $type = \gettype($data); |
63 | - $result = $this->primitives[ $type ] ?? null; |
|
63 | + $result = $this->primitives[$type] ?? null; |
|
64 | 64 | |
65 | 65 | if ($result === null) { |
66 | 66 | throw new Ex\ConfigurationNotFoundException( |
@@ -92,13 +92,13 @@ discard block |
||
92 | 92 | $cls = \get_class($data); |
93 | 93 | if ($cls !== false) { |
94 | 94 | if (\array_key_exists($cls, $this->classes)) { |
95 | - $result = $this->classes[ $cls ]; |
|
95 | + $result = $this->classes[$cls]; |
|
96 | 96 | $debug_result = 'exact config match'; |
97 | 97 | } else { |
98 | 98 | // no exact match, then lets try with `instanceof` |
99 | 99 | foreach (\array_keys($this->classes) as $class_name) { |
100 | 100 | if ($data instanceof $class_name) { |
101 | - $result = $this->classes[ $class_name ]; |
|
101 | + $result = $this->classes[$class_name]; |
|
102 | 102 | $debug_result = "subclass of {$class_name}"; |
103 | 103 | break; |
104 | 104 | } |
@@ -144,9 +144,9 @@ discard block |
||
144 | 144 | |
145 | 145 | if ($result === null && \is_object($data)) { |
146 | 146 | $cfg = $this->getClassMappingConfigOrThrow($data); |
147 | - $worker = new $cfg[ RB::KEY_HANDLER ](); |
|
147 | + $worker = new $cfg[RB::KEY_HANDLER](); |
|
148 | 148 | $result = $worker->convert($data, $cfg); |
149 | - $result = $cfg[ RB::KEY_KEY ] === null ? $result : [$cfg[ RB::KEY_KEY ] => $result]; |
|
149 | + $result = $cfg[RB::KEY_KEY] === null ? $result : [$cfg[RB::KEY_KEY] => $result]; |
|
150 | 150 | } |
151 | 151 | |
152 | 152 | if ($result === null && \is_array($data)) { |
@@ -154,12 +154,12 @@ discard block |
||
154 | 154 | |
155 | 155 | $result = $this->convertArray($data); |
156 | 156 | if (!Util::isArrayWithNonNumericKeys($data)) { |
157 | - $result = [$cfg[ RB::KEY_KEY ] => $result]; |
|
157 | + $result = [$cfg[RB::KEY_KEY] => $result]; |
|
158 | 158 | } |
159 | 159 | } |
160 | 160 | |
161 | 161 | if (\is_bool($data) || \is_float($data) || \is_int($data) || \is_string($data)) { |
162 | - $result = [$this->getPrimitiveMappingConfigOrThrow($data)[ RB::KEY_KEY ] => $data]; |
|
162 | + $result = [$this->getPrimitiveMappingConfigOrThrow($data)[RB::KEY_KEY] => $data]; |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | return $result; |
@@ -179,12 +179,12 @@ discard block |
||
179 | 179 | |
180 | 180 | foreach ($data as $key => $val) { |
181 | 181 | if (\is_array($val)) { |
182 | - $data[ $key ] = $this->convertArray($val); |
|
182 | + $data[$key] = $this->convertArray($val); |
|
183 | 183 | } elseif (\is_object($val)) { |
184 | 184 | $cfg = $this->getClassMappingConfigOrThrow($val); |
185 | - $worker = new $cfg[ RB::KEY_HANDLER ](); |
|
185 | + $worker = new $cfg[RB::KEY_HANDLER](); |
|
186 | 186 | $converted_data = $worker->convert($val, $cfg); |
187 | - $data[ $key ] = $converted_data; |
|
187 | + $data[$key] = $converted_data; |
|
188 | 188 | } |
189 | 189 | } |
190 | 190 |
@@ -12,117 +12,117 @@ 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 | - 'primitives' => [ |
|
41 | - /* Configuration for primitives used when such data is passed directly as payload (i.e. `success(15)`;) */ |
|
42 | - 'array' => [ |
|
43 | - 'key' => 'values', |
|
44 | - ], |
|
45 | - 'boolean' => [ |
|
46 | - 'key' => 'value', |
|
47 | - ], |
|
48 | - 'double' => [ |
|
49 | - 'key' => 'value', |
|
50 | - ], |
|
51 | - 'integer' => [ |
|
52 | - 'key' => 'value', |
|
53 | - ], |
|
54 | - 'string' => [ |
|
55 | - 'key' => 'value', |
|
56 | - ], |
|
57 | - ], |
|
58 | - |
|
59 | - /* Object converters configuration for supported classes */ |
|
60 | - 'classes' => [ |
|
61 | - \Illuminate\Database\Eloquent\Model::class => [ |
|
62 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
63 | - 'key' => 'item', |
|
64 | - 'pri' => 0, |
|
65 | - ], |
|
66 | - \Illuminate\Support\Collection::class => [ |
|
67 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
68 | - 'key' => 'items', |
|
69 | - 'pri' => 0, |
|
70 | - ], |
|
71 | - \Illuminate\Database\Eloquent\Collection::class => [ |
|
72 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
73 | - 'key' => 'items', |
|
74 | - 'pri' => 0, |
|
75 | - ], |
|
76 | - \Illuminate\Http\Resources\Json\JsonResource::class => [ |
|
77 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
78 | - 'key' => 'item', |
|
79 | - 'pri' => 0, |
|
80 | - ], |
|
81 | - |
|
82 | - /* |
|
39 | + 'converter' => [ |
|
40 | + 'primitives' => [ |
|
41 | + /* Configuration for primitives used when such data is passed directly as payload (i.e. `success(15)`;) */ |
|
42 | + 'array' => [ |
|
43 | + 'key' => 'values', |
|
44 | + ], |
|
45 | + 'boolean' => [ |
|
46 | + 'key' => 'value', |
|
47 | + ], |
|
48 | + 'double' => [ |
|
49 | + 'key' => 'value', |
|
50 | + ], |
|
51 | + 'integer' => [ |
|
52 | + 'key' => 'value', |
|
53 | + ], |
|
54 | + 'string' => [ |
|
55 | + 'key' => 'value', |
|
56 | + ], |
|
57 | + ], |
|
58 | + |
|
59 | + /* Object converters configuration for supported classes */ |
|
60 | + 'classes' => [ |
|
61 | + \Illuminate\Database\Eloquent\Model::class => [ |
|
62 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
63 | + 'key' => 'item', |
|
64 | + 'pri' => 0, |
|
65 | + ], |
|
66 | + \Illuminate\Support\Collection::class => [ |
|
67 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
68 | + 'key' => 'items', |
|
69 | + 'pri' => 0, |
|
70 | + ], |
|
71 | + \Illuminate\Database\Eloquent\Collection::class => [ |
|
72 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
73 | + 'key' => 'items', |
|
74 | + 'pri' => 0, |
|
75 | + ], |
|
76 | + \Illuminate\Http\Resources\Json\JsonResource::class => [ |
|
77 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
78 | + 'key' => 'item', |
|
79 | + 'pri' => 0, |
|
80 | + ], |
|
81 | + |
|
82 | + /* |
|
83 | 83 | |----------------------------------------------------------------------------------------------------------- |
84 | 84 | | Paginators converts to objects already, so we do not array wrapping here, hence setting `key` to null |
85 | 85 | |----------------------------------------------------------------------------------------------------------- |
86 | 86 | */ |
87 | 87 | |
88 | - \Illuminate\Pagination\LengthAwarePaginator::class => [ |
|
89 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class, |
|
90 | - 'key' => null, |
|
91 | - 'pri' => 0, |
|
92 | - ], |
|
93 | - \Illuminate\Pagination\Paginator::class => [ |
|
94 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class, |
|
95 | - 'key' => null, |
|
96 | - 'pri' => 0, |
|
97 | - ], |
|
98 | - |
|
99 | - /* |
|
88 | + \Illuminate\Pagination\LengthAwarePaginator::class => [ |
|
89 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class, |
|
90 | + 'key' => null, |
|
91 | + 'pri' => 0, |
|
92 | + ], |
|
93 | + \Illuminate\Pagination\Paginator::class => [ |
|
94 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class, |
|
95 | + 'key' => null, |
|
96 | + 'pri' => 0, |
|
97 | + ], |
|
98 | + |
|
99 | + /* |
|
100 | 100 | |----------------------------------------------------------------------------------------------------------- |
101 | 101 | | Generic converters should have lower pri to allow dedicated ones to kick in first when class matches |
102 | 102 | |----------------------------------------------------------------------------------------------------------- |
103 | 103 | */ |
104 | - \JsonSerializable::class => [ |
|
105 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class, |
|
106 | - 'key' => 'item', |
|
107 | - 'pri' => -10, |
|
108 | - ], |
|
109 | - \Illuminate\Contracts\Support\Arrayable::class => [ |
|
110 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class, |
|
111 | - 'key' => 'items', |
|
112 | - 'pri' => -10, |
|
113 | - ], |
|
114 | - ], |
|
115 | - |
|
116 | - ], |
|
117 | - |
|
118 | - /* |
|
104 | + \JsonSerializable::class => [ |
|
105 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class, |
|
106 | + 'key' => 'item', |
|
107 | + 'pri' => -10, |
|
108 | + ], |
|
109 | + \Illuminate\Contracts\Support\Arrayable::class => [ |
|
110 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class, |
|
111 | + 'key' => 'items', |
|
112 | + 'pri' => -10, |
|
113 | + ], |
|
114 | + ], |
|
115 | + |
|
116 | + ], |
|
117 | + |
|
118 | + /* |
|
119 | 119 | |----------------------------------------------------------------------------------------------------------- |
120 | 120 | | Exception handler error codes |
121 | 121 | |----------------------------------------------------------------------------------------------------------- |
122 | 122 | | |
123 | 123 | */ |
124 | - 'exception_handler' => [ |
|
125 | - /* |
|
124 | + 'exception_handler' => [ |
|
125 | + /* |
|
126 | 126 | * The following keys are supported for each handler specified. |
127 | 127 | * `handler` |
128 | 128 | * `pri` |
@@ -142,19 +142,19 @@ discard block |
||
142 | 142 | * message ($ex->getMessage()). |
143 | 143 | */ |
144 | 144 | |
145 | - \Illuminate\Validation\ValidationException::class => [ |
|
146 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class, |
|
147 | - 'pri' => -100, |
|
148 | - 'config' => [ |
|
145 | + \Illuminate\Validation\ValidationException::class => [ |
|
146 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class, |
|
147 | + 'pri' => -100, |
|
148 | + 'config' => [ |
|
149 | 149 | // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_VALIDATION_EXCEPTION, |
150 | 150 | // 'http_code' => HttpResponse::HTTP_UNPROCESSABLE_ENTITY, |
151 | - ], |
|
152 | - ], |
|
151 | + ], |
|
152 | + ], |
|
153 | 153 | |
154 | 154 | \Symfony\Component\HttpKernel\Exception\HttpException::class => [ |
155 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
156 | - 'pri' => -100, |
|
157 | - 'config' => [ |
|
155 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
156 | + 'pri' => -100, |
|
157 | + 'config' => [ |
|
158 | 158 | // HttpException::class => [ |
159 | 159 | // // used by unauthenticated() to obtain api and http code for the exception |
160 | 160 | // HttpResponse::HTTP_UNAUTHORIZED => [ |
@@ -170,47 +170,47 @@ discard block |
||
170 | 170 | // 'http_code' => HttpResponse::HTTP_BAD_REQUEST, |
171 | 171 | // ], |
172 | 172 | // ], |
173 | - ], |
|
173 | + ], |
|
174 | 174 | // // This is final exception handler. If ex is not dealt with yet this is its last stop. |
175 | - // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
|
175 | + // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
|
176 | 176 | |
177 | - 'default' => [ |
|
178 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
179 | - 'pri' => -127, |
|
180 | - 'config' => [ |
|
177 | + 'default' => [ |
|
178 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
179 | + 'pri' => -127, |
|
180 | + 'config' => [ |
|
181 | 181 | // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_UNHANDLED_EXCEPTION, |
182 | 182 | // 'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR, |
183 | - ], |
|
184 | - ], |
|
185 | - ], |
|
186 | - ], |
|
183 | + ], |
|
184 | + ], |
|
185 | + ], |
|
186 | + ], |
|
187 | 187 | |
188 | - /* |
|
188 | + /* |
|
189 | 189 | |----------------------------------------------------------------------------------------------------------- |
190 | 190 | | data-to-json encoding options |
191 | 191 | |----------------------------------------------------------------------------------------------------------- |
192 | 192 | | |
193 | 193 | */ |
194 | - 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
194 | + 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
195 | 195 | |
196 | - /* |
|
196 | + /* |
|
197 | 197 | |----------------------------------------------------------------------------------------------------------- |
198 | 198 | | Debug config |
199 | 199 | |----------------------------------------------------------------------------------------------------------- |
200 | 200 | | |
201 | 201 | */ |
202 | - 'debug' => [ |
|
203 | - 'debug_key' => 'debug', |
|
204 | - 'exception_handler' => [ |
|
205 | - 'trace_key' => 'trace', |
|
206 | - 'trace_enabled' => env('APP_DEBUG', false), |
|
207 | - ], |
|
208 | - |
|
209 | - // Controls debugging features of payload converter class. |
|
210 | - 'converter' => [ |
|
211 | - // Set to true to figure out what converter is used for given data payload and why. |
|
212 | - 'debug_enabled' => env('RB_CONVERTER_DEBUG', false), |
|
213 | - ], |
|
214 | - ], |
|
202 | + 'debug' => [ |
|
203 | + 'debug_key' => 'debug', |
|
204 | + 'exception_handler' => [ |
|
205 | + 'trace_key' => 'trace', |
|
206 | + 'trace_enabled' => env('APP_DEBUG', false), |
|
207 | + ], |
|
208 | + |
|
209 | + // Controls debugging features of payload converter class. |
|
210 | + 'converter' => [ |
|
211 | + // Set to true to figure out what converter is used for given data payload and why. |
|
212 | + 'debug_enabled' => env('RB_CONVERTER_DEBUG', false), |
|
213 | + ], |
|
214 | + ], |
|
215 | 215 | |
216 | 216 | ]; |