Passed
Push — dev ( da34c3...980f00 )
by Marcin
06:17
created

Util::isArrayWithNonNumericKeys()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 9
ccs 0
cts 0
cp 0
crap 12
rs 10
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-2020 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
final class Util
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 97
    public static function mergeConfig(array $original, array $merging): array
33
    {
34 97
        $array = $original;
35 97
        foreach ($merging as $m_key => $m_val) {
36 23
            if (\array_key_exists($m_key, $original)) {
37 9
                $orig_type = \gettype($original[ $m_key ]);
38 9
                $m_type = \gettype($m_val);
39 9
                if ($orig_type !== $m_type) {
40 1
                    throw new \RuntimeException(
41 1
                        "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}').");
42
                }
43
44 8
                if (\is_array($merging[ $m_key ])) {
45 8
                    $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val);
46
                } else {
47 8
                    $array[ $m_key ] = $m_val;
48
                }
49
            } else {
50 22
                $array[ $m_key ] = $m_val;
51
            }
52
        }
53
54 97
        return $array;
55
    }
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 97
    public static function sortArrayByPri(array &$array): void
64
    {
65
        uasort($array, static function(array $array_a, array $array_b) {
66 97
            $pri_a = $array_a['pri'] ?? 0;
67 97
            $pri_b = $array_b['pri'] ?? 0;
68
69 97
            return $pri_b <=> $pri_a;
70 97
        });
71 97
    }
72
73
	/**
74
	 * Checks if given array uses custom (non numeric) keys.
75
	 *
76
	 * @param array $data
77
	 *
78
	 * @return bool
79
	 */
80
	public static function isArrayWithNonNumericKeys(array $data): bool
81
	{
82
		foreach (\array_keys($data) as $key) {
83
			if (!\is_int($key)) {
84
				return true;
85
			}
86
		}
87
88
		return false;
89
	}
90
91
}
92