Failed Conditions
Branch v2.x (0635cc)
by Arnold
09:26
created

array_functions.php ➔ array_contains_assoc()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 3
nop 3
dl 0
loc 14
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
    $values = [];
20
21
    foreach ($keys as $i => $v) {
22
        $key = is_int($i) ? $v : $i;
23
        $default = is_int($i) ? null : $v;
24
        
25
        $values[] = isset($array[$key]) ? $array[$key] : $default;
26
    }
27
28
    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
    foreach ($array as &$item) {
40
        if (is_object($item)) {
41
            foreach ((array)$key as $k) {
42
                if (isset($item->$k)) unset($item->$k);
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
43
            }
44
        } elseif (is_array($item)) {
45
            foreach ((array)$key as $k) {
46
                if (isset($item[$k])) unset($item[$k]);
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
47
            }
48
        }
49
    }
50
}
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
    $intersect = array_fill_keys($keys, null);
62
    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
    $intersect = array_fill_keys($keys, null);
75
    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
    foreach ($subset as $value) {
89
        if (!in_array($value, $array, $strict)) {
90
            return false;
91
        }
92
    }
93
    
94
    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_contains_assoc(array $array, array $subset, $strict = false)
106
{
107
    foreach ($subset as $key => $value) {
108
        if (
109
            !array_key_exists($key, $array) ||
110
            isset($value) !== isset($array[$key]) ||
111
            ($strict ? $value !== $array[$key] : $value != $array[$key])
112
        ) {
113
            return false;
114
        }
115
    }
116
    
117
    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
    foreach ($array as $key => &$value) {
130
        if (!is_associative_array($value)) {
131
            continue;
132
        }
133
134
        unset($array[$key]);
135
        $value = array_flatten($value, $glue);
136
137
        foreach ($value as $subkey => $subvalue) {
138
            $array[$key . $glue . $subkey] = $subvalue;
139
        }
140
    }
141
    
142
    return $array;
143
}
144