Completed
Pull Request — master (#68)
by Vladimir
03:57
created

ArrayUtilities::array_index_by_key()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.7998
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 4
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Utilities;
9
10
abstract class ArrayUtilities
11
{
12
    /**
13
     * @param array $array
14
     *
15
     * @return bool
16
     */
17 11
    public static function is_multidimensional(array &$array)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
18
    {
19 11
        foreach ($array as &$element)
20
        {
21 11
            if (is_array($element))
22
            {
23 11
                return true;
24
            }
25
        }
26
27 8
        return false;
28
    }
29
30
    /**
31
     * Check whether the elements of a multidimensional array can be indexed based on a key.
32
     *
33
     * In the following example, the array can be indexed with the 'name' key.
34
     *
35
     * ```php
36
     * array(
37
     *     array('name' => 'sample 1'),
38
     *     array('name' => 'sample 2'),
39
     *     array('name' => 'sample 3'),
40
     * )
41
     * ```
42
     *
43
     * @param array  $arr
44
     * @param string $indexKey The key to consider the index
45
     *
46
     * @return bool
47
     */
48 40
    public static function array_can_be_indexed(array &$arr, $indexKey)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
49
    {
50 40
        foreach ($arr as &$value)
51
        {
52 40
            if (isset($value[$indexKey]) && is_scalar($value[$indexKey]))
53
            {
54 40
                return true;
55
            }
56
        }
57
58 36
        return false;
59
    }
60
61
    /**
62
     * Index an array passed on given key.
63
     *
64
     * The resulting array would be as follows when indexed by the 'name' key.
65
     *
66
     * ```php
67
     * array(
68
     *     'sample 1' => array('name' => 'sample 1'),
69
     *     'sample 2' => array('name' => 'sample 2'),
70
     *     'sample 3' => array('name' => 'sample 3'),
71
     * )
72
     * ```
73
     *
74
     * @param array  $arr
75
     * @param string $indexKey
76
     *
77
     * @see    ArrayUtilities::array_can_be_indexed()
78
     *
79
     * @return array The original array but each element's key will be the value of the element's key value
80
     */
81 4
    public static function array_index_by_key(array &$arr, $indexKey)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
82
    {
83 4
        $result = [];
84
85 4
        foreach ($arr as &$value)
86
        {
87 4
            if (isset($value[$indexKey]) && is_scalar($value[$indexKey]))
88
            {
89 4
                $result[$value[$indexKey]] = $value;
90
            }
91
        }
92
93 4
        return $result;
94
    }
95
96
    /**
97
     * Merge two arrays together while respecting possible indexing.
98
     *
99
     * @param array  $arr1     The first array
100
     * @param array  $arr2     The values in this array will overwrite the respective values from $arr2
101
     * @param string $indexKey The key to respect when indexing
102
     *
103
     * @see    ArrayUtilities::array_index_by_key()
104
     *
105
     * @return array
106
     */
107 35
    public static function array_merge_defaults(array &$arr1, array &$arr2, $indexKey)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
108
    {
109 35
        $merged = $arr1;
110
111 35
        foreach ($arr2 as $key => &$value)
112
        {
113 35
            if (is_array($value) && isset($arr1[$key]))
114
            {
115 34
                if (self::array_can_be_indexed($value, $indexKey))
116
                {
117 1
                    $indexedArr1 = self::array_index_by_key($arr1[$key], $indexKey);
118 1
                    $indexedArr2 = self::array_index_by_key($value, $indexKey);
119 1
                    $merged[$key] = array_merge($indexedArr1, $indexedArr2);
120
                }
121
                else
122
                {
123 33
                    $merged[$key] = array_merge($arr1[$key], $value);
124
                }
125
126 34
                continue;
127
            }
128
129 34
            $merged[$key] = $value;
130
        }
131
132 35
        return $merged;
133
    }
134
135
    /**
136
     * Split an associative array into two chunks based on the position of a key.
137
     *
138
     * @param string $key       The key we'll be using as the splitting point
139
     * @param array  $array     The array we're splitting up
140
     * @param bool   $inclusive when set to true, the key being used as the splitting point will be incorporated in the
141
     *                          first chunk of the split
142
     *
143
     * @return array An array with two indices (0 & 1) containing the respective chunks of the array
144
     */
145 9
    public static function associative_array_split($key, array &$array, $inclusive = true)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
146
    {
147 9
        $offset = array_search($key, array_keys($array)) + (int)$inclusive;
148 9
        $result = [];
149
150 9
        $result[0] = array_slice($array, 0, $offset, true);
151 9
        $result[1] = array_slice($array, $offset, null, true);
152
153 9
        return $result;
154
    }
155
}
156