Completed
Push — master ( 51f507...6134e1 )
by Arnold
8s
created

array_functions.php ➔ array_has_subset()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 3
nop 3
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 6
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Jasny;
4
5
/**
6
 * Get items from an array.
7
 * Set default values using [key => value].
8
 *
9
 * <code>
10
 *   list($foo, $bar, $useAll) = extract_keys($_GET, ['foo', 'bar', 'all' => false]);
11
 * </cody>
12
 *
13
 * @param array $array
14
 * @param array $keys
15
 * @return array
16
 */
17
function extract_keys(array $array, array $keys)
18
{
19 1
    $values = [];
20
21 1
    foreach ($keys as $i => $v) {
22 1
        $key = is_int($i) ? $v : $i;
23 1
        $default = is_int($i) ? null : $v;
24
        
25 1
        $values[] = isset($array[$key]) ? $array[$key] : $default;
26 1
    }
27
28 1
    return $values;
29
}
30
31
/**
32
 * Walk through the array and unset an item with the key
33
 *
34
 * @param array        $array  Array with objects or arrays
35
 * @param string|array $key
36
 */
37
function array_unset(array &$array, $key)
38
{
39 2
    foreach ($array as &$item) {
40 2
        foreach ((array)$key as $k) {
41 2
            if (is_object($item) && isset($item->$k)) {
42 1
                unset($item->$k);
43 1
            }
44
45 2
            if (is_array($item) && isset($item[$k])) {
46 1
                unset($item[$k]);
47 1
            }
48 2
        }
49 2
    }
50 2
}
51
52
/**
53
 * Return an array with only the specified keys.
54
 *
55
 * @param array $array
56
 * @param array $keys
57
 * @return array
58
 */
59
function array_only(array $array, array $keys)
60
{
61 1
    $intersect = array_fill_keys($keys, null);
62 1
    return array_intersect_key($array, $intersect);
63
}
64
65
/**
66
 * Return an array without the specified keys.
67
 *
68
 * @param array $array
69
 * @param array $keys
70
 * @return array
71
 */
72
function array_without(array $array, array $keys)
73
{
74 1
    $intersect = array_fill_keys($keys, null);
75 1
    return array_diff_key($array, $intersect);
76
}
77
78
/**
79
 * Check if an array contains a set of values.
80
 * 
81
 * @param array   $array
82
 * @param array   $subset
83
 * @param boolean $strict  Strict type checking
84
 * @return boolean
85
 */
86
function array_contains(array $array, array $subset, $strict = false)
87
{
88 3
    foreach ($subset as $value) {
89 3
        if (!in_array($value, $array, $strict)) {
90 3
            return false;
91
        }
92 3
    }
93
    
94 3
    return true;
95
}
96
97
/**
98
 * Check if an array contains a set of values with index check.
99
 * 
100
 * @param array $array
101
 * @param array $subset
102
 * @param boolean $strict  Strict type checking
103
 * @return boolean
104
 */
105
function array_has_subset(array $array, array $subset, $strict = false)
106
{
107 3
    foreach ($subset as $key => $value) {
108
        if (
109 3
            !array_key_exists($key, $array) ||
110 3
            isset($value) !== isset($array[$key]) ||
111 3
            ($strict ? $value !== $array[$key] : $value != $array[$key])
112 3
        ) {
113 3
            return false;
114
        }
115 3
    }
116
    
117 3
    return true;
118
}
119
120
/**
121
 * Flatten a nested associative array, concatenating the keys.
122
 * 
123
 * @param array  $array
124
 * @param string $glue
125
 * @return array
126
 */
127
function array_flatten(array $array, $glue = '.')
128
{
129 3
    foreach ($array as $key => &$value) {
130 3
        if (!is_associative_array($value)) {
131 3
            continue;
132
        }
133
134 3
        unset($array[$key]);
135 3
        $value = array_flatten($value, $glue);
136
137 3
        foreach ($value as $subkey => $subvalue) {
138 3
            $array[$key . $glue . $subkey] = $subvalue;
139 3
        }
140 3
    }
141
    
142 3
    return $array;
143
}
144
145