|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_input\Parsers; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class Cli |
|
8
|
|
|
* @package kalanis\kw_input\Parsers |
|
9
|
|
|
* Parse input from command line |
|
10
|
|
|
* Also accepts multiple params and returns them as array |
|
11
|
|
|
*/ |
|
12
|
|
|
class Cli extends AParser |
|
13
|
|
|
{ |
|
14
|
|
|
protected const DELIMITER_LONG_ENTRY = '--'; |
|
15
|
|
|
protected const DELIMITER_SHORT_ENTRY = '-'; |
|
16
|
|
|
protected const DELIMITER_PARAM_VALUE = '='; |
|
17
|
|
|
public const UNSORTED_PARAM = 'param_'; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string[] */ |
|
20
|
|
|
protected static array $availableLetters = ['a','b','c','d','e','f','g','h','i','j','k','l','m', |
|
21
|
|
|
'n','o','p','q','r','s','t','u','v','w','x','y','z']; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param int[]|string[] $input is $argv in boot time |
|
25
|
|
|
* @return array<string|int, string|int|bool>|array<string|int, array<int, string|int|bool>> |
|
26
|
|
|
*/ |
|
27
|
4 |
|
public function parseInput(array $input): array |
|
28
|
|
|
{ |
|
29
|
4 |
|
$clearArray = []; |
|
30
|
4 |
|
$unsorted = 0; |
|
31
|
4 |
|
foreach ($input as &$posted) { |
|
32
|
4 |
|
if (0 === strpos(strval($posted), static::DELIMITER_LONG_ENTRY)) { |
|
33
|
|
|
// large params |
|
34
|
4 |
|
$entry = substr(strval($posted), strlen(static::DELIMITER_LONG_ENTRY)); |
|
35
|
4 |
|
if (false !== strpos(strval($posted), static::DELIMITER_PARAM_VALUE)) { |
|
36
|
|
|
// we have got some value, so prepare it |
|
37
|
4 |
|
list($key, $value) = explode(static::DELIMITER_PARAM_VALUE, $entry, 2); |
|
38
|
4 |
|
$addKey = $this->removeNullBytes($key); |
|
39
|
4 |
|
$addValue = $this->removeNullBytes($value); |
|
40
|
|
|
} else { |
|
41
|
|
|
// we have no value set |
|
42
|
3 |
|
$addKey = $this->removeNullBytes($entry); |
|
43
|
3 |
|
$addValue = true; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
if (isset($clearArray[$addKey])) { |
|
47
|
|
|
// if there is multiple inputs with the same key, propagate it as array |
|
48
|
3 |
|
if (!is_array($clearArray[$addKey])) { |
|
49
|
3 |
|
$clearArray[$addKey] = [$clearArray[$addKey]]; |
|
50
|
|
|
} |
|
51
|
3 |
|
$clearArray[$addKey][] = $addValue; |
|
52
|
|
|
} else { // otherwise simple add |
|
53
|
4 |
|
$clearArray[$addKey] = $addValue; |
|
54
|
|
|
} |
|
55
|
4 |
|
} elseif (0 === strpos(strval($posted), static::DELIMITER_SHORT_ENTRY)) { |
|
56
|
|
|
// just by letters |
|
57
|
4 |
|
$entry = $this->removeNullBytes(substr(strval($posted), strlen(static::DELIMITER_SHORT_ENTRY))); |
|
58
|
4 |
|
for ($i=0; $i<strlen($entry); $i++) { |
|
59
|
4 |
|
if (in_array(strtolower($entry[$i]), static::$availableLetters)) { |
|
60
|
4 |
|
$clearArray[$entry[$i]] = true; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} else { |
|
64
|
|
|
// rest of the world |
|
65
|
3 |
|
$key = static::UNSORTED_PARAM . $unsorted; |
|
66
|
3 |
|
$clearArray[$key] = $this->removeNullBytes(strval($posted)); |
|
67
|
3 |
|
$unsorted++; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
4 |
|
return $clearArray; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|