1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Psh\Application; |
4
|
|
|
|
5
|
|
|
class ParameterParser |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @param array $params |
9
|
|
|
* @return array |
10
|
|
|
*/ |
11
|
|
|
public function parseParams(array $params): array |
12
|
|
|
{ |
13
|
|
|
if (count($params) < 2) { |
14
|
|
|
return []; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
$params = array_slice($params, 2); |
18
|
|
|
|
19
|
|
|
$reformattedParams = []; |
20
|
|
|
$paramsCount = count($params); |
21
|
|
|
for ($i = 0; $i < $paramsCount; $i++) { |
22
|
|
|
$key = $params[$i]; |
23
|
|
|
|
24
|
|
|
$this->testParameterFormat($key); |
25
|
|
|
|
26
|
|
|
if ($this->isKeyValuePair($key)) { |
27
|
|
|
list($key, $value) = explode('=', $key, 2); |
28
|
|
|
|
29
|
|
|
if ($this->isEnclosedInAmpersand($value)) { |
30
|
|
|
$value = substr($value, 1, -1); |
31
|
|
|
} |
32
|
|
|
} else { |
33
|
|
|
$i++; |
34
|
|
|
$value = $params[$i]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$key = str_replace('--', '', $key); |
38
|
|
|
$reformattedParams[strtoupper($key)] = $value; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $reformattedParams; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $key |
46
|
|
|
*/ |
47
|
|
|
private function testParameterFormat(string $key) |
48
|
|
|
{ |
49
|
|
|
if (strpos($key, '--') !== 0) { |
50
|
|
|
throw new InvalidParameterException( |
51
|
|
|
sprintf('Unable to parse parameter %s. Use -- for correct usage', $key) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $key |
58
|
|
|
* @return bool|int |
59
|
|
|
*/ |
60
|
|
|
private function isKeyValuePair($key) |
61
|
|
|
{ |
62
|
|
|
return strpos($key, '='); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $value |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
private function isEnclosedInAmpersand($value): bool |
70
|
|
|
{ |
71
|
|
|
return strpos($value, '"') === 0; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|