|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebTheory\Collection\Comparison\Abstracts; |
|
4
|
|
|
|
|
5
|
|
|
use WebTheory\Collection\Contracts\CollectionComparatorInterface; |
|
6
|
|
|
|
|
7
|
|
|
abstract class AbstractCollectionComparator implements CollectionComparatorInterface |
|
8
|
|
|
{ |
|
9
|
33 |
|
public function diff(array $array, array $values): array |
|
10
|
|
|
{ |
|
11
|
33 |
|
return array_values( |
|
12
|
33 |
|
array_udiff($array, $values, $this->getComparisonFunction()) |
|
13
|
|
|
); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
21 |
|
public function contrast(array $array1, array $array2): array |
|
17
|
|
|
{ |
|
18
|
|
|
return [ |
|
19
|
21 |
|
...$this->diff($array1, $array2), |
|
20
|
21 |
|
...$this->diff($array2, $array1), |
|
21
|
|
|
]; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
15 |
|
public function intersect($array, $values): array |
|
25
|
|
|
{ |
|
26
|
15 |
|
return array_values( |
|
27
|
15 |
|
array_uintersect($array, $values, $this->getComparisonFunction()) |
|
28
|
|
|
); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
6 |
|
public function matches(array $array1, array $array2): bool |
|
32
|
|
|
{ |
|
33
|
6 |
|
return empty($this->contrast($array1, $array2)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function stripDuplicates(array $array): array |
|
37
|
|
|
{ |
|
38
|
|
|
return array_unique($array, SORT_REGULAR); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
protected function whatEvenIsThis(array $array1, array $array2): array |
|
42
|
|
|
{ |
|
43
|
|
|
/* |
|
44
|
|
|
if both primary and secondary are empty this will return false |
|
45
|
|
|
because the "array_diff" family of functions returns an empty array |
|
46
|
|
|
if the first array provided is empty itself. if both arrays are |
|
47
|
|
|
empty this will return an empty array as there is no difference. |
|
48
|
|
|
*/ |
|
49
|
|
|
return !empty($array1) |
|
50
|
|
|
? $this->diff($array1, $array2) |
|
51
|
|
|
: $this->diff($array2, $array1); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|