Passed
Push — master ( cd4678...c3f0e5 )
by Radu
03:07
created

Arrays::powerSet()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 3
nop 1
1
<?php
2
namespace WebServCo\Framework\Utils;
3
4
final class Arrays
5
{
6
    /**
7
    * @param array[] $array
8
    * @param mixed $value
9
    */
10
    public function has(?array $array, $value) : bool
11
    {
12
        if (!is_array($array)) {
0 ignored issues
show
introduced by
The condition is_array($array) is always true.
Loading history...
13
            return false;
14
        }
15
        return in_array($value, $array);
16
    }
17
18
    /**
19
    * @param array[] $array
20
    * @param mixed $key
21
    * @param mixed $defaultValue
22
    * @return mixed
23
    */
24
    public static function get(array $array, $key, $defaultValue = false)
25
    {
26
        if (!is_array($array)) {
0 ignored issues
show
introduced by
The condition is_array($array) is always true.
Loading history...
27
            return $defaultValue;
28
        }
29
        return array_key_exists($key, $array) ? $array[$key] : $defaultValue;
30
    }
31
32
    /**
33
    * @param array[] $array
34
    */
35
    public static function isMultidimensional(array $array) : bool
36
    {
37
        if (empty($array)) {
38
            return false;
39
        }
40
        return is_array($array[key($array)]);
41
    }
42
43
    /**
44
    * @param array<mixed> $array
45
    * @return array[]
46
    */
47
    public static function nullToEmptyString(array $array) : array
48
    {
49
        foreach ($array as $key => $value) {
50
            if (is_null($value)) {
51
                $array[$key] = '';
52
            }
53
        }
54
        return $array;
55
    }
56
57
    /**
58
    * Array power set (all element combinations)
59
    *
60
    * Original function credit:
61
    * "PHP Cookbook by David Sklar, Adam Trachtenberg"
62
    * 4.24. Finding All Element Combinations of an Array
63
    * https://www.oreilly.com/library/view/php-cookbook/1565926811/ch04s25.html
64
    * @param array[] $array
65
    * @return array[]
66
    */
67
    public static function powerSet(array $array) : array
68
    {
69
        $results = [[]]; //initialize by adding the empty set
70
        foreach ($array as $element) {
71
            foreach ($results as $combination) {
72
                array_push($results, array_merge([$element], $combination));
73
            }
74
        }
75
        // sort by number of elements descending
76
        $array1 = array_map('count', $results);
77
        array_multisort($array1, SORT_DESC, $results);
78
79
        return array_filter($results); // removes empty elements
80
    }
81
82
    /**
83
    * @param array<mixed> $array
84
    */
85
    public static function toUrlQueryString(array $array) : ?string
86
    {
87
        if (empty($array)) {
88
            return null;
89
        }
90
        $queries = [];
91
        foreach ($array as $k => $v) {
92
            $queries[] = sprintf('%s=%s', $k, $v);
93
        }
94
        return '?' . implode('&', $queries);
95
    }
96
}
97