Passed
Push — master ( ce9270...90190c )
by Radu
02:14
created

Arrays::removeEmptyValues()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 4
nc 5
nop 1
1
<?php
2
namespace WebServCo\Framework\Utils;
3
4
final class Arrays
5
{
6
    public function removeEmptyValues(array $array) : array
7
    {
8
        foreach ($array as $key => $value) {
9
            if (is_array($value)) {
10
                $array[$key] = self::removeEmptyValues($array[$key]);
0 ignored issues
show
Bug Best Practice introduced by
The method WebServCo\Framework\Util...ys::removeEmptyValues() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

10
                /** @scrutinizer ignore-call */ 
11
                $array[$key] = self::removeEmptyValues($array[$key]);
Loading history...
11
            }
12
            if (empty($array[$key])) {
13
                unset($array[$key]);
14
            }
15
        }
16
        return $array;
17
    }
18
19
    /**
20
    * @param array[] $array
21
    * @param mixed $value
22
    */
23
    public function has(?array $array, $value) : bool
24
    {
25
        if (!is_array($array)) {
0 ignored issues
show
introduced by
The condition is_array($array) is always true.
Loading history...
26
            return false;
27
        }
28
        return in_array($value, $array);
29
    }
30
31
    /**
32
    * Get a value from an array if it exists, otherwise a specified default value.
33
    * For multi dimensional arrays please see ArrayStorage.
34
    * @param array[] $array
35
    * @param mixed $key
36
    * @param mixed $defaultValue
37
    * @return mixed
38
    */
39
    public static function get(array $array, $key, $defaultValue = false)
40
    {
41
        return array_key_exists($key, $array) ? $array[$key] : $defaultValue;
42
    }
43
44
    /**
45
    * @param array[] $array
46
    */
47
    public static function isMultidimensional(array $array) : bool
48
    {
49
        if (empty($array)) {
50
            return false;
51
        }
52
        return is_array($array[key($array)]);
53
    }
54
55
    /**
56
    * @param array<mixed> $array
57
    * @return array[]
58
    */
59
    public static function nullToEmptyString(array $array) : array
60
    {
61
        foreach ($array as $key => $value) {
62
            if (is_null($value)) {
63
                $array[$key] = '';
64
            }
65
        }
66
        return $array;
67
    }
68
69
    /**
70
    * Array power set (all element combinations)
71
    *
72
    * Original function credit:
73
    * "PHP Cookbook by David Sklar, Adam Trachtenberg"
74
    * 4.24. Finding All Element Combinations of an Array
75
    * https://www.oreilly.com/library/view/php-cookbook/1565926811/ch04s25.html
76
    * @param array[] $array
77
    * @return array[]
78
    */
79
    public static function powerSet(array $array) : array
80
    {
81
        $results = [[]]; //initialize by adding the empty set
82
        foreach ($array as $element) {
83
            foreach ($results as $combination) {
84
                array_push($results, array_merge([$element], $combination));
85
            }
86
        }
87
        // sort by number of elements descending
88
        $array1 = array_map('count', $results);
89
        array_multisort($array1, SORT_DESC, $results);
90
91
        return array_filter($results); // removes empty elements
92
    }
93
94
    /**
95
    * @param array<mixed> $array
96
    */
97
    public static function toUrlQueryString(array $array) : ?string
98
    {
99
        if (empty($array)) {
100
            return null;
101
        }
102
        $queries = [];
103
        foreach ($array as $k => $v) {
104
            $queries[] = sprintf('%s=%s', $k, $v);
105
        }
106
        return '?' . implode('&', $queries);
107
    }
108
}
109