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
|
87 |
|
* |
33
|
|
|
* @throws \RuntimeException |
34
|
87 |
|
*/ |
35
|
87 |
|
public static function mergeConfig(array $original, array $merging): array |
36
|
23 |
|
{ |
37
|
9 |
|
$array = $original; |
38
|
9 |
|
foreach ($merging as $m_key => $m_val) { |
39
|
9 |
|
if (\array_key_exists($m_key, $original)) { |
40
|
1 |
|
$orig_type = \gettype($original[ $m_key ]); |
41
|
1 |
|
$m_type = \gettype($m_val); |
42
|
|
|
if ($orig_type !== $m_type) { |
43
|
|
|
throw new Ex\IncompatibleTypeException( |
44
|
8 |
|
"Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
45
|
8 |
|
} |
46
|
|
|
|
47
|
8 |
|
if (\is_array($merging[ $m_key ])) { |
48
|
|
|
$array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
49
|
|
|
} else { |
50
|
22 |
|
$array[ $m_key ] = $m_val; |
51
|
|
|
} |
52
|
|
|
} else { |
53
|
|
|
$array[ $m_key ] = $m_val; |
54
|
87 |
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
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
|
87 |
|
* |
64
|
|
|
* @param array &$array |
65
|
|
|
*/ |
66
|
87 |
|
public static function sortArrayByPri(array &$array): void |
67
|
87 |
|
{ |
68
|
|
|
uasort($array, static function(array $array_a, array $array_b) { |
69
|
87 |
|
$pri_a = $array_a['pri'] ?? 0; |
70
|
87 |
|
$pri_b = $array_b['pri'] ?? 0; |
71
|
87 |
|
|
72
|
|
|
return $pri_b <=> $pri_a; |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Checks if given array uses custom (non numeric) keys. |
78
|
|
|
* |
79
|
|
|
* @param array $data |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public static function isArrayWithNonNumericKeys(array $data): bool |
84
|
|
|
{ |
85
|
|
|
foreach (\array_keys($data) as $key) { |
86
|
|
|
if (!\is_int($key)) { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|