1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace clearice\argparser; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class Validator implements ValidatorInterface |
7
|
|
|
{ |
8
|
|
|
const VALID_OPTION_KEYS = ['name', 'short_name', 'type', 'help', 'repeats', 'default', 'value', 'required']; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @param $options |
12
|
|
|
* @param $parsed |
13
|
|
|
* @throws InvalidArgumentException |
14
|
|
|
*/ |
15
|
3 |
|
public function validateArguments($options, $parsed) |
16
|
|
|
{ |
17
|
3 |
|
$required = []; |
18
|
3 |
|
foreach($options as $option) { |
19
|
3 |
|
if(isset($option['required']) && $option['required'] && !isset($parsed[$option['name']]) && $option['command'] == ($parsed['__command'] ?? '')) { |
20
|
|
|
$required[] = $option['name']; |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
3 |
|
if(!empty(($required))) { |
25
|
|
|
throw new InvalidArgumentException( |
26
|
|
|
sprintf( |
27
|
|
|
"Values for the following options are required%s: %s.\nPass the --help option for more information about possible options.", |
28
|
|
|
isset($parsed['__command']) ? " for the {$parsed['__command']} command" : "", implode(",", $required) |
29
|
|
|
) |
30
|
|
|
); |
31
|
|
|
} |
32
|
3 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $option |
36
|
|
|
* @param $commands |
37
|
|
|
* @throws InvalidArgumentDescriptionException |
38
|
|
|
* @throws UnknownCommandException |
39
|
|
|
*/ |
40
|
8 |
|
public function validateOption($option, $commands) |
41
|
|
|
{ |
42
|
8 |
|
if (!isset($option['name']) || !isset($option['short_name'])) { |
43
|
3 |
|
throw new InvalidArgumentDescriptionException("An option must have either a name, a short_name or both."); |
44
|
|
|
} |
45
|
6 |
|
$name = $option['name'] ?? $option['short_name']; |
46
|
6 |
|
if(isset($option['default']) && isset($option['required'])) { |
47
|
|
|
throw new InvalidArgumentDescriptionException("A required option, {$name} cannot have a default value."); |
48
|
|
|
} |
49
|
6 |
|
foreach($option as $key => $value) { |
50
|
6 |
|
if(!in_array($key, self::VALID_OPTION_KEYS)) { |
51
|
|
|
throw new InvalidArgumentDescriptionException("Invalid key [$key] in option description for [{$name}]"); |
52
|
|
|
} |
53
|
|
|
} |
54
|
6 |
|
if (isset($option['command']) && !isset($commands[$option['command']])) { |
55
|
|
|
throw new UnknownCommandException("The command '{$option['command']}' has not been added to this parser."); |
56
|
|
|
} |
57
|
6 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $command |
61
|
|
|
* @param $commands |
62
|
|
|
* @throws CommandExistsException |
63
|
|
|
* @throws InvalidArgumentDescriptionException |
64
|
|
|
*/ |
65
|
|
|
public function validateCommand($command, $commands) |
66
|
|
|
{ |
67
|
|
|
if (!isset($command['name'])) { |
68
|
|
|
throw new InvalidArgumentDescriptionException("Command description must contain a name"); |
69
|
|
|
} |
70
|
|
|
if (isset($commands[$command['name']])) { |
71
|
|
|
throw new CommandExistsException("Command ${command['name']} already exists."); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |