Completed
Push — master ( 013ef7...4604a2 )
by Radu
07:01
created

Arrays::removeEmptyValues()   A

Complexity

Conditions 4
Paths 5

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 4
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Utils;
6
7
final class Arrays
8
{
9
    /**
10
    * @param array<mixed> $array
11
    * @param mixed $value
12
    */
13
    public static function has(?array $array, $value): bool
14
    {
15
        if (!\is_array($array)) {
0 ignored issues
show
introduced by
The condition is_array($array) is always true.
Loading history...
16
            return false;
17
        }
18
        return \in_array($value, $array, true);
19
    }
20
21
    /**
22
    * Get a value from an array if it exists, otherwise a specified default value.
23
    * For multi dimensional arrays please see ArrayStorage.
24
     *
25
     * @param array<mixed> $array
26
    * @param mixed $key
27
    * @param mixed $defaultValue
28
    * @return mixed
29
    */
30
    public static function get(array $array, $key, $defaultValue = null)
31
    {
32
        return \array_key_exists($key, $array)
33
            ? $array[$key]
34
            : $defaultValue;
35
    }
36
37
    /**
38
    * @param array<mixed> $array
39
    */
40
    public static function isMultidimensional(array $array): bool
41
    {
42
        if (!$array) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $array of type array<mixed,mixed> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            return false;
44
        }
45
        return \is_array($array[\key($array)]);
46
    }
47
48
    /**
49
    * @param array<mixed> $array
50
    * @return array<mixed>
51
    */
52
    public static function nullToEmptyString(array $array): array
53
    {
54
        foreach ($array as $key => $value) {
55
            if (!\is_null($value)) {
56
                continue;
57
            }
58
59
            $array[$key] = '';
60
        }
61
        return $array;
62
    }
63
64
    /**
65
    * Array power set (all element combinations)
66
    *
67
    * Original function credit:
68
    * "PHP Cookbook by David Sklar, Adam Trachtenberg"
69
    * 4.24. Finding All Element Combinations of an Array
70
    * https://www.oreilly.com/library/view/php-cookbook/1565926811/ch04s25.html
71
     *
72
     * @param array<mixed> $array
73
    * @return array<mixed>
74
    */
75
    public static function powerSet(array $array): array
76
    {
77
        $results = [[]]; //initialize by adding the empty set
78
        foreach ($array as $element) {
79
            foreach ($results as $combination) {
80
                \array_push($results, \array_merge([$element], $combination));
81
            }
82
        }
83
        // sort by number of elements descending
84
        $array1 = \array_map('count', $results);
85
        \array_multisort($array1, \SORT_DESC, $results);
0 ignored issues
show
Bug introduced by
SORT_DESC cannot be passed to array_multisort() as the parameter $rest expects a reference. ( Ignorable by Annotation )

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

85
        \array_multisort($array1, /** @scrutinizer ignore-type */ \SORT_DESC, $results);
Loading history...
86
87
        return \array_filter($results); // removes empty elements
88
    }
89
90
    /**
91
    * @param array<int|string,int|float|string> $array
92
    */
93
    public static function toUrlQueryString(array $array): ?string
94
    {
95
        if (!$array) {
96
            return null;
97
        }
98
        $queries = [];
99
        foreach ($array as $k => $v) {
100
            $queries[] = \sprintf('%s=%s', $k, $v);
101
        }
102
        return '?' . \implode('&', $queries);
103
    }
104
}
105