|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebTheory\Collection\Comparison\Abstracts; |
|
4
|
|
|
|
|
5
|
|
|
use LogicException; |
|
6
|
|
|
use WebTheory\Collection\Contracts\CollectionComparatorInterface; |
|
7
|
|
|
|
|
8
|
|
|
abstract class AbstractCollectionComparator implements CollectionComparatorInterface |
|
9
|
|
|
{ |
|
10
|
45 |
|
public function diff(array $array, array $values): array |
|
11
|
|
|
{ |
|
12
|
45 |
|
return array_udiff($array, $values, $this->getComparisonFunction()); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
27 |
|
public function contrast(array $array1, array $array2): array |
|
16
|
|
|
{ |
|
17
|
27 |
|
return array_merge( |
|
18
|
27 |
|
$this->diff($array1, $array2), |
|
19
|
27 |
|
$this->diff($array2, $array1), |
|
20
|
|
|
); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
21 |
|
public function intersect($array, $values): array |
|
24
|
|
|
{ |
|
25
|
21 |
|
return array_uintersect($array, $values, $this->getComparisonFunction()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
6 |
|
public function matches(array $array1, array $array2): bool |
|
29
|
|
|
{ |
|
30
|
6 |
|
return empty($this->contrast($array1, $array2)); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function whatEvenIsThis(array $array1, array $array2): array |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
throw new LogicException('No!'); |
|
36
|
|
|
|
|
37
|
|
|
/* |
|
38
|
|
|
if both primary and secondary are empty this will return false |
|
39
|
|
|
because the "array_diff" family of functions returns an empty array |
|
40
|
|
|
if the first array provided is empty itself. if both arrays are |
|
41
|
|
|
empty this will return an empty array as there is no difference. |
|
42
|
|
|
*/ |
|
43
|
|
|
return !empty($array1) |
|
|
|
|
|
|
44
|
|
|
? $this->diff($array1, $array2) |
|
45
|
|
|
: $this->diff($array2, $array1); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.