Completed
Push — master ( 133785...f24d65 )
by Woody
8s
created

array.php ➔ exists()   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 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Equip\Arr;
4
5
use Traversable;
6
7
/**
8
 * Check if a key exists in an array.
9
 *
10
 * @param array|Traversable $array
11
 * @param string|integer $key
12
 *
13
 * @return boolean
14
 */
15
function exists($array, $key)
16
{
17 1
    return array_key_exists($key, to_array($array));
18
}
19
20
/**
21
 * Convert a value to an array.
22
 *
23
 * If the value is an array it will be returned.
24
 * If the value is Traversable it will be converted to an array.
25
 * If the value is anything else it will be cast to an array.
26
 *
27
 * @param mixed $value
28
 *
29
 * @return array
30
 */
31
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...
32
{
33 6
    if (is_array($value)) {
34 6
        return $value;
35
    }
36
37 3
    if ($value instanceof Traversable) {
38 1
        return iterator_to_array($value);
39
    }
40
41 3
    return (array) $value;
42
}
43
44
/**
45
 * Get a single value from an array.
46
 *
47
 * If the value does not exist, the default will be returned.
48
 *
49
 * @param array|Traversable $source
50
 * @param string|integer $key
51
 * @param mixed $default
52
 *
53
 * @return mixed
54
 */
55
function get($source, $key, $default = null)
56
{
57 1
    if (exists($source, $key)) {
58 1
        return $source[$key];
59
    }
60
61 1
    return $default;
62
}
63
64
/**
65
 * Grab some values from an array.
66
 *
67
 * @param array|Traversable $source
68
 * @param array|string $keys
69
 *
70
 * @return array
71
 */
72
function some($source, $keys)
73
{
74 1
    return array_intersect_key(
75 1
        to_array($source),
76 1
        array_flip(to_array($keys))
77 1
    );
78
}
79
80
/**
81
 * Exclude some values from an array.
82
 *
83
 * This is the inverse of some().
84
 *
85
 * @param array|Traversable $source
86
 * @param array|string $keys
87
 *
88
 * @return array
89
 */
90
function without($source, $keys)
91
{
92 1
    return array_diff_key(
93 1
        to_array($source),
94 1
        array_flip(to_array($keys))
95 1
    );
96
}
97
98
/**
99
 * Take the first value from an array.
100
 *
101
 * @param array $list
102
 *
103
 * @return mixed
104
 */
105
function head($list)
106
{
107 1
    $list = to_array($list);
108 1
    return array_shift($list);
109
}
110
111
/**
112
 * Take the last value from an array.
113
 *
114
 * @param array $list
115
 *
116
 * @return mixed
117
 */
118
function tail($list)
119
{
120 1
    $list = to_array($list);
121 1
    return array_pop($list);
122
}
123