1 | <?php |
||
2 | |||
3 | namespace Slides\Connector\Auth\Helpers; |
||
4 | |||
5 | use Illuminate\Support\Arr; |
||
6 | |||
7 | /** |
||
8 | * Class ConsoleHelper |
||
9 | * |
||
10 | * @package Slides\Connector\Auth\Helpers |
||
11 | */ |
||
12 | class ConsoleHelper |
||
13 | { |
||
14 | /** |
||
15 | * Convert a string like "field1:value1,field2:value" to the array |
||
16 | * |
||
17 | * Also supports one-dimensional array in the representation "value1,value2,value3" |
||
18 | * |
||
19 | * @param string|null $string |
||
20 | * @param string $valueDelimiter |
||
21 | * @param string $itemDelimiter |
||
22 | * |
||
23 | * @return array |
||
24 | */ |
||
25 | public static function stringToArray(string $string = null, string $valueDelimiter = ':', string $itemDelimiter = ',') |
||
26 | { |
||
27 | if(is_null($string)) { |
||
28 | return []; |
||
29 | } |
||
30 | |||
31 | $values = []; |
||
32 | $items = preg_split('/' . preg_quote($itemDelimiter) . '/', $string, -1, PREG_SPLIT_NO_EMPTY); |
||
33 | |||
34 | foreach ($items as $index => $item) { |
||
35 | $item = explode($valueDelimiter, $item); |
||
36 | |||
37 | $key = Arr::get($item, 0); |
||
38 | $value = Arr::get($item, 1); |
||
39 | |||
40 | if(is_null($value)) { |
||
41 | $value = $key; |
||
42 | $key = $index; |
||
43 | } |
||
44 | |||
45 | $values[trim($key)] = trim($value); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
46 | } |
||
47 | |||
48 | return $values; |
||
49 | } |
||
50 | } |