Completed
Push — master ( 76cd54...4f141e )
by Derek
02:04
created

ArrayHelper::removeNumericKeys()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
crap 3
1
<?php
2
namespace Subreality\Dilmun\Anshar\Utils;
3
4
5
class ArrayHelper
6
{
7
    /** @var  array */
8
    protected $array;
9
10
    /**
11
     * ArrayHelper constructor.  Accepts and stores an array.
12
     *
13
     * @throws \InvalidArgumentException    if not initialized with an array
14
     * @param mixed[] $array                The array needing help
15
     */
16 29
    public function __construct($array)
17
    {
18 29
        if (!is_array($array)) {
19 6
            throw new \InvalidArgumentException("Supplied array is not an array");
20
        }
21
22 23
        $this->array = $array;
23 23
    }
24
25
    /**
26
     * Returns the value corresponding with a supplied key; returns FALSE if the supplied key does not exist within
27
     * the array.
28
     *
29
     * **WARNING** This function may return FALSE or a non-Boolean value that evaluates to FALSE even if a supplied
30
     * key exists
31
     *
32
     * @param int|string $key   The key to look up
33
     * @return bool|mixed       Returns FALSE if the supplied key does not exist within the array
34
     *                          Returns the value corresponding with the key otherwise
35
     */
36 10
    public function valueLookup($key)
37
    {
38 10
        $value = false;
39
40 10
        if (array_key_exists($key, $this->array)) {
41 3
            $value = $this->array[$key];
42 3
        }
43
44 10
        return $value;
45
    }
46
47
    /**
48
     * Returns a copy of the array with numeric key/value pairs stripped.
49
     *
50
     * Preserves the original array.
51
     *
52
     * @return mixed[]  A copy of the array with numeric key/value pairs stripped
53
     */
54 20
    public function removeNumericKeys()
55
    {
56 20
        $non_numeric_keys = $this->array;
57
58 20
        foreach ($this->array as $key => $value) {
59 20
            if (is_int($key)) {
60 20
                unset($non_numeric_keys[$key]);
61 20
            }
62 20
        }
63
64 20
        return $non_numeric_keys;
65
    }
66
}
67