1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2015-11-26 |
5
|
|
|
*/ |
6
|
|
|
namespace Net\Bazzline\Component\Cli\Arguments; |
7
|
|
|
|
8
|
|
|
class Parser |
9
|
|
|
{ |
10
|
|
|
/** @var array */ |
11
|
|
|
private $flags; |
12
|
|
|
|
13
|
|
|
/** @var array */ |
14
|
|
|
private $lists; |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
private $values; |
18
|
|
|
|
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->initiate(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array $arguments |
26
|
|
|
*/ |
27
|
|
|
public function parse(array $arguments) |
28
|
|
|
{ |
29
|
|
|
$this->initiate(); |
30
|
|
|
|
31
|
|
|
foreach ($arguments as $argument) { |
32
|
|
|
$this->addToFittingCollection($argument); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function getFlags() |
40
|
|
|
{ |
41
|
|
|
return $this->flags; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function getLists() |
48
|
|
|
{ |
49
|
|
|
return $this->lists; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function getValues() |
56
|
|
|
{ |
57
|
|
|
return $this->values; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $name |
62
|
|
|
* @param mixed $value |
63
|
|
|
*/ |
64
|
|
|
private function addToList($name, $value) |
65
|
|
|
{ |
66
|
|
|
$value = trim($value, '"'); //remove >"< if exists |
67
|
|
|
|
68
|
|
|
if (isset($this->lists[$name])) { |
69
|
|
|
$collection = $this->lists[$name]; |
70
|
|
|
} else { |
71
|
|
|
$collection = []; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$collection[] = $value; |
75
|
|
|
$this->lists[$name] = $collection; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $string |
80
|
|
|
* @param string $search |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
private function contains($string, $search) |
84
|
|
|
{ |
85
|
|
|
if (strlen($search) === 0) { |
86
|
|
|
$contains = false; |
87
|
|
|
} else { |
88
|
|
|
$contains = !(strpos($string, $search) === false); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $contains; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function handleLongNameListOrFlag($argument) |
95
|
|
|
{ |
96
|
|
|
if ($this->contains($argument, '=')) { |
97
|
|
|
$position = strpos($argument, '='); |
98
|
|
|
$name = substr($argument, 0, $position); |
99
|
|
|
$value = substr($argument, ($position + 1)); |
100
|
|
|
$this->addToList($name, $value); |
101
|
|
|
} else { |
102
|
|
|
$this->flags[] = $argument; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function handleShortNameListOrFlag($argument) |
107
|
|
|
{ |
108
|
|
|
$containsEqualCharacter = ($this->contains($argument, '=')); |
109
|
|
|
$equalCharacterIsOnSecondPosition = (strpos($argument, '=') === 1); |
110
|
|
|
$isShortNameList = ($containsEqualCharacter |
111
|
|
|
&& $equalCharacterIsOnSecondPosition); |
112
|
|
|
|
113
|
|
|
if ($isShortNameList) { |
114
|
|
|
$name = substr($argument, 0, 1); |
115
|
|
|
$value = substr($argument, 2); |
116
|
|
|
$this->addToList($name, $value); |
117
|
|
|
} else if (!$containsEqualCharacter) { |
118
|
|
|
$length = strlen($argument); |
119
|
|
|
$iterator = 0; |
120
|
|
|
while ($iterator < $length) { |
121
|
|
|
$this->flags[] = $argument{$iterator}; |
122
|
|
|
++$iterator; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function initiate() |
128
|
|
|
{ |
129
|
|
|
$this->flags = []; |
130
|
|
|
$this->lists = []; |
131
|
|
|
$this->values = []; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $argument |
136
|
|
|
*/ |
137
|
|
|
private function addToFittingCollection($argument) |
138
|
|
|
{ |
139
|
|
|
if ($this->hasLengthOf($argument, 1)) { |
140
|
|
|
$this->values[] = $argument; |
141
|
|
|
} else if ($this->startsWith($argument, '--')) { |
142
|
|
|
$argument = substr($argument, 2); |
143
|
|
|
$this->handleLongNameListOrFlag($argument); |
144
|
|
|
} else if ($this->startsWith($argument, '-')) { |
145
|
|
|
$argument = substr($argument, 1); |
146
|
|
|
$this->handleShortNameListOrFlag($argument); |
147
|
|
|
} else { |
148
|
|
|
$this->values[] = $argument; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $string |
154
|
|
|
* @param string $start |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
private function startsWith($string, $start) |
158
|
|
|
{ |
159
|
|
|
return (strncmp($string, $start, strlen($start)) === 0); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $string |
164
|
|
|
* @param int $expectedLength |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
private function hasLengthOf($string, $expectedLength) |
168
|
|
|
{ |
169
|
|
|
$length = strlen($string); |
170
|
|
|
$hasLengthOf = ($length == $expectedLength); |
171
|
|
|
|
172
|
|
|
return $hasLengthOf; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|