Util   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 24
c 4
b 0
f 0
dl 0
loc 73
ccs 20
cts 20
cp 1
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeConfig() 0 23 5
A sortArrayByPri() 0 7 1
A isArrayWithNonNumericKeys() 0 9 3
1
<?php
2
declare(strict_types=1);
3
4
namespace MarcinOrlowski\ResponseBuilder;
5
6
/**
7
 * Laravel API Response Builder
8
 *
9
 * @package   MarcinOrlowski\ResponseBuilder
10
 *
11
 * @author    Marcin Orlowski <mail (#) marcinOrlowski (.) com>
12
 * @copyright 2016-2021 Marcin Orlowski
13
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
14
 * @link      https://github.com/MarcinOrlowski/laravel-api-response-builder
15
 */
16
17
use MarcinOrlowski\ResponseBuilder\Exceptions as Ex;
18
19
/**
20
 * Commonly used utility functions
21
 */
22
final class Util
23
{
24
	/**
25
	 * Merges the configs together and takes multi-dimensional arrays into account.
26
	 * Support for multi-dimensional config array. Built-in config merge only supports flat arrays.
27
	 * Throws \RuntimeException if arrays stucture causes type conflics (i.e. you want to merge
28
	 * array with int).
29
	 *
30
	 * @param array $original Array to merge other array into. Usually default values to overwrite.
31
	 * @param array $merging  Array with items to be merged into $original, overriding (primitives) or merging
32 87
	 *                        (arrays) entries in destination array.
33
	 *
34 87
	 * @return array
35 87
	 *
36 23
	 * @throws \RuntimeException
37 9
	 */
38 9
	public static function mergeConfig(array $original, array $merging): array
39 9
	{
40 1
		$array = $original;
41 1
		foreach ($merging as $m_key => $m_val) {
42
			if (\array_key_exists($m_key, $original)) {
43
				$orig_type = \gettype($original[ $m_key ]);
44 8
				$m_type = \gettype($m_val);
45 8
				if ($orig_type !== $m_type) {
46
					throw new Ex\IncompatibleTypeException(
47 8
						"Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}').");
48
				}
49
50 22
				if (\is_array($merging[ $m_key ])) {
51
					$array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val);
52
				} else {
53
					$array[ $m_key ] = $m_val;
54 87
				}
55
			} else {
56
				$array[ $m_key ] = $m_val;
57
			}
58
		}
59
60
		return $array;
61
	}
62
63 87
	/**
64
	 * Sorts array (in place) by value, assuming value is an array and contains `pri` key with integer
65
	 * (positive/negative) value which is used for sorting higher -> lower priority.
66 87
	 *
67 87
	 * @param array &$array
68
	 */
69 87
	public static function sortArrayByPri(array &$array): void
70 87
	{
71 87
		uasort($array, static function(array $array_a, array $array_b) {
72
			$pri_a = $array_a['pri'] ?? 0;
73
			$pri_b = $array_b['pri'] ?? 0;
74
75
			return $pri_b <=> $pri_a;
76
		});
77
	}
78
79
	/**
80
	 * Checks if given array uses custom (non numeric) keys.
81
	 *
82
	 * @param array $data
83
	 *
84
	 * @return bool
85
	 */
86
	public static function isArrayWithNonNumericKeys(array $data): bool
87
	{
88
		foreach (\array_keys($data) as $key) {
89
			if (!\is_int($key)) {
90
				return true;
91
			}
92
		}
93
94
		return false;
95
	}
96
97
}
98