@@ -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}", RB::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}", RB::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,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(object $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(object $obj, array $config): array; |
|
28 | 28 | } |
@@ -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(object $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(object $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(object $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(object $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 | } |
@@ -18,60 +18,60 @@ |
||
18 | 18 | */ |
19 | 19 | final class Util |
20 | 20 | { |
21 | - /** |
|
22 | - * Merges the configs together and takes multi-dimensional arrays into account. |
|
23 | - * Support for multi-dimensional config array. Built-in config merge only supports flat arrays. |
|
24 | - * Throws \RuntimeException if arrays stucture causes type conflics (i.e. you want to merge |
|
25 | - * array with int). |
|
26 | - * |
|
27 | - * @param array $original Array to merge other array into. Usually default values to overwrite. |
|
28 | - * @param array $merging Array with items to be merged into $original, overriding (primitives) or merging |
|
29 | - * (arrays) entries in destination array. |
|
30 | - * |
|
31 | - * @return array |
|
32 | - * |
|
33 | - * @throws \RuntimeException |
|
34 | - */ |
|
35 | - public static function mergeConfig(array $original, array $merging): array |
|
36 | - { |
|
37 | - $array = $original; |
|
38 | - foreach ($merging as $m_key => $m_val) { |
|
39 | - if (\array_key_exists($m_key, $original)) { |
|
40 | - $orig_type = \gettype($original[ $m_key ]); |
|
41 | - $m_type = \gettype($m_val); |
|
42 | - if ($orig_type !== $m_type) { |
|
43 | - throw new Ex\IncompatibleTypeException( |
|
44 | - "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
|
45 | - } |
|
21 | + /** |
|
22 | + * Merges the configs together and takes multi-dimensional arrays into account. |
|
23 | + * Support for multi-dimensional config array. Built-in config merge only supports flat arrays. |
|
24 | + * Throws \RuntimeException if arrays stucture causes type conflics (i.e. you want to merge |
|
25 | + * array with int). |
|
26 | + * |
|
27 | + * @param array $original Array to merge other array into. Usually default values to overwrite. |
|
28 | + * @param array $merging Array with items to be merged into $original, overriding (primitives) or merging |
|
29 | + * (arrays) entries in destination array. |
|
30 | + * |
|
31 | + * @return array |
|
32 | + * |
|
33 | + * @throws \RuntimeException |
|
34 | + */ |
|
35 | + public static function mergeConfig(array $original, array $merging): array |
|
36 | + { |
|
37 | + $array = $original; |
|
38 | + foreach ($merging as $m_key => $m_val) { |
|
39 | + if (\array_key_exists($m_key, $original)) { |
|
40 | + $orig_type = \gettype($original[ $m_key ]); |
|
41 | + $m_type = \gettype($m_val); |
|
42 | + if ($orig_type !== $m_type) { |
|
43 | + throw new Ex\IncompatibleTypeException( |
|
44 | + "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
|
45 | + } |
|
46 | 46 | |
47 | - if (\is_array($merging[ $m_key ])) { |
|
48 | - $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
49 | - } else { |
|
50 | - $array[ $m_key ] = $m_val; |
|
51 | - } |
|
52 | - } else { |
|
53 | - $array[ $m_key ] = $m_val; |
|
54 | - } |
|
55 | - } |
|
47 | + if (\is_array($merging[ $m_key ])) { |
|
48 | + $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
49 | + } else { |
|
50 | + $array[ $m_key ] = $m_val; |
|
51 | + } |
|
52 | + } else { |
|
53 | + $array[ $m_key ] = $m_val; |
|
54 | + } |
|
55 | + } |
|
56 | 56 | |
57 | - return $array; |
|
58 | - } |
|
57 | + return $array; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Sorts array (in place) by value, assuming value is an array and contains `pri` key with integer |
|
62 | - * (positive/negative) value which is used for sorting higher -> lower priority. |
|
63 | - * |
|
64 | - * @param array &$array |
|
65 | - */ |
|
66 | - public static function sortArrayByPri(array &$array): void |
|
67 | - { |
|
68 | - uasort($array, static function(array $array_a, array $array_b) { |
|
69 | - $pri_a = $array_a['pri'] ?? 0; |
|
70 | - $pri_b = $array_b['pri'] ?? 0; |
|
60 | + /** |
|
61 | + * Sorts array (in place) by value, assuming value is an array and contains `pri` key with integer |
|
62 | + * (positive/negative) value which is used for sorting higher -> lower priority. |
|
63 | + * |
|
64 | + * @param array &$array |
|
65 | + */ |
|
66 | + public static function sortArrayByPri(array &$array): void |
|
67 | + { |
|
68 | + uasort($array, static function(array $array_a, array $array_b) { |
|
69 | + $pri_a = $array_a['pri'] ?? 0; |
|
70 | + $pri_b = $array_b['pri'] ?? 0; |
|
71 | 71 | |
72 | - return $pri_b <=> $pri_a; |
|
73 | - }); |
|
74 | - } |
|
72 | + return $pri_b <=> $pri_a; |
|
73 | + }); |
|
74 | + } |
|
75 | 75 | |
76 | 76 | /** |
77 | 77 | * Checks if given array uses custom (non numeric) keys. |
@@ -37,20 +37,20 @@ |
||
37 | 37 | $array = $original; |
38 | 38 | foreach ($merging as $m_key => $m_val) { |
39 | 39 | if (\array_key_exists($m_key, $original)) { |
40 | - $orig_type = \gettype($original[ $m_key ]); |
|
40 | + $orig_type = \gettype($original[$m_key]); |
|
41 | 41 | $m_type = \gettype($m_val); |
42 | 42 | if ($orig_type !== $m_type) { |
43 | 43 | throw new Ex\IncompatibleTypeException( |
44 | 44 | "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
45 | 45 | } |
46 | 46 | |
47 | - if (\is_array($merging[ $m_key ])) { |
|
48 | - $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
47 | + if (\is_array($merging[$m_key])) { |
|
48 | + $array[$m_key] = static::mergeConfig($original[$m_key], $m_val); |
|
49 | 49 | } else { |
50 | - $array[ $m_key ] = $m_val; |
|
50 | + $array[$m_key] = $m_val; |
|
51 | 51 | } |
52 | 52 | } else { |
53 | - $array[ $m_key ] = $m_val; |
|
53 | + $array[$m_key] = $m_val; |
|
54 | 54 | } |
55 | 55 | } |
56 | 56 |
@@ -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)) { |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | $result = null; |
62 | 62 | |
63 | 63 | $type = \gettype($data); |
64 | - $result = $this->primitives[ $type ] ?? null; |
|
64 | + $result = $this->primitives[$type] ?? null; |
|
65 | 65 | if (!\is_array($result) && !empty($result)) { |
66 | 66 | throw new Ex\InvalidConfigurationElementException( |
67 | 67 | sprintf('Invalid conversion mapping config for "%s" primitive.', $type)); |
@@ -97,13 +97,13 @@ discard block |
||
97 | 97 | $cls = \get_class($data); |
98 | 98 | if (\is_string($cls)) { |
99 | 99 | if (\array_key_exists($cls, $this->classes)) { |
100 | - $result = $this->classes[ $cls ]; |
|
100 | + $result = $this->classes[$cls]; |
|
101 | 101 | $debug_result = 'exact config match'; |
102 | 102 | } else { |
103 | 103 | // no exact match, then lets try with `instanceof` |
104 | 104 | foreach (\array_keys($this->classes) as $class_name) { |
105 | 105 | if ($data instanceof $class_name) { |
106 | - $result = $this->classes[ $class_name ]; |
|
106 | + $result = $this->classes[$class_name]; |
|
107 | 107 | $debug_result = "subclass of {$class_name}"; |
108 | 108 | break; |
109 | 109 | } |
@@ -149,8 +149,8 @@ discard block |
||
149 | 149 | |
150 | 150 | if ($result === null && \is_object($data)) { |
151 | 151 | $cfg = $this->getClassMappingConfigOrThrow($data); |
152 | - $worker = new $cfg[ RB::KEY_HANDLER ](); |
|
153 | - $result = [$cfg[ RB::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
152 | + $worker = new $cfg[RB::KEY_HANDLER](); |
|
153 | + $result = [$cfg[RB::KEY_KEY] => $worker->convert($data, $cfg)]; |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | if ($result === null && \is_array($data)) { |
@@ -158,12 +158,12 @@ discard block |
||
158 | 158 | |
159 | 159 | $result = $this->convertArray($data); |
160 | 160 | if (!Util::isArrayWithNonNumericKeys($data)) { |
161 | - $result = [$cfg[ RB::KEY_KEY ] => $result]; |
|
161 | + $result = [$cfg[RB::KEY_KEY] => $result]; |
|
162 | 162 | } |
163 | 163 | } |
164 | 164 | |
165 | 165 | if (\is_bool($data) || \is_float($data) || \is_int($data) || \is_string($data)) { |
166 | - $result = [$this->getPrimitiveMappingConfigOrThrow($data)[ RB::KEY_KEY ] => $data]; |
|
166 | + $result = [$this->getPrimitiveMappingConfigOrThrow($data)[RB::KEY_KEY] => $data]; |
|
167 | 167 | } |
168 | 168 | |
169 | 169 | return $result; |
@@ -183,12 +183,12 @@ discard block |
||
183 | 183 | |
184 | 184 | foreach ($data as $key => $val) { |
185 | 185 | if (\is_array($val)) { |
186 | - $data[ $key ] = $this->convertArray($val); |
|
186 | + $data[$key] = $this->convertArray($val); |
|
187 | 187 | } elseif (\is_object($val)) { |
188 | 188 | $cfg = $this->getClassMappingConfigOrThrow($val); |
189 | - $worker = new $cfg[ RB::KEY_HANDLER ](); |
|
189 | + $worker = new $cfg[RB::KEY_HANDLER](); |
|
190 | 190 | $converted_data = $worker->convert($val, $cfg); |
191 | - $data[ $key ] = $converted_data; |
|
191 | + $data[$key] = $converted_data; |
|
192 | 192 | } |
193 | 193 | } |
194 | 194 |