|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Slides\Saml2\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ConsoleHelper |
|
9
|
|
|
* |
|
10
|
|
|
* @package App\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
|
1 |
|
public static function stringToArray(?string $string = null, string $valueDelimiter = ':', string $itemDelimiter = ',') |
|
26
|
|
|
{ |
|
27
|
1 |
|
if(is_null($string)) { |
|
28
|
1 |
|
return []; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
$values = []; |
|
32
|
1 |
|
$items = preg_split('/' . preg_quote($itemDelimiter) . '/', $string, -1, PREG_SPLIT_NO_EMPTY); |
|
33
|
|
|
|
|
34
|
1 |
|
foreach ($items as $index => $item) { |
|
35
|
1 |
|
$item = explode($valueDelimiter, $item); |
|
36
|
|
|
|
|
37
|
1 |
|
$key = Arr::get($item, 0); |
|
38
|
1 |
|
$value = Arr::get($item, 1); |
|
39
|
|
|
|
|
40
|
1 |
|
if(is_null($value)) { |
|
41
|
1 |
|
$value = $key; |
|
42
|
1 |
|
$key = $index; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
$values[trim($key)] = trim($value); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
return $values; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Converts an array to string |
|
53
|
|
|
* |
|
54
|
|
|
* ['one', 'two', 'three'] to 'one, two, three', |
|
55
|
|
|
* ['one' => 1, 'two' => 2, 'three' => 3] to 'one:1,two:2,three:3' |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $array |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function arrayToString(array $array): string |
|
62
|
|
|
{ |
|
63
|
|
|
$values = []; |
|
64
|
|
|
|
|
65
|
|
|
foreach ($array as $key => $value) { |
|
66
|
|
|
if(is_array($value)) { |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$values[] = is_string($key) |
|
71
|
|
|
? $key . ':' . $value |
|
72
|
|
|
: $value; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return implode(',', $values); |
|
76
|
|
|
} |
|
77
|
|
|
} |