Completed
Push — master ( bc5d3e...2d1e33 )
by Woody
9s
created

array.php ➔ head()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Equip;
4
5
/**
6
 * Get a single value from an array.
7
 *
8
 * If the value does not exist, the default will be returned.
9
 *
10
 * @param array $source
11
 * @param string|integer $key
12
 * @param mixed $default
13
 *
14
 * @return mixed
15
 */
16
function take(array $source, $key, $default = null)
17
{
18 1
    if (isset($source[$key])) {
19 1
        return $source[$key];
20
    }
21
22 1
    return $default;
23
}
24
25
/**
26
 * Grab some values from an array.
27
 *
28
 * @param array $source
29
 * @param array|string $keys
30
 *
31
 * @return array
32
 */
33
function grab(array $source, $keys)
34
{
35 1
    return array_intersect_key(
36 1
        $source,
37 1
        array_flip((array) $keys)
38 1
    );
39
}
40
41
/**
42
 * Exclude some values from an array.
43
 *
44
 * This is the inverse of grab().
45
 *
46
 * @param array $source
47
 * @param array|string $keys
48
 *
49
 * @return array
50
 */
51
function except(array $source, $keys)
52
{
53 1
    return array_diff_key(
54 1
        $source,
55 1
        array_flip((array) $keys)
56 1
    );
57
}
58
59
/**
60
 * Take the first value from an array.
61
 *
62
 * @param array $list
63
 *
64
 * @return mixed
65
 */
66
function head(array $list)
67
{
68 1
    return array_shift($list);
69
}
70
71
/**
72
 * Take the last value from an array.
73
 *
74
 * @param array $list
75
 *
76
 * @return mixed
77
 */
78
function tail(array $list)
79
{
80 1
    return array_pop($list);
81
}
82
83
/**
84
 * Get an array from a value.
85
 *
86
 * If the value is not an array, it is assumed to be a Traversable.
87
 *
88
 * @param Traversable|array $value
89
 *
90
 * @return array
91
 */
92
function to_array($value)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
93
{
94 3
    if (is_array($value)) {
95 3
        return $value;
96
    }
97
98 3
    return iterator_to_array($value);
99
}
100
101
/**
102
 * Extract a specific key from each array
103
 *
104
 * @param array $source
105
 * @param string $key
106
 *
107
 * @return array
108
 */
109
function pluck(array $source, $key)
110
{
111
    return array_map(function ($array) use ($key) {
112 2
        if (isset($array[$key])) {
113 1
            return $array[$key];
114
        }
115 2
    }, $source);
116
}
117