Arr::filterKeyStartsWith()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 11
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
crap 3
1
<?php
2
/**
3
 * Part of twLib
4
 * http://www.thomaswhiston.com
5
 * [email protected]
6
 * Created by PhpStorm.
7
 * User: Thomas Whiston
8
 * Date: 12/12/2015
9
 * Time: 00:15
10
 */
11
namespace twhiston\twLib\Arr;
12
13
use twhiston\twLib\Str\Str;
14
15
/**
16
 * Class Arr
17
 * Array helper functions, reKey arrays, filter arrays, get Keys for 2D Arrays
18
 * 3D arrays will only work on the top level
19
 * @package twhiston\twLib
20
 */
21
class Arr
22
{
23
24
    /**
25
     * convenience function to see if an array definately has a key in an efficient way
26
     * @param $arr
27
     * @param $key
28
     * @return bool
29
     */
30 1
    static public function hasKey(&$arr, $key)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
31
    {
32
33 1
        if (isset($arr[$key]) || array_key_exists($key, $arr)) {
34 1
            return true;
35
        }
36
37 1
        return false;
38
    }
39
40
    /**
41
     * Convenience function to get the last part of a key from an array by a division character
42
     * With this functions an array of ['node_edit_form' = 'whatever', 'user_edit_form' = new stdClass ]
43
     * would return an array of ['form','form']
44
     * @param $arr
45
     * @param $division
46
     * @return array
47
     */
48 2 View Code Duplication
    static public function getKeysByLastDivision(&$arr, $division)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
49
    {
50
51 2
        $return = array();
52 2
        foreach ($arr as $key => $value) {
53 2
            if (($pos = strrpos($key, $division)) !== false) {
54 2
                $return[] = substr($key, $pos + 1);
55 2
            }
56 2
        }
57
58 2
        return $return;
59
    }
60
61
    /**
62
     * Convenience function to get keys containing a string
63
     * @param $arr      array to test
64
     * @param $contains string to test for
65
     * @return array  members of $arr whos key contains $contains
66
     */
67 View Code Duplication
    static public function getKeyContains(&$arr, $contains)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
68
    {
69
        $results = array();
70
        foreach ($arr as $key => $value) {
71
            if (($pos = strpos($key, $contains)) !== false) {
72
                $results[] = $key;
73
            }
74
        }
75
76
        return $results;
77
    }
78
79
    /**
80
     * Convenience function to get the first part of a key from an array by a division character
81
     * With this functions an array of ['node_edit_form' = 'whatever', 'user_edit_form' = new stdClass ]
82
     * would return an array of ['node','user']
83
     * @param $arr
84
     * @param $division
85
     * @return array
86
     */
87 1 View Code Duplication
    static public function getKeysByFirstDivision(&$arr, $division)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
88
    {
89
90 1
        $return = array();
91 1
        foreach ($arr as $key => $value) {
92 1
            if (($pos = strpos($key, $division)) !== false) {
93 1
                $return[] = substr($key, 0, $pos);
94 1
            }
95 1
        }
96
97 1
        return $return;
98
99
    }
100
101
    /**
102
     * Convenience function to rekey an array by a division
103
     * With this functions an array of ['node_edit_form' = 'whatever', 'user_edit_form' = new stdClass ]
104
     * would return an array of ['form' = stdClass]
105
     * If your keys share a final part of their key they will get overwritten. Sucks to be you
106
     * @param $arr
107
     * @param $division
108
     * @return array
109
     */
110 1 View Code Duplication
    static public function reKeyByLastKeyDivision(&$arr, $division)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
111
    {
112
113 1
        $return = array();
114 1
        foreach ($arr as $key => $value) {
115 1
            if (($pos = strrpos($key, $division)) !== false) {
116 1
                $return[substr($key, $pos + 1)] = $value;
117 1
            }
118 1
        }
119
120 1
        return $return;
121
    }
122
123
    /**
124
     * Convenience function to rekey an array by a division
125
     * With this functions an array of
126
     * ['node_edit_form' = 'whatever', 'user_edit_form' = new stdClass , 'nodelimiterhere' = 0]
127
     * with division '_'
128
     * would return an array of ['node' = 'whatever','user' = stdClass]
129
     * @param $arr
130
     * @param $division
131
     * @return array
132
     */
133 1 View Code Duplication
    static public function reKeyByFirstKeyDivision(&$arr, $division)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
134
    {
135
136 1
        $return = array();
137 1
        foreach ($arr as $key => $value) {
138 1
            if (($pos = strpos($key, $division)) !== false) {
139 1
                $return[substr($key, 0, $pos)] = $value;
140 1
            }
141 1
        }
142
143 1
        return $return;
144
145
    }
146
147
    /**
148
     * Convenience function to filter arrays by their key containing a string
149
     * @param $arr      array to test
150
     * @param $contains string to test for
151
     * @return array  members of $arr whos key contains $contains
152
     */
153 1 View Code Duplication
    static public function filterKeyContains(&$arr, $contains)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
154
    {
155 1
        $results = array();
156 1
        foreach ($arr as $key => $value) {
157 1
            if (($pos = strpos($key, $contains)) !== false) {
158 1
                $results[$key] = $value;
159 1
            }
160 1
        }
161
162 1
        return $results;
163
    }
164
165
    /**
166
     * Convenience function to filter arrays by their key !containing a string
167
     * @param $arr      array to test
168
     * @param $contains string to test for
169
     * @return array  members of $arr whos key !contains $contains
170
     */
171 1 View Code Duplication
    static public function filterKeyNotContains(&$arr, $contains)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
172
    {
173 1
        $results = array();
174 1
        foreach ($arr as $key => $value) {
175 1
            if (($pos = strpos($key, $contains)) === false) {
176 1
                $results[$key] = $value;
177 1
            }
178 1
        }
179
180 1
        return $results;
181
    }
182
183
    /**
184
     * return an array of values filtered by the key starting with $startswith
185
     * @param $arr []
186
     * @param $startwith string
187
     * @return array
188
     */
189 1 View Code Duplication
    static public function filterKeyStartsWith(&$arr, $startwith)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
190
    {
191 1
        $results = array();
192 1
        foreach ($arr as $key => $value) {
193 1
            if (Str::startsWith($key, $startwith) === true) {
194 1
                $results[$key] = $value;
195 1
            }
196 1
        }
197
198 1
        return $results;
199
    }
200
201
    /**
202
     * return an array of values filtered by the key ending with $endswith
203
     * @param $arr []
204
     * @param $endswith string
205
     * @return array
206
     */
207 1 View Code Duplication
    static public function filterKeyEndsWith(&$arr, $endswith)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
208
    {
209 1
        $results = array();
210 1
        foreach ($arr as $key => $value) {
211 1
            if (Str::endsWith($key, $endswith) === true) {
212 1
                $results[$key] = $value;
213 1
            }
214 1
        }
215
216 1
        return $results;
217
    }
218
219
220
    /**
221
     * Filter an array by value
222
     * @param $arr
223
     * @param $value
224
     * @return array Array of entries that contain $value
225
     */
226 1 View Code Duplication
    static public function filterHasValue(&$arr, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
227
    {
228 1
        $results = array();
229
230
        //test what type the value is
231 1
        $type = gettype($value);
232 1
        foreach ($arr as $key => $val) {
233
            //we can only compare if the types match
234 1
            if (gettype($val) === $type) {
235 1
                if ($val == $value) {
236 1
                    $results[$key] = $val;
237 1
                }
238 1
            }
239 1
        }
240
241 1
        return $results;
242
    }
243
244
    /**
245
     * Filter an array by !value
246
     * @param $arr
247
     * @param $value
248
     * @return array Array of entries that do not contain $value
249
     */
250 1 View Code Duplication
    static public function filterHasNotValue(&$arr, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
251
    {
252 1
        $results = array();
253
254
        //test what type the value is
255 1
        $type = gettype($value);
256 1
        foreach ($arr as $key => $val) {
257
            //we can only compare if the types match
258 1
            if (gettype($val) === $type) {
259 1
                if ($val !== $value) {
260 1
                    $results[$key] = $val;
261 1
                }
262 1
            }
263 1
        }
264
265 1
        return $results;
266
    }
267
268
    /**
269
     * Filter by a type, takes internal type or fully qualified class name
270
     * @param $arr
271
     * @param $type
272
     * @return array Array of entries that are of $type
273
     */
274 1
    static public function filterByType(&$arr, $type)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
275
    {
276 1
        $results = array();
277
278 1
        if (!is_string($type)) {
279
            $type = gettype($type);
280
        }
281
282 1
        foreach ($arr as $key => $val) {
283
            //do types match
284
285 1
            if (is_object($val)) {
286 1
                $t = get_class($val);
287 1
            } else {
288 1
                $t = gettype($val);
289
            }
290 1
            if ($t === $type) {
291 1
                $results[$key] = $val;
292 1
            }
293 1
        }
294
295 1
        return $results;
296
    }
297
298
}