ArrayUtils::shortenSequential()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
cc 6
eloc 16
nc 9
nop 2
1
<?php
2
3
namespace Ridibooks\Platform\Common;
4
5
class ArrayUtils
6
{
7
	/**
8
	 * $array1을 기준으로 $array2와 비교했을 때, $array1이 바뀐 값들을 리턴한다.
9
	 * @param array $array1
10
	 * @param array $array2
11
	 * @return array
12
	 */
13
	public static function getArrayDiffRecursively($array1, $array2)
14
	{
15
		$diff_array = [];
16
17
		// value 비교
18
		if (!is_array($array1)) {
19
			if ($array1 != $array2) {
20
				return $array1;
21
			} else {
22
				return [];
23
			}
24
		}
25
26
		// array 비교
27
		foreach ($array1 as $key => $value) {
28
			if (is_array($value)) {
29
				if (!is_array($array2[$key])) {
30
					$diff_array[$key] = $value;
31
				} else {
32
					$sub_diff_array = ArrayUtils::getArrayDiffRecursively($value, $array2[$key]);
33
					if (count($sub_diff_array)) {
34
//						$diff_array[$key] = $sub_diff_array;
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
35
						$diff_array[$key] = $value;
36
					}
37
				}
38
			} elseif ($array2[$key] != $value) {
39
				$diff_array[$key] = $value;
40
			}
41
		}
42
		return $diff_array;
43
	}
44
45 View Code Duplication
	public static function joinDicts($leftDicts, $rightDicts, $leftDictsColumnIndex = 0, $rightDictsColumnIndex = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
	{
47
		if (count($leftDicts) == 0) {
48
			return $rightDicts;
49
		}
50
		if (count($rightDicts) == 0) {
51
			return $leftDicts;
52
		}
53
54
		$leftKeys = array_keys($leftDicts[0]);
55
		$leftKey = $leftKeys[$leftDictsColumnIndex];
56
		$rightKeys = array_keys($rightDicts[0]);
57
		$rightKey = $rightKeys[$rightDictsColumnIndex];
58
59
		foreach ($leftDicts as $lk => $lv) {
60
			foreach ($rightDicts as $rk => $rv) {
61
				if ($lv[$leftKey] != $rv[$rightKey]) {
62
					continue;
63
				}
64
				$leftDicts[$lk] = array_merge($lv, $rv);
65
			}
66
		}
67
68
		$keys = [];
69
		foreach ($leftDicts as $dict) {
70
			$keys = array_unique(array_merge($keys, array_keys($dict)));
71
		}
72
73
		foreach ($leftDicts as $dictKey => $dict) {
74
			foreach ($keys as $key) {
75
				if (!isset($leftDicts[$dictKey][$key])) {
76
					$leftDicts[$dictKey][$key] = null;
77
				}
78
			}
79
		}
80
81
		return $leftDicts;
82
	}
83
84
	/**
85
	 * @param $from_list
86
	 * @param $to_list
87
	 * @return array
88
	 * [input left_list]   [input right_list]
89
	 * 111              => 444
90
	 * 222              => 444
91
	 * 444              => 333
92
	 * 444              => 111
93
	 * 555              => 666
94
	 * [return]
95
	 * array(
96
	 *   array(111, 222, 444, 333)
97
	 *   array(555, 666)
98
	 * )
99
	 */
100
	public static function convertLinksToSets($from_list, $to_list)
101
	{
102
		$sets = [];
103
		$setid_by_bid_map = [];
104
		foreach ($from_list as $i => $b_id) {
105
			$b_id2 = $to_list[$i];
106
			if (!strlen($b_id)) {
107
				continue;
108
			}
109
			if (!strlen($b_id2)) {
110
				continue;
111
			}
112
113
			if (!isset($setid_by_bid_map[$b_id]) && !isset($setid_by_bid_map[$b_id2])) {
114
				//no set
115
				$setid = count($sets);
116
				$setid_by_bid_map[$b_id] = $setid;
117
				$setid_by_bid_map[$b_id2] = $setid;
118
				$sets[$setid][] = $b_id;
119
				$sets[$setid][] = $b_id2;
120
			} elseif (isset($setid_by_bid_map[$b_id]) || isset($setid_by_bid_map[$b_id2])) {
121
				//found 1 set
122
				$setid = (isset($setid_by_bid_map[$b_id]) ? $setid_by_bid_map[$b_id] : $setid_by_bid_map[$b_id2]);
123
				$setid_by_bid_map[$b_id] = $setid;
124
				$setid_by_bid_map[$b_id2] = $setid;
125
				$sets[$setid][] = $b_id;
126
				$sets[$setid][] = $b_id2;
127
			} else {
128
				//found 2 sets => merge
129
				$setid_to_merge = $setid_by_bid_map[$b_id];
130
				$setid_to_destory = $setid_by_bid_map[$b_id2];
131
				$set_to_destory = $sets[$setid_to_destory];
132
				foreach ($set_to_destory as $merge_bid) {
133
					$setid_by_bid_map[$merge_bid] = $setid_to_merge;
134
				}
135
				$sets[$setid_to_merge] = array_merge($sets[$setid_to_merge], $set_to_destory);
136
				$sets[$setid_to_merge] = array_filter(array_unique($sets[$setid_to_merge]));
137
				unset($sets[$setid_to_destory]);
138
			}
139
		}
140
		foreach ($sets as &$set) {
141
			$set = array_unique($set);
142
		}
143
		return $sets;
144
	}
145
146
	public static function arrayFilterByKey($dict, $keys)
147
	{
148
		$ret = [];
149
		foreach ($keys as $key) {
150
			$ret[$key] = $dict[$key];
151
		}
152
		return $ret;
153
	}
154
155
	public static function convertValuesToKey($array)
156
	{
157
		return array_combine($array, $array);
158
	}
159
160
	/**
161
	 * @param $object
162
	 * @return array
163
	 */
164
	public static function parseArray($object)
165
	{
166
		if (is_array($object) === false) {
167
			if (is_null($object)) {
168
				return [];
169
			} else {
170
				return [$object];
171
			}
172
		}
173
		return $object;
174
	}
175
176
	/**
177
	 * 순차적인 array item 을 생략해주는 메서드입니다.
178
	 * @example [1, 2, 3, 5, 6, 8] -> ['1~3', '5~6', '8']
179
	 *
180
	 * @param array  $values
181
	 *
182
	 * @param string $glue
183
	 *
184
	 * @return \string[]
185
	 */
186
	public static function shortenSequential(array $values, $glue = '~')
187
	{
188
		$result = [];
189
		$sequential_values = [];
190
191
		sort($values);
192
		foreach ($values as $index => $value) {
193
			$previous_value = $values[$index - 1];
194
195
			if (!in_array(($value - 1), $values)) {
196
				if (count($sequential_values) > 0) {
197
					$result[] = self::implodeSequential($glue, $sequential_values);
198
				}
199
				$sequential_values = [$value];
200
			} else {
201
				if ($previous_value + 1 === $value) {
202
					$sequential_values[] = $value;
203
				}
204
			}
205
206
			if ($value === end($values)) {
207
				$result[] = self::implodeSequential($glue, $sequential_values);
208
			}
209
		}
210
211
		return $result;
212
	}
213
214
	/**
215
	 * @param string $glue
216
	 * @param array  $values
217
	 *
218
	 * @return string
219
	 */
220
	private static function implodeSequential(string $glue, array $values)
221
	{
222
		$first_value = reset($values);
223
		$last_value = end($values);
224
		if ($first_value !== $last_value) {
225
			return $first_value . $glue . $last_value;
226
		}
227
228
		return $first_value;
229
	}
230
}
231