Passed
Push — master ( 1194ed...f9a0e6 )
by Tomáš
01:47
created

Arrays::getReference()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 6
nop 2
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace Apicart\Utils\Arrays;
4
5
use InvalidArgumentException;
6
7
final class Arrays
8
{
9
10
	/**
11
	 * @param mixed $default
12
	 * @return mixed
13
	 */
14 1
	public static function get(array $array, string $path, $default = null)
15
	{
16 1
		$return = $default;
17 1
		$keyParts = explode('.', $path);
18 1
		$rootKey = (string) array_shift($keyParts);
19 1
		if (strpos($rootKey, ':') !== false) { // parse sub-structure search
20 1
			$parts = explode(':', $rootKey);
21 1
			$rootKey = $parts[0];
22 1
			$rootValue = $parts[1];
23
		}
24 1
		foreach ($array as $mainIndex => $mainValue) {
25 1
			if (isset($rootValue)) {
26 1
				if (is_array($mainValue)) {
27 1
					foreach ($mainValue as $key => $value) {
28 1
						if ($key === $rootKey && $value === $rootValue) {
29 1
							if (count($keyParts) > 0) {
30 1
								$return = self::get($mainValue, implode('.', $keyParts), $default);
31
							} else {
32
								$return = $mainValue;
33
							}
34 1
							break 2;
35
						}
36
					}
37
				}
38 1
				continue;
39
			}
40
41 1
			if ($mainIndex === $rootKey || (is_int($mainIndex) && $mainIndex === (int) $rootKey)) {
42 1
				if (is_array($mainValue) && $keyParts !== []) {
43 1
					$return = self::get($mainValue, implode('.', $keyParts), $default);
44 1
				} elseif ($keyParts !== []) {
45
					$return = $default;
46
				} else {
47 1
					$return = $mainValue;
48
				}
49
50 1
				break;
51
			}
52
		}
53
54 1
		return $return;
55
	}
56
57
58
	/**
59
	 * @param string|int|array $key one or more keys
60
	 * @return mixed
61
	 * @throws InvalidArgumentException
62
	 */
63 2
	public static function &getReference(array &$array, $key)
64
	{
65 2
		foreach (is_array($key) ? $key : [$key] as $k) {
66 2
			if (is_array($array) || $array === null) {
67 2
				$array = &$array[$k];
68
			} else {
69 2
				throw new InvalidArgumentException('Traversed item is not an array.');
70
			}
71
		}
72
73 2
		return $array;
74
	}
75
76
77
	/**
78
	 * @param string|array $key
79
	 * @param mixed $value
80
	 */
81 1
	public static function set(array $haystack, $key, $value, string $keySeparator = '.'): array
82
	{
83 1
		if (is_string($key)) {
84 1
			$key = explode($keySeparator, $key);
85
		} elseif (! is_array($key)) {
0 ignored issues
show
introduced by
The condition is_array($key) is always true.
Loading history...
86
			$key = [$key];
87
		}
88
89 1
		$clone = $haystack;
90 1
		$valueRef = & self::getReference($clone, $key);
91 1
		$valueRef = $value;
92
93 1
		return $clone;
94
	}
95
96
97
	/**
98
	 * @param array|string|null $left
99
	 * @param array|string|null $right
100
	 * @return array
101
	 */
102 1
	public static function merge($left, $right)
103
	{
104 1
		if (is_array($left) && is_array($right)) {
105 1
			foreach ($left as $key => $val) {
106 1
				if (is_int($key)) {
107
					$right[] = $val;
108
				} else {
109 1
					if (isset($right[$key])) {
110 1
						$val = static::merge($val, $right[$key]);
111
					}
112 1
					$right[$key] = $val;
113
				}
114
			}
115 1
			return $right;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $right also could return the type string which is incompatible with the documented return type array.
Loading history...
116 1
		} elseif ($left === null && is_array($right)) {
117
			return $right;
118
		}
119
120 1
		return $left;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $left also could return the type string which is incompatible with the documented return type array.
Loading history...
121
	}
122
123
}
124