-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynopsis.php
149 lines (125 loc) · 4.08 KB
/
Synopsis.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
<?php
declare(strict_types=1);
namespace Snicco\Component\BetterWPCLI\Synopsis;
use InvalidArgumentException;
use function array_map;
use function array_values;
use function get_class;
use function gettype;
use function is_iterable;
use function is_object;
use function sprintf;
final class Synopsis
{
/**
* @var array<string,InputDefinition>
*/
private array $definitions = [];
private ?InputArgument $repeating_and_positional = null;
private ?InputArgument $optional_and_positional = null;
public function __construct(InputDefinition ...$definitions)
{
$this->add($definitions);
}
/**
* @return list<array<string,mixed>>
*/
public function toArray(): array
{
return array_values(
array_map(fn (InputDefinition $definition): array => $definition->toArray(), $this->definitions)
);
}
/**
* @param InputDefinition|iterable<InputDefinition> $definitions
*/
public function with($definitions): self
{
$new = clone $this;
$new->add($definitions);
return $new;
}
/**
* @interal
*
* @return array<string,InputArgument>
*
* @psalm-internal Snicco\Component\BetterWPCLI
* @psalm-mutation-free
*/
public function positionalArguments(): array
{
$positional = [];
foreach ($this->definitions as $name => $definition) {
if (! $definition instanceof InputArgument) {
continue;
}
$positional[$name] = $definition;
}
return $positional;
}
/**
* @interal
*
* @psalm-internal Snicco\Component\BetterWPCLI
* @psalm-mutation-free
*/
public function hasRepeatingPositionalArgument(): bool
{
return isset($this->repeating_and_positional);
}
/**
* @param InputDefinition|iterable<InputDefinition> $definitions
*
* @psalm-suppress DocblockTypeContradiction
*/
private function add($definitions): void
{
$definitions = is_iterable($definitions) ? $definitions : [$definitions];
foreach ($definitions as $definition) {
if (! $definition instanceof InputDefinition) {
throw new InvalidArgumentException(
sprintf(
'%s is not an instance of %s',
is_object($definition) ? get_class($definition) : gettype($definition),
InputDefinition::class
)
);
}
$name = $definition->name();
if (isset($this->definitions[$name])) {
throw new InvalidArgumentException(sprintf(
'Duplicate input name [%s] is not allowed in synopsis.',
$name
));
}
if ($definition instanceof InputArgument) {
if (null !== $this->repeating_and_positional) {
throw new InvalidArgumentException(
sprintf(
'Positional argument [%s] can not be added after repeating positional argument [%s].',
$name,
$this->repeating_and_positional->name()
)
);
}
if ($this->optional_and_positional && ! $definition->isOptional()) {
throw new InvalidArgumentException(
sprintf(
'Required argument [%s] can not be added after optional argument [%s].',
$name,
$this->optional_and_positional->name()
)
);
}
if ($definition->isRepeating()) {
$this->repeating_and_positional = $definition;
}
if ($definition->isOptional()) {
$this->optional_and_positional = $definition;
}
}
$this->definitions[$name] = $definition;
}
}
}