-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathWithOperands.php
More file actions
97 lines (85 loc) · 2.17 KB
/
Copy pathWithOperands.php
File metadata and controls
97 lines (85 loc) · 2.17 KB
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
<?php
namespace GetOpt;
trait WithOperands
{
/** @var Operand[] */
protected $operands = [];
/**
* Add an array of $operands
*
* @param Operand[] $operands
* @return self
*/
public function addOperands(array $operands)
{
foreach ($operands as $operand) {
$this->addOperand($operand);
}
return $this;
}
/**
* Add an $operand
*
* @param Operand $operand
* @return self
*/
public function addOperand(Operand $operand)
{
if ($operand->isRequired()) {
foreach ($this->operands as $previousOperand) {
$previousOperand->required();
}
}
if ($this->hasOperands()) {
/** @var Operand $lastOperand */
$lastOperand = array_slice($this->operands, -1)[0];
if ($lastOperand->isMultiple()) {
throw new \InvalidArgumentException(sprintf(
'Operand %s is multiple - no more operands allowed',
$lastOperand->getName()
));
}
}
$this->operands[] = $operand;
return $this;
}
/**
* Returns the list of operands.
*
* @return Operand[]
*/
public function getOperands(): array
{
return $this->operands;
}
/**
* Returns the nth operand (starting with 0), or null if it does not exist.
*
* When $index is a string it returns the current value or the default value for the named operand.
*
* @param int|string $index
* @return Operand
*/
public function getOperand($index): ?Operand
{
if (is_string($index)) {
$name = $index;
foreach ($this->operands as $operand) {
if ($operand->getName() === $name) {
return $operand;
}
}
return null;
}
return isset($this->operands[$index]) ? $this->operands[$index] : null;
}
/**
* Check if operands are defined
*
* @return bool
*/
public function hasOperands(): bool
{
return !empty($this->operands);
}
}