-
-
Notifications
You must be signed in to change notification settings - Fork 556
/
Copy pathArrayInputTrait.php
50 lines (45 loc) · 1.34 KB
/
ArrayInputTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/**
* @file
* Contains Drupal\Console\Command\Shared\ArrayInputTrait.
*/
namespace Drupal\Console\Command\Shared;
/**
* Class ArrayInputTrait
*
* @package Drupal\Console\Command
*/
trait ArrayInputTrait
{
/**
* Parse strings to array '"key":"value","key1":"value1"'.
*
* @param string $inlineInputs
* Input from the user.
* @return array
* Input array.
*/
public function explodeInlineArray($inlineInputs)
{
$inputs = [];
foreach ($inlineInputs as $inlineInput) {
$explodeInput = explode('|', $inlineInput);
$parameters = [];
foreach ($explodeInput as $inlineParameter) {
$inlineParameter = trim($inlineParameter);
list($key, $value) = explode('":"', $inlineParameter);
$key = rtrim(ltrim($key, '"'), '"');
$value = rtrim(ltrim($value, '"'), '"');
if (!empty($value)) {
$parameters[$key] = $value;
}
}
// Remove options data if type isn't in the list of allowed types
if($parameters['options'] && !in_array($parameters['type'], ['checkboxes', 'radios', 'select'])){
$parameters['options'] = [];
}
$inputs[] = $parameters;
}
return $inputs;
}
}