Passed
Push — master ( 74b96c...259c4a )
by Shane
01:55
created

Arr::keyBy()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.6111
cc 5
nc 4
nop 2
crap 5
1
<?php
2
3
namespace ElegantMedia\PHPToolkit;
4
5
class Arr
6
{
7
8
9
	/**
10
	 *
11
	 * Is the array an associative array?
12
	 *
13
	 * @param $input
14
	 *
15
	 * @return bool
16
	 */
17 27
	public static function isAssoc($input): bool
18
	{
19 27
		if (!is_array($input)) {
20 3
			return false;
21
		}
22 24
		return array_keys($input) !== range(0, count($input) - 1);
23
	}
24
25
26
	/**
27
	 *
28
	 * Find matching structure on a nested array recursively
29
	 *
30
	 * @param       $subset
31
	 * @param       $array
32
	 * @param array $results
33
	 *
34
	 * @return array
35
	 */
36 21
	public static function intersectRecursive($array, $subset, $results = []): array
37
	{
38 21
		$isAssocArray = self::isAssoc($subset);
39
40 21
		if ($isAssocArray) {
41
			// loop each row of array
42
			// iterating through parents
43 12
			foreach ($subset as $key => $value) {
44 12
				if ($key) {
45 12
					if (isset($array[$key])) {
46 12
						$filteredSource = $array[$key];
47
48
						//if the value is array, it will do the recursive
49 12
						if (is_array($value)) {
50 12
							$loopResults = self::intersectRecursive($filteredSource, $subset[$key], $results);
51 12
							$results[$key] = $loopResults;
52
						}
53
					}
54
				}
55
			}
56
		} else {
57
			// iterate through final leaf nodes
58 21
			foreach ($subset as $subsetRow) {
59 21
				foreach ($array as $sourceRow) {
60 21
					$subsetRow = self::removeChildArrays($subsetRow);
61 21
					$sourceRow = self::removeChildArrays($sourceRow);
62 21
					if (array_intersect($subsetRow, $sourceRow) == $subsetRow) {
63 18
						$results[] = $subsetRow;
64
					}
65
				}
66
			}
67
		}
68
69 21
		return $results;
70
	}
71
72
73 21
	public static function removeChildArrays($array): array
74
	{
75 21
		$response = [];
76 21
		foreach ($array as $key => $value) {
77 21
			if (is_array($value)) {
78 3
				$response[$key] = json_encode($value);
79
			} else {
80 21
				$response[$key] = $value;
81
			}
82
		}
83 21
		return $response;
84
	}
85
86
	/**
87
	 *
88
	 * Implode but don't include empty values
89
	 *
90
	 * @param $glue
91
	 * @param $pieces
92
	 *
93
	 * @return string
94
	 */
95 3
	public static function implodeIgnoreEmpty($glue, $pieces): string
96
	{
97
		// remove empty values
98 3
		$pieces = array_filter($pieces);
99
100 3
		return implode($glue, $pieces);
101
	}
102
103
	/**
104
	 *
105
	 * Replace an existing key of an array with a new one, recursively if required.
106
	 *
107
	 * @param array $array
108
	 * @param $existingKey
109
	 * @param $newKey
110
	 * @param false $recursive
111
	 * @return array
112
	 */
113 6
	public static function swapKey(array $array, $existingKey, $newKey, $recursive = false): array
114
	{
115 6
		$allArrayData = [];
116 6
		foreach ($array as $item) {
117 6
			$arrayData = $item;
118 6
			if (array_key_exists($existingKey, $arrayData)) {
119 6
				$arrayData[$newKey] = $arrayData[$existingKey];
120 6
				unset($arrayData[$existingKey]);
121
			}
122
123
			// do this recursively
124 6
			if ($recursive) {
125 3
				if (isset($arrayData[$newKey]) && count($arrayData[$newKey])) {
126 3
					$arrayData[$newKey] = self::swapKey($arrayData[$newKey], $existingKey, $newKey, true);
127
				}
128
			}
129
130 6
			$allArrayData[] = $arrayData;
131
		}
132 6
		return $allArrayData;
133
	}
134
135
	/**
136
	 *
137
	 * Get an array and key it by a given key
138
	 * @example
139
	 * [
140
	 *        [ 'name' => 'john', 'age' => 45 ],
141
	 *        [ 'name' => 'jane', 'age', => 32 ],
142
	 * ]
143
	 * $people = Arr::keyBy($rows, 'name');
144
	 *
145
	 * becomes
146
	 * [
147
	 *        'john' => [ 'age' => 45 ],
148
	 *        'jane' => [ 'age' => 32 ],
149
	 * ]
150
	 *
151
	 *
152
	 * @param $array
153
	 * @param $keyBy
154
	 *
155
	 * @return array
156
	 */
157 3
	public static function keyBy($array, $keyBy): array
158
	{
159 3
		$newValues = [];
160
161 3
		foreach ($array as $key => $value) {
162 3
			if (is_array($value)) {
163 3
				if (isset($value[$keyBy]) && $value[$keyBy] !== '') {
164 3
					$newValues[$value[$keyBy]][] = $value;
165
				}
166
			}
167
		}
168
169 3
		return $newValues;
170
	}
171
}
172