Completed
Pull Request — master (#148)
by Marcin
08:39 queued 05:32
created

Util::printArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 12
rs 10
c 0
b 0
f 0
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 108
    public static function mergeConfig(array $original, array $merging): array
33
    {
34 108
        $array = $original;
35 108
        foreach ($merging as $m_key => $m_val) {
36 36
            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 31
                $array[ $m_key ] = $m_val;
51
            }
52
        }
53
54 108
        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 108
    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 108
            $pri_b = $array_b['pri'] ?? 0;
68 108
69
            return $pri_b <=> $pri_a;
70 108
        });
71 108
    }
72 108
73
	/**
74
	 * Prints content of given array in compacted form.
75
	 *
76
	 * @param array $array
77
	 * @param int   $indent
78
	 */
79
	public static function printArray(array $array, int $indent = 0): void
80
	{
81
		$i = '  ' . substr('                        ', 0, $indent * 2);
82
83
		foreach ($array as $k => $v) {
84
			if (\is_array($v)) {
85
				echo "{$i}{$k}:\n";
86
				self::printArray($v, $indent + 1);
87
			} else {
88
				echo "{$i}{$k}: {$v}\n";
89
			}
90
		}
91
	}
92
93
94
}
95