This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInput.php
211 lines (189 loc) · 4.91 KB
/
Input.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Command\Component\Command;
use Ulrack\Command\Common\Command\InputInterface;
use Ulrack\Command\Common\Dao\CommandConfigurationInterface;
class Input implements InputInterface
{
/**
* Contains the passed command.
*
* @var string[]
*/
private $command;
/**
* Contains the passed parameters.
*
* @var array
*/
private $parameters;
/**
* Contains the passed flags.
*
* @var array
*/
private $flags;
/**
* Contains the loaded command configuration.
*
* @var CommandConfigurationInterface
*/
private $commandConfiguration;
/**
* Constructor
*
* @param string[] $command
* @param array $parameters
* @param array $flags
*/
public function __construct(
array $command = [],
array $parameters = [],
array $flags = []
) {
$this->command = $command;
$this->parameters = $parameters;
$this->flags = $flags;
}
/**
* Loads configuration into the input.
*
* @param CommandConfigurationInterface $commandConfiguration
*
* @return void
*/
public function loadConfiguration(
CommandConfigurationInterface $commandConfiguration
): void {
$this->commandConfiguration = $commandConfiguration;
}
/**
* Checks whether the flag is set.
*
* @param string $flag
*
* @return bool
*/
public function hasParameter(string $parameter): bool
{
if (array_key_exists($parameter, $this->parameters)) {
return true;
}
if ($this->commandConfiguration !== null) {
foreach (
$this->commandConfiguration
->getParameters() as $parameterInput
) {
$long = $parameterInput['long'] ?? '';
$short = $parameterInput['short'] ?? '';
if ($long === $parameter || $short === $parameter) {
if (
array_key_exists($long, $this->parameters)
|| array_key_exists($short, $this->parameters)
) {
return true;
}
}
}
}
return false;
}
/**
* Sets the value of a parameter.
*
* @param string $parameter
* @param mixed $value
*
* @return void
*/
public function setParameter(string $parameter, $value): void
{
$this->parameters[$parameter] = $value;
}
/**
* Retrieves the parameter from the input.
*
* @param string $parameter
*
* @return mixed
*/
public function getParameter(string $parameter)
{
if (array_key_exists($parameter, $this->parameters)) {
return $this->parameters[$parameter];
}
if ($this->commandConfiguration !== null) {
foreach ($this->commandConfiguration->getParameters() as $parameterInput) {
$long = $parameterInput['long'] ?? '';
$short = $parameterInput['short'] ?? '';
if ($long === $parameter || $short === $parameter) {
if (array_key_exists($long, $this->parameters)) {
return $this->parameters[$long];
} elseif (array_key_exists($short, $this->parameters)) {
return $this->parameters[$short];
}
}
}
}
return null;
}
/**
* Checks whether the flag is set.
*
* @param string $flag
*
* @return bool
*/
public function isSetFlag(string $flag): bool
{
if (in_array($flag, $this->flags)) {
return true;
}
if ($this->commandConfiguration !== null) {
foreach ($this->commandConfiguration->getFlags() as $configFlag) {
if (
$configFlag['long'] === $flag
|| $configFlag['short'] === $flag
) {
if (
in_array($configFlag['long'], $this->flags)
|| in_array($configFlag['short'], $this->flags)
) {
return true;
}
}
}
}
return false;
}
/**
* Returns the command.
*
* @return string[]
*/
public function getCommand(): array
{
return $this->command;
}
/**
* Returns the parameters.
*
* @return array
*/
public function getParameters(): array
{
return $this->parameters;
}
/**
* Returns the flags.
*
* @return array
*/
public function getFlags(): array
{
return $this->flags;
}
}