@@ -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. |
@@ -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)) { |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | * @return HttpResponse |
| 86 | 86 | */ |
| 87 | 87 | public static function success($data = null, $api_code = null, array $placeholders = null, |
| 88 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
| 88 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
| 89 | 89 | { |
| 90 | 90 | return static::asSuccess($api_code) |
| 91 | 91 | ->withData($data) |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | * @return HttpResponse |
| 114 | 114 | */ |
| 115 | 115 | public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
| 116 | - int $json_opts = null): HttpResponse |
|
| 116 | + int $json_opts = null): HttpResponse |
|
| 117 | 117 | { |
| 118 | 118 | return static::asError($api_code) |
| 119 | 119 | ->withPlaceholders($placeholders) |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | public function withJsonOptions(int $json_opts = null): self |
| 198 | 198 | { |
| 199 | 199 | Validator::assertIsType('json_opts', $json_opts, [Type::INTEGER, |
| 200 | - Type::NULL]); |
|
| 200 | + Type::NULL]); |
|
| 201 | 201 | $this->json_opts = $json_opts; |
| 202 | 202 | |
| 203 | 203 | return $this; |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | public function withDebugData(array $debug_data = null): self |
| 212 | 212 | { |
| 213 | 213 | Validator::assertIsType('$debug_data', $debug_data, [Type::ARRAY, |
| 214 | - Type::NULL]); |
|
| 214 | + Type::NULL]); |
|
| 215 | 215 | $this->debug_data = $debug_data; |
| 216 | 216 | |
| 217 | 217 | return $this; |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | public function withMessage(string $msg = null): self |
| 226 | 226 | { |
| 227 | 227 | Validator::assertIsType('message', $msg, [Type::STRING, |
| 228 | - Type::NULL]); |
|
| 228 | + Type::NULL]); |
|
| 229 | 229 | $this->message = $msg; |
| 230 | 230 | |
| 231 | 231 | return $this; |
@@ -312,8 +312,8 @@ discard block |
||
| 312 | 312 | * @noinspection PhpTooManyParametersInspection |
| 313 | 313 | */ |
| 314 | 314 | protected function make(bool $success, int $api_code, $msg_or_api_code, $data = null, |
| 315 | - int $http_code = null, array $placeholders = null, array $http_headers = null, |
|
| 316 | - int $json_opts = null, array $debug_data = null): HttpResponse |
|
| 315 | + int $http_code = null, array $placeholders = null, array $http_headers = null, |
|
| 316 | + int $json_opts = null, array $debug_data = null): HttpResponse |
|
| 317 | 317 | { |
| 318 | 318 | $http_headers = $http_headers ?? []; |
| 319 | 319 | $http_code = $http_code ?? ($success ? RB::DEFAULT_HTTP_CODE_OK : RB::DEFAULT_HTTP_CODE_ERROR); |
@@ -352,8 +352,8 @@ discard block |
||
| 352 | 352 | * @noinspection PhpTooManyParametersInspection |
| 353 | 353 | */ |
| 354 | 354 | protected function buildResponse(bool $success, int $api_code, |
| 355 | - $msg_or_api_code, array $placeholders = null, |
|
| 356 | - $data = null, array $debug_data = null): array |
|
| 355 | + $msg_or_api_code, array $placeholders = null, |
|
| 356 | + $data = null, array $debug_data = null): array |
|
| 357 | 357 | { |
| 358 | 358 | // ensure $data is either @null, array or object of class with configured mapping. |
| 359 | 359 | $data = (new Converter())->convert($data); |