ArrayAccessor::replaceKey()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

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 2
eloc 7
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Pouzor\MongoDBBundle\Services;
4
5
6
class ArrayAccessor
7
{
8
9
    /**
10
     * Replace an array key with another while keeping value
11
     *
12
     * @param array $array
13
     * @param $key1
14
     * @param $key2
15
     * @return array
16
     */
17 1
    public static function replaceKey(&$array, $key1, $key2)
18
    {
19 1
        $keys = array_keys($array);
20 1
        $index = array_search($key1, $keys, true);
21
22 1
        if ($index !== false) {
23 1
            $keys[$index] = $key2;
24 1
            $array = array_combine($keys, $array);
25 1
        }
26
27 1
        return $array;
28
    }
29
30
    /**
31
     * Get a reference from an array using a path with dot notation. Ex:
32
     *
33
     * array(
34
     *      'person' => [
35
     *           'name' => 'john',
36
     *           'lastname' => 'smith',
37
     *           'childs' => [
38
     *               [ 'name' => 'victor'],
39
     *               [ 'name' => 'david']
40
     *           ]
41
     *      ]
42
     * )
43
     *
44
     * will return for:
45
     *
46
     * person.name = john
47
     * person.childs.0.name = victor
48
     *
49
     * @param array $data
50
     * @param $path
51
     * @param $default
52
     * @return array|null
53
     */
54 3
    public static function dget(array &$data, $path, $default = null)
55
    {
56 3
        $keys = explode('.', $path);
57 3
        foreach ($keys as $k) {
58 3
            if (isset($data[$k])) {
59 3
                $data =& $data[$k];
60 3
            } else {
61 1
                return $default;
62
            }
63 3
        }
64 3
        return $data;
65
    }
66
67
    /**
68
     * Set a reference from an array using a path with dot notation. Ex:
69
     *
70
     * array(
71
     *      'person' => [
72
     *           'name' => 'john',
73
     *           'lastname'='smith',
74
     *           'childs': [
75
     *               [ 'name' => 'victor'],
76
     *               [ 'name' => 'david']
77
     *           ]
78
     *      ]
79
     * )
80
     *
81
     * will set for:
82
     *
83
     * person.name = john
84
     * person.childs.0.name = victor
85
     *
86
     *
87
     * @param array $data
88
     * @param $path
89
     * @param $value
90
     */
91 1
    public static function dset(array &$data, $path, $value)
92
    {
93 1
        $keys = explode('.', $path);
94 1
        $last = array_pop($keys);
95 1
        foreach ($keys as $k) {
96 1
            if (isset($data[$k]) && is_array($data[$k])) {
97 1
                $data =& $data[$k];
98 1
            } else {
99 1
                $data[$k] = array();
100 1
                $data =& $data[$k];
101
            }
102 1
        }
103 1
        $data[$last] = $value;
104 1
    }
105
106
    /**
107
     * Count a reference from an array using a path with do notation. Ex:
108
     *
109
     * array(
110
     *      'person' => [
111
     *           'name' => 'john',
112
     *           'lastname'='smith',
113
     *           'childs': [
114
     *               [ 'name' => 'victor'],
115
     *               [ 'name' => 'david']
116
     *           ]
117
     *      ]
118
     * )
119
     *
120
     * will return for:
121
     *
122
     * person.name = 1
123
     * person.childs = 2
124
     *
125
     * @param array $data
126
     * @param $path
127
     * @return int|null
128
     */
129 1
    public static function dcount(array &$data, $path)
130
    {
131 1
        $keys = explode('.', $path);
132 1
        $last = array_pop($keys);
133 1 View Code Duplication
        foreach ($keys as $k) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134 1
            if (isset($data[$k]) && is_array($data[$k])) {
135 1
                $data =& $data[$k];
136 1
            } else {
137 1
                return null;
138
            }
139 1
        }
140 1
        return isset($data[$last]) && is_array($data[$last]) ? count($data[$last]) : null;
141
    }
142
143
    /**
144
     * Deletes a reference from an array using a path with do notation. Ex:
145
     *
146
     * array(
147
     *      'person' => [
148
     *           'name' => 'john',
149
     *           'lastname'='smith',
150
     *           'childs': [
151
     *               [ 'name' => 'victor'],
152
     *               [ 'name' => 'david']
153
     *           ]
154
     *      ]
155
     * )
156
     *
157
     * will delete for:
158
     *
159
     * person.name
160
     *
161
     * will result in
162
     *
163
     * array(
164
     *      'person' => [
165
     *           'lastname'='smith',
166
     *           'childs': [
167
     *               [ 'name' => 'victor'],
168
     *               [ 'name' => 'david']
169
     *           ]
170
     *      ]
171
     * )
172
     *
173
     * @param array $data
174
     * @param $path
175
     */
176 1
    public static function ddel(array &$data, $path)
177
    {
178 1
        $keys = explode('.', $path);
179 1
        $last = array_pop($keys);
180 1 View Code Duplication
        foreach ($keys as $k) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181 1
            if (isset($data[$k]) && is_array($data[$k])) {
182 1
                $data =& $data[$k];
183 1
            } else {
184 1
                return;
185
            }
186 1
        }
187 1
        unset($data[$last]);
188 1
    }
189
190
    /**
191
     * @param $array
192
     * @param $key
193
     * @param null $default
194
     * @return null
195
     */
196 1
    public static function get_key_exist($array, $key, $default = null) {
197
198 1
        if (isset($array[$key]))
199 1
            return $array[$key];
200
201 1
        return $default;
202
203
    }
204
}
205