Completed
Push — master ( 5c3a06...ea4baa )
by Ehsan
02:56
created

ArrayUtility::getNestedArrayValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Botonomous\utility;
4
5
/**
6
 * Class ArrayUtility.
7
 */
8
class ArrayUtility extends AbstractUtility
9
{
10
    /**
11
     * @param array $toFilter
12
     * @param array $keepKeys Includes the keys that need to be kept in $toFilter array
13
     *
14
     * @return array
15
     */
16 29
    public function filterArray(array $toFilter, array $keepKeys)
17
    {
18 29
        return array_intersect_key($toFilter, array_flip($keepKeys));
19
    }
20
21
    /**
22
     * Check if key is present in $search array and has got a value only if the value is string.
23
     *
24
     * @param       $key
25
     * @param array $search
26
     *
27
     * @return bool
28
     */
29 31
    public function arrayKeyValueExists($key, array $search)
30
    {
31 31
        if (!array_key_exists($key, $search)) {
32 4
            return false;
33
        }
34
35 30
        if (is_string($search[$key])) {
36 30
            return strlen(trim($search[$key])) > 0;
37
        }
38
39 1
        return true;
40
    }
41
42
    /**
43
     * Set a value in a nested array based on path.
44
     *
45
     * @param array $array The array to modify
46
     * @param array $path  The path in the array
47
     * @param mixed $value The value to set
48
     *
49
     * @return void
50
     */
51 24
    public function setNestedArrayValue(&$array, $path, &$value)
52
    {
53 24
        $current = &$array;
54 24
        foreach ($path as $key) {
55 24
            $current = &$current[$key];
56
        }
57
58 24
        $current = $value;
59 24
    }
60
61
    /**
62
     * Get a value in a nested array based on path
63
     * See https://stackoverflow.com/a/9628276/419887.
64
     *
65
     * @param array $array The array to modify
66
     * @param array $path  The path in the array
67
     *
68
     * @return mixed
69
     */
70 1
    public function getNestedArrayValue(&$array, $path)
71
    {
72 1
        $current = &$array;
73 1
        foreach ($path as $key) {
74 1
            $current = &$current[$key];
75
        }
76
77 1
        return $current;
78
    }
79
80
    /**
81
     * @param $array
82
     *
83
     * @return mixed
84
     */
85
    public function sortArrayByLength($array)
86
    {
87 6
        usort($array, function ($array1, $array2) {
88 6
            return strlen($array2) <=> strlen($array1);
89 6
        });
90
91 6
        return $array;
92
    }
93
94
    /**
95
     * Find the key for the max positive value
96
     *
97
     * @param $array
98
     *
99
     * @return mixed
100
     */
101 4
    public function maxPositiveValueKey($array)
102
    {
103 4
        $maxValue = max($array);
104
105 4
        if ($maxValue > 0) {
106 1
            return array_search($maxValue, $array);
107
        }
108 4
    }
109
110
}
111