@@ -12,100 +12,100 @@ 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 | - ], |
|
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 | 58 | |
59 | - /* Object converters configuration for supported classes */ |
|
60 | - 'map' => [ |
|
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 | - ], |
|
59 | + /* Object converters configuration for supported classes */ |
|
60 | + 'map' => [ |
|
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 | 81 | |
82 | - /* |
|
82 | + /* |
|
83 | 83 | |----------------------------------------------------------------------------------------------------------- |
84 | 84 | | Generic converters should have lower pri to allow dedicated ones to kick in first when class matches |
85 | 85 | |----------------------------------------------------------------------------------------------------------- |
86 | 86 | */ |
87 | - \JsonSerializable::class => [ |
|
88 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class, |
|
89 | - 'key' => 'item', |
|
90 | - 'pri' => -10, |
|
91 | - ], |
|
92 | - \Illuminate\Contracts\Support\Arrayable::class => [ |
|
93 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class, |
|
94 | - 'key' => 'items', |
|
95 | - 'pri' => -10, |
|
96 | - ], |
|
97 | - ], |
|
87 | + \JsonSerializable::class => [ |
|
88 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class, |
|
89 | + 'key' => 'item', |
|
90 | + 'pri' => -10, |
|
91 | + ], |
|
92 | + \Illuminate\Contracts\Support\Arrayable::class => [ |
|
93 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class, |
|
94 | + 'key' => 'items', |
|
95 | + 'pri' => -10, |
|
96 | + ], |
|
97 | + ], |
|
98 | 98 | |
99 | - ], |
|
99 | + ], |
|
100 | 100 | |
101 | - /* |
|
101 | + /* |
|
102 | 102 | |----------------------------------------------------------------------------------------------------------- |
103 | 103 | | Exception handler error codes |
104 | 104 | |----------------------------------------------------------------------------------------------------------- |
105 | 105 | | |
106 | 106 | */ |
107 | - 'exception_handler' => [ |
|
108 | - /* |
|
107 | + 'exception_handler' => [ |
|
108 | + /* |
|
109 | 109 | * The following keys are supported for each handler specified. |
110 | 110 | * `handler` |
111 | 111 | * `pri` |
@@ -125,19 +125,19 @@ discard block |
||
125 | 125 | * message ($ex->getMessage()). |
126 | 126 | */ |
127 | 127 | |
128 | - \Illuminate\Validation\ValidationException::class => [ |
|
129 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class, |
|
130 | - 'pri' => -100, |
|
131 | - 'config' => [ |
|
128 | + \Illuminate\Validation\ValidationException::class => [ |
|
129 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class, |
|
130 | + 'pri' => -100, |
|
131 | + 'config' => [ |
|
132 | 132 | // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_VALIDATION_EXCEPTION, |
133 | 133 | // 'http_code' => HttpResponse::HTTP_UNPROCESSABLE_ENTITY, |
134 | - ], |
|
135 | - ], |
|
134 | + ], |
|
135 | + ], |
|
136 | 136 | |
137 | 137 | \Symfony\Component\HttpKernel\Exception\HttpException::class => [ |
138 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
139 | - 'pri' => -100, |
|
140 | - 'config' => [ |
|
138 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
139 | + 'pri' => -100, |
|
140 | + 'config' => [ |
|
141 | 141 | // HttpException::class => [ |
142 | 142 | // // used by unauthenticated() to obtain api and http code for the exception |
143 | 143 | // HttpResponse::HTTP_UNAUTHORIZED => [ |
@@ -153,47 +153,47 @@ discard block |
||
153 | 153 | // 'http_code' => HttpResponse::HTTP_BAD_REQUEST, |
154 | 154 | // ], |
155 | 155 | // ], |
156 | - ], |
|
156 | + ], |
|
157 | 157 | // // This is final exception handler. If ex is not dealt with yet this is its last stop. |
158 | - // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
|
158 | + // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
|
159 | 159 | |
160 | - 'default' => [ |
|
161 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
162 | - 'pri' => -127, |
|
163 | - 'config' => [ |
|
160 | + 'default' => [ |
|
161 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
162 | + 'pri' => -127, |
|
163 | + 'config' => [ |
|
164 | 164 | // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_UNHANDLED_EXCEPTION, |
165 | 165 | // 'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR, |
166 | - ], |
|
167 | - ], |
|
168 | - ], |
|
169 | - ], |
|
166 | + ], |
|
167 | + ], |
|
168 | + ], |
|
169 | + ], |
|
170 | 170 | |
171 | - /* |
|
171 | + /* |
|
172 | 172 | |----------------------------------------------------------------------------------------------------------- |
173 | 173 | | data-to-json encoding options |
174 | 174 | |----------------------------------------------------------------------------------------------------------- |
175 | 175 | | |
176 | 176 | */ |
177 | - 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
177 | + 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
178 | 178 | |
179 | - /* |
|
179 | + /* |
|
180 | 180 | |----------------------------------------------------------------------------------------------------------- |
181 | 181 | | Debug config |
182 | 182 | |----------------------------------------------------------------------------------------------------------- |
183 | 183 | | |
184 | 184 | */ |
185 | - 'debug' => [ |
|
186 | - 'debug_key' => 'debug', |
|
187 | - 'exception_handler' => [ |
|
188 | - 'trace_key' => 'trace', |
|
189 | - 'trace_enabled' => env('APP_DEBUG', false), |
|
190 | - ], |
|
185 | + 'debug' => [ |
|
186 | + 'debug_key' => 'debug', |
|
187 | + 'exception_handler' => [ |
|
188 | + 'trace_key' => 'trace', |
|
189 | + 'trace_enabled' => env('APP_DEBUG', false), |
|
190 | + ], |
|
191 | 191 | |
192 | - // Controls debugging features of payload converter class. |
|
193 | - 'converter' => [ |
|
194 | - // Set to true to figure out what converter is used for given data payload and why. |
|
195 | - 'debug_enabled' => env('RB_CONVERTER_DEBUG', false), |
|
196 | - ], |
|
197 | - ], |
|
192 | + // Controls debugging features of payload converter class. |
|
193 | + 'converter' => [ |
|
194 | + // Set to true to figure out what converter is used for given data payload and why. |
|
195 | + 'debug_enabled' => env('RB_CONVERTER_DEBUG', false), |
|
196 | + ], |
|
197 | + ], |
|
198 | 198 | |
199 | 199 | ]; |
@@ -15,186 +15,186 @@ |
||
15 | 15 | */ |
16 | 16 | class Validator |
17 | 17 | { |
18 | - /** @var string */ |
|
19 | - public const TYPE_STRING = 'string'; |
|
18 | + /** @var string */ |
|
19 | + public const TYPE_STRING = 'string'; |
|
20 | 20 | |
21 | - /** @var string */ |
|
22 | - public const TYPE_INTEGER = 'integer'; |
|
21 | + /** @var string */ |
|
22 | + public const TYPE_INTEGER = 'integer'; |
|
23 | 23 | |
24 | - /** @var string */ |
|
25 | - public const TYPE_BOOL = 'boolean'; |
|
24 | + /** @var string */ |
|
25 | + public const TYPE_BOOL = 'boolean'; |
|
26 | 26 | |
27 | - /** @var string */ |
|
28 | - public const TYPE_ARRAY = 'array'; |
|
27 | + /** @var string */ |
|
28 | + public const TYPE_ARRAY = 'array'; |
|
29 | 29 | |
30 | - /** @var string */ |
|
31 | - public const TYPE_OBJECT = 'object'; |
|
30 | + /** @var string */ |
|
31 | + public const TYPE_OBJECT = 'object'; |
|
32 | 32 | |
33 | 33 | /** @var string */ |
34 | 34 | public const TYPE_DOUBLE = 'double'; |
35 | 35 | |
36 | 36 | /** @var string */ |
37 | - public const TYPE_NULL = 'NULL'; |
|
38 | - |
|
39 | - /** |
|
40 | - * Checks if given $val is of type boolean |
|
41 | - * |
|
42 | - * @param string $key Name of the key to be used if exception is thrown. |
|
43 | - * @param mixed $var Variable to be asserted. |
|
44 | - * |
|
45 | - * @return void |
|
46 | - * |
|
47 | - * @throws \InvalidArgumentException |
|
48 | - */ |
|
49 | - public static function assertIsBool(string $key, $var): void |
|
50 | - { |
|
51 | - self::assertIsType($key, $var, [self::TYPE_BOOL]); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Checks if given $val is of type integer |
|
56 | - * |
|
57 | - * @param string $key Name of the key to be used if exception is thrown. |
|
58 | - * @param mixed $var Variable to be asserted. |
|
59 | - * |
|
60 | - * @return void |
|
61 | - * |
|
62 | - * @throws \InvalidArgumentException |
|
63 | - */ |
|
64 | - public static function assertIsInt(string $key, $var): void |
|
65 | - { |
|
66 | - self::assertIsType($key, $var, [self::TYPE_INTEGER]); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Checks if given $val is of type array |
|
71 | - * |
|
72 | - * @param string $key Name of the key to be used if exception is thrown. |
|
73 | - * @param mixed $var Variable to be asserted. |
|
74 | - * |
|
75 | - * @return void |
|
76 | - * |
|
77 | - * @throws \InvalidArgumentException |
|
78 | - */ |
|
79 | - public static function assertIsArray(string $key, $var): void |
|
80 | - { |
|
81 | - self::assertIsType($key, $var, [self::TYPE_ARRAY]); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Checks if given $val is an object |
|
86 | - * |
|
87 | - * @param string $key Name of the key to be used if exception is thrown. |
|
88 | - * @param mixed $var Variable to be asserted. |
|
89 | - * |
|
90 | - * @return void |
|
91 | - * |
|
92 | - * @throws \InvalidArgumentException |
|
93 | - */ |
|
94 | - public static function assertIsObject(string $key, $var): void |
|
95 | - { |
|
96 | - self::assertIsType($key, $var, [self::TYPE_OBJECT]); |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Checks if given $val is of type string |
|
101 | - * |
|
102 | - * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
103 | - * @param mixed $var Variable to be asserted. |
|
104 | - * |
|
105 | - * @return void |
|
106 | - * |
|
107 | - * @throws \InvalidArgumentException |
|
108 | - */ |
|
109 | - public static function assertIsString(string $name, $var): void |
|
110 | - { |
|
111 | - self::assertIsType($name, $var, [self::TYPE_STRING]); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
116 | - * @param mixed $var Variable to be asserted. |
|
117 | - * @param int $min Min allowed value (inclusive) |
|
118 | - * @param int $max Max allowed value (inclusive) |
|
119 | - * |
|
120 | - * @return void |
|
121 | - * |
|
122 | - * @throws \InvalidArgumentException |
|
123 | - * @throws \RuntimeException |
|
124 | - */ |
|
125 | - public static function assertIsIntRange(string $name, $var, int $min, int $max): void |
|
126 | - { |
|
127 | - self::assertIsInt($name, $var); |
|
128 | - |
|
129 | - if ($min > $max) { |
|
130 | - throw new \RuntimeException( |
|
131 | - \sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $name)); |
|
132 | - } |
|
133 | - |
|
134 | - if (($min > $var) || ($var > $max)) { |
|
135 | - throw new \InvalidArgumentException( |
|
136 | - \sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $name, $var, $min, $max)); |
|
137 | - } |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Checks if $item (of name $key) is of type that is include in $allowed_types. |
|
142 | - * |
|
143 | - * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
144 | - * @param mixed $var Variable to be asserted. |
|
145 | - * @param array $allowed_types Array of allowed types for $var, i.e. [Validator::TYPE_INTEGER] |
|
146 | - * |
|
147 | - * @return void |
|
148 | - * |
|
149 | - * @throws \InvalidArgumentException |
|
150 | - */ |
|
151 | - public static function assertIsType(string $name, $var, array $allowed_types): void |
|
152 | - { |
|
153 | - $type = \gettype($var); |
|
154 | - if (!\in_array($type, $allowed_types, true)) { |
|
155 | - throw new \InvalidArgumentException( |
|
156 | - \sprintf('"%s" must be one of allowed types: %s (%s given)', |
|
157 | - $name, implode(', ', $allowed_types), \gettype($var)) |
|
158 | - ); |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Ensures given $http_code is valid code for error response. |
|
164 | - * |
|
165 | - * @param int $http_code |
|
166 | - */ |
|
167 | - public static function assertErrorHttpCode(int $http_code): void |
|
168 | - { |
|
169 | - self::assertIsInt('http_code', $http_code); |
|
170 | - self::assertIsIntRange('http_code', $http_code, |
|
171 | - ResponseBuilder::ERROR_HTTP_CODE_MIN, ResponseBuilder::ERROR_HTTP_CODE_MAX); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Ensures given $http_code is valid for response indicating sucessful operation. |
|
176 | - * |
|
177 | - * @param int $http_code |
|
178 | - */ |
|
179 | - public static function assertOkHttpCode(int $http_code): void |
|
180 | - { |
|
181 | - self::assertIsInt('http_code', $http_code); |
|
182 | - self::assertIsIntRange('http_code', $http_code, 200, 299); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Ensures $obj is instance of $cls. |
|
187 | - * |
|
188 | - * @param string $name |
|
189 | - * @param object $obj |
|
190 | - * @param string $cls |
|
191 | - */ |
|
192 | - public static function assertInstanceOf(string $name, object $obj, string $cls): void |
|
193 | - { |
|
194 | - if (!($obj instanceof $cls)) { |
|
195 | - throw new \InvalidArgumentException( |
|
196 | - \sprintf('"%s" must be instance of "%s".', $name, $cls) |
|
197 | - ); |
|
198 | - } |
|
199 | - } |
|
37 | + public const TYPE_NULL = 'NULL'; |
|
38 | + |
|
39 | + /** |
|
40 | + * Checks if given $val is of type boolean |
|
41 | + * |
|
42 | + * @param string $key Name of the key to be used if exception is thrown. |
|
43 | + * @param mixed $var Variable to be asserted. |
|
44 | + * |
|
45 | + * @return void |
|
46 | + * |
|
47 | + * @throws \InvalidArgumentException |
|
48 | + */ |
|
49 | + public static function assertIsBool(string $key, $var): void |
|
50 | + { |
|
51 | + self::assertIsType($key, $var, [self::TYPE_BOOL]); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Checks if given $val is of type integer |
|
56 | + * |
|
57 | + * @param string $key Name of the key to be used if exception is thrown. |
|
58 | + * @param mixed $var Variable to be asserted. |
|
59 | + * |
|
60 | + * @return void |
|
61 | + * |
|
62 | + * @throws \InvalidArgumentException |
|
63 | + */ |
|
64 | + public static function assertIsInt(string $key, $var): void |
|
65 | + { |
|
66 | + self::assertIsType($key, $var, [self::TYPE_INTEGER]); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Checks if given $val is of type array |
|
71 | + * |
|
72 | + * @param string $key Name of the key to be used if exception is thrown. |
|
73 | + * @param mixed $var Variable to be asserted. |
|
74 | + * |
|
75 | + * @return void |
|
76 | + * |
|
77 | + * @throws \InvalidArgumentException |
|
78 | + */ |
|
79 | + public static function assertIsArray(string $key, $var): void |
|
80 | + { |
|
81 | + self::assertIsType($key, $var, [self::TYPE_ARRAY]); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Checks if given $val is an object |
|
86 | + * |
|
87 | + * @param string $key Name of the key to be used if exception is thrown. |
|
88 | + * @param mixed $var Variable to be asserted. |
|
89 | + * |
|
90 | + * @return void |
|
91 | + * |
|
92 | + * @throws \InvalidArgumentException |
|
93 | + */ |
|
94 | + public static function assertIsObject(string $key, $var): void |
|
95 | + { |
|
96 | + self::assertIsType($key, $var, [self::TYPE_OBJECT]); |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Checks if given $val is of type string |
|
101 | + * |
|
102 | + * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
103 | + * @param mixed $var Variable to be asserted. |
|
104 | + * |
|
105 | + * @return void |
|
106 | + * |
|
107 | + * @throws \InvalidArgumentException |
|
108 | + */ |
|
109 | + public static function assertIsString(string $name, $var): void |
|
110 | + { |
|
111 | + self::assertIsType($name, $var, [self::TYPE_STRING]); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
116 | + * @param mixed $var Variable to be asserted. |
|
117 | + * @param int $min Min allowed value (inclusive) |
|
118 | + * @param int $max Max allowed value (inclusive) |
|
119 | + * |
|
120 | + * @return void |
|
121 | + * |
|
122 | + * @throws \InvalidArgumentException |
|
123 | + * @throws \RuntimeException |
|
124 | + */ |
|
125 | + public static function assertIsIntRange(string $name, $var, int $min, int $max): void |
|
126 | + { |
|
127 | + self::assertIsInt($name, $var); |
|
128 | + |
|
129 | + if ($min > $max) { |
|
130 | + throw new \RuntimeException( |
|
131 | + \sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $name)); |
|
132 | + } |
|
133 | + |
|
134 | + if (($min > $var) || ($var > $max)) { |
|
135 | + throw new \InvalidArgumentException( |
|
136 | + \sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $name, $var, $min, $max)); |
|
137 | + } |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Checks if $item (of name $key) is of type that is include in $allowed_types. |
|
142 | + * |
|
143 | + * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
144 | + * @param mixed $var Variable to be asserted. |
|
145 | + * @param array $allowed_types Array of allowed types for $var, i.e. [Validator::TYPE_INTEGER] |
|
146 | + * |
|
147 | + * @return void |
|
148 | + * |
|
149 | + * @throws \InvalidArgumentException |
|
150 | + */ |
|
151 | + public static function assertIsType(string $name, $var, array $allowed_types): void |
|
152 | + { |
|
153 | + $type = \gettype($var); |
|
154 | + if (!\in_array($type, $allowed_types, true)) { |
|
155 | + throw new \InvalidArgumentException( |
|
156 | + \sprintf('"%s" must be one of allowed types: %s (%s given)', |
|
157 | + $name, implode(', ', $allowed_types), \gettype($var)) |
|
158 | + ); |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Ensures given $http_code is valid code for error response. |
|
164 | + * |
|
165 | + * @param int $http_code |
|
166 | + */ |
|
167 | + public static function assertErrorHttpCode(int $http_code): void |
|
168 | + { |
|
169 | + self::assertIsInt('http_code', $http_code); |
|
170 | + self::assertIsIntRange('http_code', $http_code, |
|
171 | + ResponseBuilder::ERROR_HTTP_CODE_MIN, ResponseBuilder::ERROR_HTTP_CODE_MAX); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Ensures given $http_code is valid for response indicating sucessful operation. |
|
176 | + * |
|
177 | + * @param int $http_code |
|
178 | + */ |
|
179 | + public static function assertOkHttpCode(int $http_code): void |
|
180 | + { |
|
181 | + self::assertIsInt('http_code', $http_code); |
|
182 | + self::assertIsIntRange('http_code', $http_code, 200, 299); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Ensures $obj is instance of $cls. |
|
187 | + * |
|
188 | + * @param string $name |
|
189 | + * @param object $obj |
|
190 | + * @param string $cls |
|
191 | + */ |
|
192 | + public static function assertInstanceOf(string $name, object $obj, string $cls): void |
|
193 | + { |
|
194 | + if (!($obj instanceof $cls)) { |
|
195 | + throw new \InvalidArgumentException( |
|
196 | + \sprintf('"%s" must be instance of "%s".', $name, $cls) |
|
197 | + ); |
|
198 | + } |
|
199 | + } |
|
200 | 200 | } |
@@ -374,7 +374,7 @@ |
||
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; |
@@ -23,37 +23,37 @@ discard block |
||
23 | 23 | */ |
24 | 24 | class Converter |
25 | 25 | { |
26 | - /** @var array */ |
|
27 | - protected $classes = []; |
|
28 | - |
|
29 | - /** @var array */ |
|
30 | - protected $primitives = []; |
|
31 | - |
|
32 | - /** @var bool */ |
|
33 | - protected $debug_enabled = false; |
|
34 | - |
|
35 | - /** |
|
36 | - * Converter constructor. |
|
37 | - * |
|
38 | - * @throws \RuntimeException |
|
39 | - */ |
|
40 | - public function __construct() |
|
41 | - { |
|
42 | - $this->classes = static::getClassesMapping() ?? []; |
|
43 | - $this->primitives = static::getPrimitivesMapping() ?? []; |
|
44 | - |
|
45 | - $this->debug_enabled = Config::get(ResponseBuilder::CONF_KEY_CONVERTER_DEBUG_KEY, false); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Returns local copy of configuration mapping for data classes. |
|
50 | - * |
|
51 | - * @return array |
|
52 | - */ |
|
53 | - public function getClasses(): array |
|
54 | - { |
|
55 | - return $this->classes; |
|
56 | - } |
|
26 | + /** @var array */ |
|
27 | + protected $classes = []; |
|
28 | + |
|
29 | + /** @var array */ |
|
30 | + protected $primitives = []; |
|
31 | + |
|
32 | + /** @var bool */ |
|
33 | + protected $debug_enabled = false; |
|
34 | + |
|
35 | + /** |
|
36 | + * Converter constructor. |
|
37 | + * |
|
38 | + * @throws \RuntimeException |
|
39 | + */ |
|
40 | + public function __construct() |
|
41 | + { |
|
42 | + $this->classes = static::getClassesMapping() ?? []; |
|
43 | + $this->primitives = static::getPrimitivesMapping() ?? []; |
|
44 | + |
|
45 | + $this->debug_enabled = Config::get(ResponseBuilder::CONF_KEY_CONVERTER_DEBUG_KEY, false); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Returns local copy of configuration mapping for data classes. |
|
50 | + * |
|
51 | + * @return array |
|
52 | + */ |
|
53 | + public function getClasses(): array |
|
54 | + { |
|
55 | + return $this->classes; |
|
56 | + } |
|
57 | 57 | |
58 | 58 | /** |
59 | 59 | * Returns local copy of configuration mapping for primitives. |
@@ -61,9 +61,9 @@ discard block |
||
61 | 61 | * @return array |
62 | 62 | */ |
63 | 63 | public function getPrimitives(): array |
64 | - { |
|
65 | - return $this->primitives; |
|
66 | - } |
|
64 | + { |
|
65 | + return $this->primitives; |
|
66 | + } |
|
67 | 67 | |
68 | 68 | /** |
69 | 69 | * Returns "converter/primitives" entry for given primitive object or throws exception if no config found. |
@@ -76,67 +76,67 @@ discard block |
||
76 | 76 | * |
77 | 77 | * @throws \InvalidArgumentException |
78 | 78 | */ |
79 | - protected function getPrimitiveMappingConfigOrThrow($data): array |
|
80 | - { |
|
81 | - $result = null; |
|
82 | - |
|
83 | - $type = \gettype($data); |
|
84 | - $result = $this->primitives[ $type ] ?? null; |
|
85 | - if ($result === null) { |
|
86 | - throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" primitive.', $type)); |
|
87 | - } |
|
88 | - |
|
89 | - if ($this->debug_enabled) { |
|
90 | - Log::debug(__CLASS__ . ": Converting primitive type of '{$type}' to data node '{$result[ResponseBuilder::KEY_KEY]}'."); |
|
91 | - } |
|
92 | - |
|
93 | - return $result; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Returns "converter/map" mapping configured for given $data object class or throws exception if not found. |
|
98 | - * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
99 | - * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
100 | - * |
|
101 | - * @param object $data Object to get config for. |
|
102 | - * |
|
103 | - * @return array |
|
104 | - * |
|
105 | - * @throws \InvalidArgumentException |
|
106 | - */ |
|
107 | - protected function getClassMappingConfigOrThrow(object $data): array |
|
108 | - { |
|
109 | - $result = null; |
|
110 | - $debug_result = ''; |
|
111 | - |
|
112 | - // check for exact class name match... |
|
113 | - $cls = \get_class($data); |
|
114 | - if (\is_string($cls)) { |
|
115 | - if (\array_key_exists($cls, $this->classes)) { |
|
116 | - $result = $this->classes[ $cls ]; |
|
117 | - $debug_result = 'exact config match'; |
|
118 | - } else { |
|
119 | - // no exact match, then lets try with `instanceof` |
|
120 | - foreach (\array_keys($this->getClasses()) as $class_name) { |
|
121 | - if ($data instanceof $class_name) { |
|
122 | - $result = $this->classes[ $class_name ]; |
|
123 | - $debug_result = "subclass of {$class_name}"; |
|
124 | - break; |
|
125 | - } |
|
126 | - } |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - if ($result === null) { |
|
131 | - throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
132 | - } |
|
133 | - |
|
134 | - if ($this->debug_enabled) { |
|
79 | + protected function getPrimitiveMappingConfigOrThrow($data): array |
|
80 | + { |
|
81 | + $result = null; |
|
82 | + |
|
83 | + $type = \gettype($data); |
|
84 | + $result = $this->primitives[ $type ] ?? null; |
|
85 | + if ($result === null) { |
|
86 | + throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" primitive.', $type)); |
|
87 | + } |
|
88 | + |
|
89 | + if ($this->debug_enabled) { |
|
90 | + Log::debug(__CLASS__ . ": Converting primitive type of '{$type}' to data node '{$result[ResponseBuilder::KEY_KEY]}'."); |
|
91 | + } |
|
92 | + |
|
93 | + return $result; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Returns "converter/map" mapping configured for given $data object class or throws exception if not found. |
|
98 | + * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
99 | + * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
100 | + * |
|
101 | + * @param object $data Object to get config for. |
|
102 | + * |
|
103 | + * @return array |
|
104 | + * |
|
105 | + * @throws \InvalidArgumentException |
|
106 | + */ |
|
107 | + protected function getClassMappingConfigOrThrow(object $data): array |
|
108 | + { |
|
109 | + $result = null; |
|
110 | + $debug_result = ''; |
|
111 | + |
|
112 | + // check for exact class name match... |
|
113 | + $cls = \get_class($data); |
|
114 | + if (\is_string($cls)) { |
|
115 | + if (\array_key_exists($cls, $this->classes)) { |
|
116 | + $result = $this->classes[ $cls ]; |
|
117 | + $debug_result = 'exact config match'; |
|
118 | + } else { |
|
119 | + // no exact match, then lets try with `instanceof` |
|
120 | + foreach (\array_keys($this->getClasses()) as $class_name) { |
|
121 | + if ($data instanceof $class_name) { |
|
122 | + $result = $this->classes[ $class_name ]; |
|
123 | + $debug_result = "subclass of {$class_name}"; |
|
124 | + break; |
|
125 | + } |
|
126 | + } |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + if ($result === null) { |
|
131 | + throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
132 | + } |
|
133 | + |
|
134 | + if ($this->debug_enabled) { |
|
135 | 135 | Log::debug(__CLASS__ . ": Converting {$cls} using {$result[ResponseBuilder::KEY_HANDLER]} because: {$debug_result}."); |
136 | - } |
|
136 | + } |
|
137 | 137 | |
138 | - return $result; |
|
139 | - } |
|
138 | + return $result; |
|
139 | + } |
|
140 | 140 | |
141 | 141 | /** |
142 | 142 | * Checks if we have "classes" mapping configured given class name. |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | } |
191 | 191 | |
192 | 192 | Validator::assertIsType('data', $whole_payload, [Validator::TYPE_ARRAY, |
193 | - Validator::TYPE_OBJECT]); |
|
193 | + Validator::TYPE_OBJECT]); |
|
194 | 194 | |
195 | 195 | $key = null; |
196 | 196 | if (\is_object($data)) { |
@@ -204,145 +204,145 @@ discard block |
||
204 | 204 | } else { |
205 | 205 | return $data; |
206 | 206 | } |
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Main entry for data conversion |
|
211 | - * |
|
212 | - * @param object|array|null $data |
|
213 | - * |
|
214 | - * @return mixed|null |
|
215 | - * |
|
216 | - * @throws \InvalidArgumentException |
|
217 | - */ |
|
218 | - public function convert($data = null): ?array |
|
219 | - { |
|
220 | - if ($data === null) { |
|
221 | - return null; |
|
222 | - } |
|
223 | - |
|
224 | - $result = null; |
|
225 | - |
|
226 | - Validator::assertIsType('data', $data, [ |
|
227 | - Validator::TYPE_ARRAY, |
|
228 | - Validator::TYPE_BOOL, |
|
229 | - Validator::TYPE_DOUBLE, |
|
230 | - Validator::TYPE_INTEGER, |
|
231 | - Validator::TYPE_STRING, |
|
232 | - Validator::TYPE_OBJECT, |
|
233 | - ]); |
|
234 | - |
|
235 | - if ($result === null && \is_object($data)) { |
|
236 | - $cfg = $this->getClassMappingConfigOrThrow($data); |
|
237 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
238 | - $result = [$cfg[ ResponseBuilder::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
239 | - } |
|
240 | - |
|
241 | - if ($result === null && \is_array($data)) { |
|
242 | - $cfg = $this->getPrimitiveMappingConfigOrThrow($data); |
|
243 | - |
|
244 | - if ($this->hasNonNumericKeys($data)){ |
|
245 | - $result = $this->convertArray($data); |
|
246 | - } else { |
|
247 | - $result = [$cfg[ ResponseBuilder::KEY_KEY ] => $this->convertArray($data)]; |
|
248 | - } |
|
249 | - } |
|
250 | - |
|
251 | - if ( \is_bool($data) || \is_float($data) || \is_int($data) || \is_string($data)) { |
|
252 | - $result = [$this->getPrimitiveMappingConfigOrThrow($data)[ ResponseBuilder::KEY_KEY ] => $data]; |
|
253 | - } |
|
254 | - |
|
255 | - return $result; |
|
256 | - } |
|
257 | - |
|
258 | - protected function hasNonNumericKeys(array $data): bool |
|
259 | - { |
|
260 | - foreach ($data as $key => $val) { |
|
261 | - if (!\is_int($key)) { |
|
262 | - return true; |
|
263 | - } |
|
264 | - } |
|
265 | - |
|
266 | - return false; |
|
267 | - } |
|
268 | - |
|
269 | - /** |
|
270 | - * Recursively walks $data array and converts all known objects if found. Note |
|
271 | - * $data array is passed by reference so source $data array may be modified. |
|
272 | - * |
|
273 | - * @param array $data array to recursively convert known elements of |
|
274 | - * |
|
275 | - * @return array |
|
276 | - * |
|
277 | - * @throws \RuntimeException |
|
278 | - */ |
|
279 | - protected function convertArray(array $data): array |
|
280 | - { |
|
281 | - // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
282 | - // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
283 | - // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
284 | - $string_keys_cnt = 0; |
|
285 | - $int_keys_cnt = 0; |
|
286 | - foreach ($data as $key => $val) { |
|
287 | - if (\is_int($key)) { |
|
288 | - $int_keys_cnt++; |
|
289 | - } else { |
|
290 | - $string_keys_cnt++; |
|
291 | - } |
|
292 | - |
|
293 | - if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
294 | - throw new \RuntimeException( |
|
295 | - 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
296 | - 'Arrays with mixed keys are not supported by design.'); |
|
297 | - } |
|
298 | - } |
|
299 | - |
|
300 | - foreach ($data as $key => $val) { |
|
301 | - if (\is_array($val)) { |
|
302 | - $data[ $key ] = $this->convertArray($val); |
|
303 | - } elseif (\is_object($val)) { |
|
304 | - $cfg = $this->getClassMappingConfigOrThrow($val); |
|
305 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
306 | - $converted_data = $worker->convert($val, $cfg); |
|
307 | - $data[ $key ] = $converted_data; |
|
308 | - } |
|
309 | - } |
|
310 | - |
|
311 | - return $data; |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Reads and validates "converter/map" config mapping |
|
316 | - * |
|
317 | - * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
318 | - * |
|
319 | - * @throws \RuntimeException if config mapping is technically invalid (i.e. not array etc). |
|
320 | - */ |
|
321 | - protected static function getClassesMapping(): array |
|
322 | - { |
|
323 | - $classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER_MAP) ?? []; |
|
324 | - |
|
325 | - if (!\is_array($classes)) { |
|
326 | - throw new \RuntimeException( |
|
327 | - \sprintf('CONFIG: "%s" mapping must be an array (%s given)', ResponseBuilder::CONF_KEY_CONVERTER_MAP, \gettype($classes))); |
|
328 | - } |
|
329 | - |
|
330 | - if (!empty($classes)) { |
|
331 | - $mandatory_keys = [ |
|
332 | - ResponseBuilder::KEY_HANDLER, |
|
333 | - ResponseBuilder::KEY_KEY, |
|
334 | - ]; |
|
335 | - foreach ($classes as $class_name => $class_config) { |
|
336 | - foreach ($mandatory_keys as $key_name) { |
|
337 | - if (!\array_key_exists($key_name, $class_config)) { |
|
338 | - throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
339 | - } |
|
340 | - } |
|
341 | - } |
|
342 | - } |
|
343 | - |
|
344 | - return $classes; |
|
345 | - } |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Main entry for data conversion |
|
211 | + * |
|
212 | + * @param object|array|null $data |
|
213 | + * |
|
214 | + * @return mixed|null |
|
215 | + * |
|
216 | + * @throws \InvalidArgumentException |
|
217 | + */ |
|
218 | + public function convert($data = null): ?array |
|
219 | + { |
|
220 | + if ($data === null) { |
|
221 | + return null; |
|
222 | + } |
|
223 | + |
|
224 | + $result = null; |
|
225 | + |
|
226 | + Validator::assertIsType('data', $data, [ |
|
227 | + Validator::TYPE_ARRAY, |
|
228 | + Validator::TYPE_BOOL, |
|
229 | + Validator::TYPE_DOUBLE, |
|
230 | + Validator::TYPE_INTEGER, |
|
231 | + Validator::TYPE_STRING, |
|
232 | + Validator::TYPE_OBJECT, |
|
233 | + ]); |
|
234 | + |
|
235 | + if ($result === null && \is_object($data)) { |
|
236 | + $cfg = $this->getClassMappingConfigOrThrow($data); |
|
237 | + $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
238 | + $result = [$cfg[ ResponseBuilder::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
239 | + } |
|
240 | + |
|
241 | + if ($result === null && \is_array($data)) { |
|
242 | + $cfg = $this->getPrimitiveMappingConfigOrThrow($data); |
|
243 | + |
|
244 | + if ($this->hasNonNumericKeys($data)){ |
|
245 | + $result = $this->convertArray($data); |
|
246 | + } else { |
|
247 | + $result = [$cfg[ ResponseBuilder::KEY_KEY ] => $this->convertArray($data)]; |
|
248 | + } |
|
249 | + } |
|
250 | + |
|
251 | + if ( \is_bool($data) || \is_float($data) || \is_int($data) || \is_string($data)) { |
|
252 | + $result = [$this->getPrimitiveMappingConfigOrThrow($data)[ ResponseBuilder::KEY_KEY ] => $data]; |
|
253 | + } |
|
254 | + |
|
255 | + return $result; |
|
256 | + } |
|
257 | + |
|
258 | + protected function hasNonNumericKeys(array $data): bool |
|
259 | + { |
|
260 | + foreach ($data as $key => $val) { |
|
261 | + if (!\is_int($key)) { |
|
262 | + return true; |
|
263 | + } |
|
264 | + } |
|
265 | + |
|
266 | + return false; |
|
267 | + } |
|
268 | + |
|
269 | + /** |
|
270 | + * Recursively walks $data array and converts all known objects if found. Note |
|
271 | + * $data array is passed by reference so source $data array may be modified. |
|
272 | + * |
|
273 | + * @param array $data array to recursively convert known elements of |
|
274 | + * |
|
275 | + * @return array |
|
276 | + * |
|
277 | + * @throws \RuntimeException |
|
278 | + */ |
|
279 | + protected function convertArray(array $data): array |
|
280 | + { |
|
281 | + // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
282 | + // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
283 | + // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
284 | + $string_keys_cnt = 0; |
|
285 | + $int_keys_cnt = 0; |
|
286 | + foreach ($data as $key => $val) { |
|
287 | + if (\is_int($key)) { |
|
288 | + $int_keys_cnt++; |
|
289 | + } else { |
|
290 | + $string_keys_cnt++; |
|
291 | + } |
|
292 | + |
|
293 | + if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
294 | + throw new \RuntimeException( |
|
295 | + 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
296 | + 'Arrays with mixed keys are not supported by design.'); |
|
297 | + } |
|
298 | + } |
|
299 | + |
|
300 | + foreach ($data as $key => $val) { |
|
301 | + if (\is_array($val)) { |
|
302 | + $data[ $key ] = $this->convertArray($val); |
|
303 | + } elseif (\is_object($val)) { |
|
304 | + $cfg = $this->getClassMappingConfigOrThrow($val); |
|
305 | + $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
306 | + $converted_data = $worker->convert($val, $cfg); |
|
307 | + $data[ $key ] = $converted_data; |
|
308 | + } |
|
309 | + } |
|
310 | + |
|
311 | + return $data; |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Reads and validates "converter/map" config mapping |
|
316 | + * |
|
317 | + * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
318 | + * |
|
319 | + * @throws \RuntimeException if config mapping is technically invalid (i.e. not array etc). |
|
320 | + */ |
|
321 | + protected static function getClassesMapping(): array |
|
322 | + { |
|
323 | + $classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER_MAP) ?? []; |
|
324 | + |
|
325 | + if (!\is_array($classes)) { |
|
326 | + throw new \RuntimeException( |
|
327 | + \sprintf('CONFIG: "%s" mapping must be an array (%s given)', ResponseBuilder::CONF_KEY_CONVERTER_MAP, \gettype($classes))); |
|
328 | + } |
|
329 | + |
|
330 | + if (!empty($classes)) { |
|
331 | + $mandatory_keys = [ |
|
332 | + ResponseBuilder::KEY_HANDLER, |
|
333 | + ResponseBuilder::KEY_KEY, |
|
334 | + ]; |
|
335 | + foreach ($classes as $class_name => $class_config) { |
|
336 | + foreach ($mandatory_keys as $key_name) { |
|
337 | + if (!\array_key_exists($key_name, $class_config)) { |
|
338 | + throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
339 | + } |
|
340 | + } |
|
341 | + } |
|
342 | + } |
|
343 | + |
|
344 | + return $classes; |
|
345 | + } |
|
346 | 346 | |
347 | 347 | /** |
348 | 348 | * Reads and validates "converter/primitives" config mapping |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | $result = null; |
82 | 82 | |
83 | 83 | $type = \gettype($data); |
84 | - $result = $this->primitives[ $type ] ?? null; |
|
84 | + $result = $this->primitives[$type] ?? null; |
|
85 | 85 | if ($result === null) { |
86 | 86 | throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" primitive.', $type)); |
87 | 87 | } |
@@ -113,13 +113,13 @@ discard block |
||
113 | 113 | $cls = \get_class($data); |
114 | 114 | if (\is_string($cls)) { |
115 | 115 | if (\array_key_exists($cls, $this->classes)) { |
116 | - $result = $this->classes[ $cls ]; |
|
116 | + $result = $this->classes[$cls]; |
|
117 | 117 | $debug_result = 'exact config match'; |
118 | 118 | } else { |
119 | 119 | // no exact match, then lets try with `instanceof` |
120 | 120 | foreach (\array_keys($this->getClasses()) as $class_name) { |
121 | 121 | if ($data instanceof $class_name) { |
122 | - $result = $this->classes[ $class_name ]; |
|
122 | + $result = $this->classes[$class_name]; |
|
123 | 123 | $debug_result = "subclass of {$class_name}"; |
124 | 124 | break; |
125 | 125 | } |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | |
158 | 158 | // check for exact class name match... |
159 | 159 | if (\array_key_exists($cls, $this->classes)) { |
160 | - $result = $this->classes[ $cls ]; |
|
160 | + $result = $this->classes[$cls]; |
|
161 | 161 | $debug_result = 'exact config match'; |
162 | 162 | } |
163 | 163 | |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | $key = null; |
196 | 196 | if (\is_object($data)) { |
197 | 197 | $cfg = $this->getClassMappingConfigOrThrow($data); |
198 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
198 | + $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
199 | 199 | |
200 | 200 | } |
201 | 201 | |
@@ -234,22 +234,22 @@ discard block |
||
234 | 234 | |
235 | 235 | if ($result === null && \is_object($data)) { |
236 | 236 | $cfg = $this->getClassMappingConfigOrThrow($data); |
237 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
238 | - $result = [$cfg[ ResponseBuilder::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
237 | + $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
238 | + $result = [$cfg[ResponseBuilder::KEY_KEY] => $worker->convert($data, $cfg)]; |
|
239 | 239 | } |
240 | 240 | |
241 | 241 | if ($result === null && \is_array($data)) { |
242 | 242 | $cfg = $this->getPrimitiveMappingConfigOrThrow($data); |
243 | 243 | |
244 | - if ($this->hasNonNumericKeys($data)){ |
|
244 | + if ($this->hasNonNumericKeys($data)) { |
|
245 | 245 | $result = $this->convertArray($data); |
246 | 246 | } else { |
247 | - $result = [$cfg[ ResponseBuilder::KEY_KEY ] => $this->convertArray($data)]; |
|
247 | + $result = [$cfg[ResponseBuilder::KEY_KEY] => $this->convertArray($data)]; |
|
248 | 248 | } |
249 | 249 | } |
250 | 250 | |
251 | - if ( \is_bool($data) || \is_float($data) || \is_int($data) || \is_string($data)) { |
|
252 | - $result = [$this->getPrimitiveMappingConfigOrThrow($data)[ ResponseBuilder::KEY_KEY ] => $data]; |
|
251 | + if (\is_bool($data) || \is_float($data) || \is_int($data) || \is_string($data)) { |
|
252 | + $result = [$this->getPrimitiveMappingConfigOrThrow($data)[ResponseBuilder::KEY_KEY] => $data]; |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | return $result; |
@@ -299,12 +299,12 @@ discard block |
||
299 | 299 | |
300 | 300 | foreach ($data as $key => $val) { |
301 | 301 | if (\is_array($val)) { |
302 | - $data[ $key ] = $this->convertArray($val); |
|
302 | + $data[$key] = $this->convertArray($val); |
|
303 | 303 | } elseif (\is_object($val)) { |
304 | 304 | $cfg = $this->getClassMappingConfigOrThrow($val); |
305 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
305 | + $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
306 | 306 | $converted_data = $worker->convert($val, $cfg); |
307 | - $data[ $key ] = $converted_data; |
|
307 | + $data[$key] = $converted_data; |
|
308 | 308 | } |
309 | 309 | } |
310 | 310 |