Conditions | 4 |
Paths | 4 |
Total Lines | 24 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 20 |
Changes | 0 |
1 | <?php |
||
23 | public static function stringToArray(string $string = null, string $valueDelimiter = ':', string $itemDelimiter = ',') |
||
24 | { |
||
25 | if(is_null($string)) { |
||
26 | return []; |
||
27 | } |
||
28 | |||
29 | $values = []; |
||
30 | $items = preg_split('/' . preg_quote($itemDelimiter) . '/', $string, -1, PREG_SPLIT_NO_EMPTY); |
||
31 | |||
32 | foreach ($items as $index => $item) { |
||
33 | $item = explode($valueDelimiter, $item); |
||
34 | |||
35 | $key = array_get($item, 0); |
||
36 | $value = array_get($item, 1); |
||
37 | |||
38 | if(is_null($value)) { |
||
39 | $value = $key; |
||
40 | $key = $index; |
||
41 | } |
||
42 | |||
43 | $values[trim($key)] = trim($value); |
||
|
|||
44 | } |
||
45 | |||
46 | return $values; |
||
47 | } |
||
48 | } |