Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Set
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 3
156
0.00% covered (danger)
0.00%
0 / 1
 union
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 intersect
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 difference
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2
3namespace Lucent\Helpers\Maths;
4
5
6class Set
7{
8
9    //Union returns an array containing both sets of values;
10    public static function union(array $a, array $b): array
11    {
12        return array_merge($a,$b);
13    }
14
15    //The intersection returns elements that are in 'A' and 'B'.
16    public static function intersect(array $a, array $b, SetComparison $type = SetComparison::ValuesToValues): array
17    {
18
19        //TODO add later
20
21        return [];
22    }
23
24    //The difference returns elements that are in 'A' but not 'B'.
25    public static function difference(array $a, array $b, SetComparison $type = SetComparison::ValuesToValues) : array
26    {
27        $output = [];
28
29        switch ($type) {
30
31            case SetComparison::KeysToKeys:
32
33                foreach ($a as $key => $value) {
34                    if (!array_key_exists($key, $b)) {
35                        array_push($output, $key);
36                    }
37                }
38                break;
39
40            case SetComparison::ValuesToKeys:
41
42                foreach ($a as $item) {
43                    if (!array_key_exists($item, $b)) {
44                        array_push($output, $item);
45                    }
46                }
47                break;
48
49            //Default comparison is value to value;
50            default:
51                foreach ($a as $item) {
52                    if (!in_array($item, $b)) {
53                        array_push($output, $item);
54                    }
55                }
56        }
57
58
59        return $output;
60    }
61
62}