@@ -23,27 +23,27 @@ discard block |
||
23 | 23 | */ |
24 | 24 | class Converter |
25 | 25 | { |
26 | - /** @var array */ |
|
27 | - protected $classes = []; |
|
26 | + /** @var array */ |
|
27 | + protected $classes = []; |
|
28 | 28 | |
29 | - /** @var array */ |
|
30 | - protected $primitives = []; |
|
29 | + /** @var array */ |
|
30 | + protected $primitives = []; |
|
31 | 31 | |
32 | - /** @var bool */ |
|
33 | - protected $debug_enabled = false; |
|
32 | + /** @var bool */ |
|
33 | + protected $debug_enabled = false; |
|
34 | 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() ?? []; |
|
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 | 44 | |
45 | - $this->debug_enabled = Config::get(RB::CONF_KEY_DEBUG_CONVERTER_DEBUG_ENABLED, false); |
|
46 | - } |
|
45 | + $this->debug_enabled = Config::get(RB::CONF_KEY_DEBUG_CONVERTER_DEBUG_ENABLED, false); |
|
46 | + } |
|
47 | 47 | |
48 | 48 | /** |
49 | 49 | * Returns "converter/primitives" entry for given primitive object or throws exception if no config found. |
@@ -56,196 +56,196 @@ discard block |
||
56 | 56 | * |
57 | 57 | * @throws \InvalidArgumentException |
58 | 58 | */ |
59 | - protected function getPrimitiveMappingConfigOrThrow($data): array |
|
60 | - { |
|
61 | - $result = null; |
|
62 | - |
|
63 | - $type = \gettype($data); |
|
64 | - $result = $this->primitives[ $type ] ?? null; |
|
65 | - if ($result === null) { |
|
66 | - throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" primitive.', $type)); |
|
67 | - } |
|
68 | - |
|
69 | - if ($this->debug_enabled) { |
|
70 | - Log::debug(__CLASS__ . ": Converting primitive type of '{$type}' to data node '{$result[RB::KEY_KEY]}'."); |
|
71 | - } |
|
72 | - |
|
73 | - return $result; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Returns "converter/map" mapping configured for given $data object class or throws exception if not found. |
|
78 | - * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
79 | - * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
80 | - * |
|
81 | - * @param object $data Object to get config for. |
|
82 | - * |
|
83 | - * @return array |
|
84 | - * |
|
85 | - * @throws \InvalidArgumentException |
|
86 | - */ |
|
87 | - protected function getClassMappingConfigOrThrow(object $data): array |
|
88 | - { |
|
89 | - $result = null; |
|
90 | - $debug_result = ''; |
|
91 | - |
|
92 | - // check for exact class name match... |
|
93 | - $cls = \get_class($data); |
|
94 | - if (\is_string($cls)) { |
|
95 | - if (\array_key_exists($cls, $this->classes)) { |
|
96 | - $result = $this->classes[ $cls ]; |
|
97 | - $debug_result = 'exact config match'; |
|
98 | - } else { |
|
99 | - // no exact match, then lets try with `instanceof` |
|
100 | - foreach (\array_keys($this->classes) as $class_name) { |
|
101 | - if ($data instanceof $class_name) { |
|
102 | - $result = $this->classes[ $class_name ]; |
|
103 | - $debug_result = "subclass of {$class_name}"; |
|
104 | - break; |
|
105 | - } |
|
106 | - } |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - if ($result === null) { |
|
111 | - throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
112 | - } |
|
113 | - |
|
114 | - if ($this->debug_enabled) { |
|
59 | + protected function getPrimitiveMappingConfigOrThrow($data): array |
|
60 | + { |
|
61 | + $result = null; |
|
62 | + |
|
63 | + $type = \gettype($data); |
|
64 | + $result = $this->primitives[ $type ] ?? null; |
|
65 | + if ($result === null) { |
|
66 | + throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" primitive.', $type)); |
|
67 | + } |
|
68 | + |
|
69 | + if ($this->debug_enabled) { |
|
70 | + Log::debug(__CLASS__ . ": Converting primitive type of '{$type}' to data node '{$result[RB::KEY_KEY]}'."); |
|
71 | + } |
|
72 | + |
|
73 | + return $result; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Returns "converter/map" mapping configured for given $data object class or throws exception if not found. |
|
78 | + * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
79 | + * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
80 | + * |
|
81 | + * @param object $data Object to get config for. |
|
82 | + * |
|
83 | + * @return array |
|
84 | + * |
|
85 | + * @throws \InvalidArgumentException |
|
86 | + */ |
|
87 | + protected function getClassMappingConfigOrThrow(object $data): array |
|
88 | + { |
|
89 | + $result = null; |
|
90 | + $debug_result = ''; |
|
91 | + |
|
92 | + // check for exact class name match... |
|
93 | + $cls = \get_class($data); |
|
94 | + if (\is_string($cls)) { |
|
95 | + if (\array_key_exists($cls, $this->classes)) { |
|
96 | + $result = $this->classes[ $cls ]; |
|
97 | + $debug_result = 'exact config match'; |
|
98 | + } else { |
|
99 | + // no exact match, then lets try with `instanceof` |
|
100 | + foreach (\array_keys($this->classes) as $class_name) { |
|
101 | + if ($data instanceof $class_name) { |
|
102 | + $result = $this->classes[ $class_name ]; |
|
103 | + $debug_result = "subclass of {$class_name}"; |
|
104 | + break; |
|
105 | + } |
|
106 | + } |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + if ($result === null) { |
|
111 | + throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
112 | + } |
|
113 | + |
|
114 | + if ($this->debug_enabled) { |
|
115 | 115 | Log::debug(__CLASS__ . ": Converting {$cls} using {$result[RB::KEY_HANDLER]} because: {$debug_result}."); |
116 | - } |
|
117 | - |
|
118 | - return $result; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Main entry for data conversion |
|
123 | - * |
|
124 | - * @param object|array|null $data |
|
125 | - * |
|
126 | - * @return mixed|null |
|
127 | - * |
|
128 | - * @throws \InvalidArgumentException |
|
129 | - */ |
|
130 | - public function convert($data = null): ?array |
|
131 | - { |
|
132 | - if ($data === null) { |
|
133 | - return null; |
|
134 | - } |
|
135 | - |
|
136 | - $result = null; |
|
137 | - |
|
138 | - Validator::assertIsType('data', $data, [ |
|
139 | - Type::ARRAY, |
|
140 | - Type::BOOLEAN, |
|
141 | - Type::DOUBLE, |
|
142 | - Type::INTEGER, |
|
143 | - Type::STRING, |
|
144 | - Type::OBJECT, |
|
145 | - ]); |
|
146 | - |
|
147 | - if ($result === null && \is_object($data)) { |
|
148 | - $cfg = $this->getClassMappingConfigOrThrow($data); |
|
149 | - $worker = new $cfg[ RB::KEY_HANDLER ](); |
|
150 | - $result = [$cfg[ RB::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
151 | - } |
|
152 | - |
|
153 | - if ($result === null && \is_array($data)) { |
|
154 | - $cfg = $this->getPrimitiveMappingConfigOrThrow($data); |
|
155 | - |
|
156 | - $result = $this->convertArray($data); |
|
157 | - if (!Util::isArrayWithNonNumericKeys($data)){ |
|
158 | - $result = [$cfg[ RB::KEY_KEY ] => $result]; |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - if ( \is_bool($data) || \is_float($data) || \is_int($data) || \is_string($data)) { |
|
163 | - $result = [$this->getPrimitiveMappingConfigOrThrow($data)[ RB::KEY_KEY ] => $data]; |
|
164 | - } |
|
165 | - |
|
166 | - return $result; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Recursively walks $data array and converts all known objects if found. Note |
|
171 | - * $data array is passed by reference so source $data array may be modified. |
|
172 | - * |
|
173 | - * @param array $data array to recursively convert known elements of |
|
174 | - * |
|
175 | - * @return array |
|
176 | - * |
|
177 | - * @throws \RuntimeException |
|
178 | - */ |
|
179 | - protected function convertArray(array $data): array |
|
180 | - { |
|
181 | - // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
182 | - // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
183 | - // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
184 | - $string_keys_cnt = 0; |
|
185 | - $int_keys_cnt = 0; |
|
186 | - foreach ($data as $key => $val) { |
|
187 | - if (\is_int($key)) { |
|
188 | - $int_keys_cnt++; |
|
189 | - } else { |
|
190 | - $string_keys_cnt++; |
|
191 | - } |
|
192 | - |
|
193 | - if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
194 | - throw new \RuntimeException( |
|
195 | - 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
196 | - 'Arrays with mixed keys are not supported by design.'); |
|
197 | - } |
|
198 | - } |
|
199 | - |
|
200 | - foreach ($data as $key => $val) { |
|
201 | - if (\is_array($val)) { |
|
202 | - $data[ $key ] = $this->convertArray($val); |
|
203 | - } elseif (\is_object($val)) { |
|
204 | - $cfg = $this->getClassMappingConfigOrThrow($val); |
|
205 | - $worker = new $cfg[ RB::KEY_HANDLER ](); |
|
206 | - $converted_data = $worker->convert($val, $cfg); |
|
207 | - $data[ $key ] = $converted_data; |
|
208 | - } |
|
209 | - } |
|
210 | - |
|
211 | - return $data; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Reads and validates "converter/map" config mapping |
|
216 | - * |
|
217 | - * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
218 | - * |
|
219 | - * @throws \RuntimeException if config mapping is technically invalid (i.e. not array etc). |
|
220 | - */ |
|
221 | - protected static function getClassesMapping(): array |
|
222 | - { |
|
223 | - $classes = Config::get(RB::CONF_KEY_CONVERTER_CLASSES) ?? []; |
|
224 | - |
|
225 | - if (!\is_array($classes)) { |
|
226 | - throw new \RuntimeException( |
|
227 | - \sprintf('CONFIG: "%s" mapping must be an array (%s given)', RB::CONF_KEY_CONVERTER_CLASSES, \gettype($classes))); |
|
228 | - } |
|
229 | - |
|
230 | - if (!empty($classes)) { |
|
231 | - $mandatory_keys = [ |
|
232 | - RB::KEY_HANDLER, |
|
233 | - RB::KEY_KEY, |
|
234 | - ]; |
|
235 | - foreach ($classes as $class_name => $class_config) { |
|
236 | - if (!\is_array($class_config)) { |
|
237 | - throw new \InvalidArgumentException(sprintf("CONFIG: Config for '{$class_name}' class must be an array (%s given).", \gettype($class_config))); |
|
238 | - } |
|
239 | - foreach ($mandatory_keys as $key_name) { |
|
240 | - if (!\array_key_exists($key_name, $class_config)) { |
|
241 | - throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
242 | - } |
|
243 | - } |
|
244 | - } |
|
245 | - } |
|
246 | - |
|
247 | - return $classes; |
|
248 | - } |
|
116 | + } |
|
117 | + |
|
118 | + return $result; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Main entry for data conversion |
|
123 | + * |
|
124 | + * @param object|array|null $data |
|
125 | + * |
|
126 | + * @return mixed|null |
|
127 | + * |
|
128 | + * @throws \InvalidArgumentException |
|
129 | + */ |
|
130 | + public function convert($data = null): ?array |
|
131 | + { |
|
132 | + if ($data === null) { |
|
133 | + return null; |
|
134 | + } |
|
135 | + |
|
136 | + $result = null; |
|
137 | + |
|
138 | + Validator::assertIsType('data', $data, [ |
|
139 | + Type::ARRAY, |
|
140 | + Type::BOOLEAN, |
|
141 | + Type::DOUBLE, |
|
142 | + Type::INTEGER, |
|
143 | + Type::STRING, |
|
144 | + Type::OBJECT, |
|
145 | + ]); |
|
146 | + |
|
147 | + if ($result === null && \is_object($data)) { |
|
148 | + $cfg = $this->getClassMappingConfigOrThrow($data); |
|
149 | + $worker = new $cfg[ RB::KEY_HANDLER ](); |
|
150 | + $result = [$cfg[ RB::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
151 | + } |
|
152 | + |
|
153 | + if ($result === null && \is_array($data)) { |
|
154 | + $cfg = $this->getPrimitiveMappingConfigOrThrow($data); |
|
155 | + |
|
156 | + $result = $this->convertArray($data); |
|
157 | + if (!Util::isArrayWithNonNumericKeys($data)){ |
|
158 | + $result = [$cfg[ RB::KEY_KEY ] => $result]; |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + if ( \is_bool($data) || \is_float($data) || \is_int($data) || \is_string($data)) { |
|
163 | + $result = [$this->getPrimitiveMappingConfigOrThrow($data)[ RB::KEY_KEY ] => $data]; |
|
164 | + } |
|
165 | + |
|
166 | + return $result; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Recursively walks $data array and converts all known objects if found. Note |
|
171 | + * $data array is passed by reference so source $data array may be modified. |
|
172 | + * |
|
173 | + * @param array $data array to recursively convert known elements of |
|
174 | + * |
|
175 | + * @return array |
|
176 | + * |
|
177 | + * @throws \RuntimeException |
|
178 | + */ |
|
179 | + protected function convertArray(array $data): array |
|
180 | + { |
|
181 | + // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
182 | + // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
183 | + // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
184 | + $string_keys_cnt = 0; |
|
185 | + $int_keys_cnt = 0; |
|
186 | + foreach ($data as $key => $val) { |
|
187 | + if (\is_int($key)) { |
|
188 | + $int_keys_cnt++; |
|
189 | + } else { |
|
190 | + $string_keys_cnt++; |
|
191 | + } |
|
192 | + |
|
193 | + if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
194 | + throw new \RuntimeException( |
|
195 | + 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
196 | + 'Arrays with mixed keys are not supported by design.'); |
|
197 | + } |
|
198 | + } |
|
199 | + |
|
200 | + foreach ($data as $key => $val) { |
|
201 | + if (\is_array($val)) { |
|
202 | + $data[ $key ] = $this->convertArray($val); |
|
203 | + } elseif (\is_object($val)) { |
|
204 | + $cfg = $this->getClassMappingConfigOrThrow($val); |
|
205 | + $worker = new $cfg[ RB::KEY_HANDLER ](); |
|
206 | + $converted_data = $worker->convert($val, $cfg); |
|
207 | + $data[ $key ] = $converted_data; |
|
208 | + } |
|
209 | + } |
|
210 | + |
|
211 | + return $data; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Reads and validates "converter/map" config mapping |
|
216 | + * |
|
217 | + * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
218 | + * |
|
219 | + * @throws \RuntimeException if config mapping is technically invalid (i.e. not array etc). |
|
220 | + */ |
|
221 | + protected static function getClassesMapping(): array |
|
222 | + { |
|
223 | + $classes = Config::get(RB::CONF_KEY_CONVERTER_CLASSES) ?? []; |
|
224 | + |
|
225 | + if (!\is_array($classes)) { |
|
226 | + throw new \RuntimeException( |
|
227 | + \sprintf('CONFIG: "%s" mapping must be an array (%s given)', RB::CONF_KEY_CONVERTER_CLASSES, \gettype($classes))); |
|
228 | + } |
|
229 | + |
|
230 | + if (!empty($classes)) { |
|
231 | + $mandatory_keys = [ |
|
232 | + RB::KEY_HANDLER, |
|
233 | + RB::KEY_KEY, |
|
234 | + ]; |
|
235 | + foreach ($classes as $class_name => $class_config) { |
|
236 | + if (!\is_array($class_config)) { |
|
237 | + throw new \InvalidArgumentException(sprintf("CONFIG: Config for '{$class_name}' class must be an array (%s given).", \gettype($class_config))); |
|
238 | + } |
|
239 | + foreach ($mandatory_keys as $key_name) { |
|
240 | + if (!\array_key_exists($key_name, $class_config)) { |
|
241 | + throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
242 | + } |
|
243 | + } |
|
244 | + } |
|
245 | + } |
|
246 | + |
|
247 | + return $classes; |
|
248 | + } |
|
249 | 249 | |
250 | 250 | /** |
251 | 251 | * Reads and validates "converter/primitives" config mapping |
@@ -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 ($result === null) { |
66 | 66 | throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" primitive.', $type)); |
67 | 67 | } |
@@ -93,13 +93,13 @@ discard block |
||
93 | 93 | $cls = \get_class($data); |
94 | 94 | if (\is_string($cls)) { |
95 | 95 | if (\array_key_exists($cls, $this->classes)) { |
96 | - $result = $this->classes[ $cls ]; |
|
96 | + $result = $this->classes[$cls]; |
|
97 | 97 | $debug_result = 'exact config match'; |
98 | 98 | } else { |
99 | 99 | // no exact match, then lets try with `instanceof` |
100 | 100 | foreach (\array_keys($this->classes) as $class_name) { |
101 | 101 | if ($data instanceof $class_name) { |
102 | - $result = $this->classes[ $class_name ]; |
|
102 | + $result = $this->classes[$class_name]; |
|
103 | 103 | $debug_result = "subclass of {$class_name}"; |
104 | 104 | break; |
105 | 105 | } |
@@ -146,21 +146,21 @@ discard block |
||
146 | 146 | |
147 | 147 | if ($result === null && \is_object($data)) { |
148 | 148 | $cfg = $this->getClassMappingConfigOrThrow($data); |
149 | - $worker = new $cfg[ RB::KEY_HANDLER ](); |
|
150 | - $result = [$cfg[ RB::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
149 | + $worker = new $cfg[RB::KEY_HANDLER](); |
|
150 | + $result = [$cfg[RB::KEY_KEY] => $worker->convert($data, $cfg)]; |
|
151 | 151 | } |
152 | 152 | |
153 | 153 | if ($result === null && \is_array($data)) { |
154 | 154 | $cfg = $this->getPrimitiveMappingConfigOrThrow($data); |
155 | 155 | |
156 | 156 | $result = $this->convertArray($data); |
157 | - if (!Util::isArrayWithNonNumericKeys($data)){ |
|
158 | - $result = [$cfg[ RB::KEY_KEY ] => $result]; |
|
157 | + if (!Util::isArrayWithNonNumericKeys($data)) { |
|
158 | + $result = [$cfg[RB::KEY_KEY] => $result]; |
|
159 | 159 | } |
160 | 160 | } |
161 | 161 | |
162 | - if ( \is_bool($data) || \is_float($data) || \is_int($data) || \is_string($data)) { |
|
163 | - $result = [$this->getPrimitiveMappingConfigOrThrow($data)[ RB::KEY_KEY ] => $data]; |
|
162 | + if (\is_bool($data) || \is_float($data) || \is_int($data) || \is_string($data)) { |
|
163 | + $result = [$this->getPrimitiveMappingConfigOrThrow($data)[RB::KEY_KEY] => $data]; |
|
164 | 164 | } |
165 | 165 | |
166 | 166 | return $result; |
@@ -199,12 +199,12 @@ discard block |
||
199 | 199 | |
200 | 200 | foreach ($data as $key => $val) { |
201 | 201 | if (\is_array($val)) { |
202 | - $data[ $key ] = $this->convertArray($val); |
|
202 | + $data[$key] = $this->convertArray($val); |
|
203 | 203 | } elseif (\is_object($val)) { |
204 | 204 | $cfg = $this->getClassMappingConfigOrThrow($val); |
205 | - $worker = new $cfg[ RB::KEY_HANDLER ](); |
|
205 | + $worker = new $cfg[RB::KEY_HANDLER](); |
|
206 | 206 | $converted_data = $worker->convert($val, $cfg); |
207 | - $data[ $key ] = $converted_data; |
|
207 | + $data[$key] = $converted_data; |
|
208 | 208 | } |
209 | 209 | } |
210 | 210 |
@@ -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 | * Checks if given array uses custom (non numeric) keys. |