Completed
Push — master ( 277a70...42ae0f )
by Marcin
24s queued 11s
created

Util::isArrayWithNonNumericKeys()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

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 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace MarcinOrlowski\ResponseBuilder;
5
6
7
use MarcinOrlowski\ResponseBuilder\Exceptions as Ex;
8
9
/**
10
 * Laravel API Response Builder
11
 *
12
 * @package   MarcinOrlowski\ResponseBuilder
13
 *
14
 * @author    Marcin Orlowski <mail (#) marcinOrlowski (.) com>
15
 * @copyright 2016-2020 Marcin Orlowski
16
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
17
 * @link      https://github.com/MarcinOrlowski/laravel-api-response-builder
18
 */
19
final class Util
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 101
    public static function mergeConfig(array $original, array $merging): array
36
    {
37 101
        $array = $original;
38 101
        foreach ($merging as $m_key => $m_val) {
39 28
            if (\array_key_exists($m_key, $original)) {
40 9
                $orig_type = \gettype($original[ $m_key ]);
41 9
                $m_type = \gettype($m_val);
42 9
                if ($orig_type !== $m_type) {
43 1
                    throw new Ex\IncompatibleTypeException(
44 1
                        "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}').");
45
                }
46
47 8
                if (\is_array($merging[ $m_key ])) {
48 8
                    $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val);
49
                } else {
50 8
                    $array[ $m_key ] = $m_val;
51
                }
52
            } else {
53 27
                $array[ $m_key ] = $m_val;
54
            }
55
        }
56
57 101
        return $array;
58
    }
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 101
    public static function sortArrayByPri(array &$array): void
67
    {
68
        uasort($array, static function(array $array_a, array $array_b) {
69 101
            $pri_a = $array_a['pri'] ?? 0;
70 101
            $pri_b = $array_b['pri'] ?? 0;
71
72 101
            return $pri_b <=> $pri_a;
73 101
        });
74 101
    }
75
76
	/**
77
	 * Checks if given array uses custom (non numeric) keys.
78
	 *
79
	 * @param array $data
80
	 *
81
	 * @return bool
82
	 */
83 9
	public static function isArrayWithNonNumericKeys(array $data): bool
84
	{
85 9
		foreach (\array_keys($data) as $key) {
86 9
			if (!\is_int($key)) {
87 5
				return true;
88
			}
89
		}
90
91 4
		return false;
92
	}
93
94
}
95