1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_clipr\Tasks; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_input\Interfaces\IEntry; |
7
|
|
|
use Traversable; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Params |
12
|
|
|
* @package kalanis\kw_clipr\Tasks |
13
|
|
|
* Paths to accessing tasks/commands somewhere on volumes |
14
|
|
|
*/ |
15
|
|
|
class Params |
16
|
|
|
{ |
17
|
|
|
/** @var array<string, IEntry> */ |
18
|
|
|
protected array $inputs = []; |
19
|
|
|
/** @var array<string, Params\Option> */ |
20
|
|
|
protected array $available = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param array<IEntry> $inputs |
24
|
|
|
*/ |
25
|
20 |
|
public function __construct(array &$inputs) |
26
|
|
|
{ |
27
|
20 |
|
$this->inputs = array_combine(array_map([$this, 'getKey'], $inputs), $inputs); |
28
|
20 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @codeCoverageIgnore why someone would run that?! |
32
|
|
|
*/ |
33
|
|
|
private function __clone() |
34
|
|
|
{ |
35
|
|
|
} |
36
|
|
|
|
37
|
20 |
|
public function getKey(IEntry $entry): string |
38
|
|
|
{ |
39
|
20 |
|
return $entry->getKey(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $variable |
44
|
|
|
* @param string $cliKey |
45
|
|
|
* @param string|null $match |
46
|
|
|
* @param array|bool|mixed|string|null $defaultValue |
47
|
|
|
* @param string|null $short |
48
|
|
|
* @param string $desc |
49
|
|
|
* @return Params |
50
|
|
|
*/ |
51
|
20 |
|
public function addParam(string $variable, string $cliKey, ?string $match = null, $defaultValue = null, ?string $short = null, string $desc = ''): self |
52
|
|
|
{ |
53
|
20 |
|
$param = new Params\Option(); |
54
|
20 |
|
$param->setData($variable, $cliKey, $match, $defaultValue, $short, $desc); |
55
|
20 |
|
$param->setValue($this->whichVariant($param)); |
56
|
20 |
|
$this->available[$variable] = $param; |
57
|
20 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Params\Option $param |
62
|
|
|
* @return array|bool|mixed|string|null |
63
|
|
|
*/ |
64
|
20 |
|
protected function whichVariant(Params\Option $param) |
65
|
|
|
{ |
66
|
20 |
|
if (isset($this->inputs[$param->getCliKey()])) { |
67
|
8 |
|
$input = $this->inputs[$param->getCliKey()]; |
68
|
|
|
|
69
|
8 |
|
if (!is_null($param->getMatch())) { |
70
|
1 |
|
$matches = []; |
71
|
1 |
|
if (preg_match($param->getMatch(), trim($input->getValue()), $matches)) { |
72
|
1 |
|
if (1 == count($matches)) { // no submatch set |
73
|
1 |
|
return reset($matches); |
74
|
1 |
|
} elseif (2 == count($matches)) { // one submatch set |
75
|
1 |
|
reset($matches); |
76
|
1 |
|
return next($matches); |
77
|
|
|
} else { // more than one submatch |
78
|
1 |
|
return $matches; |
79
|
|
|
} |
80
|
|
|
} else { |
81
|
1 |
|
return $param->getDefaultValue(); |
82
|
|
|
} |
83
|
|
|
} else { |
84
|
8 |
|
return (is_bool($param->getDefaultValue())) ? (false === $param->getDefaultValue()) : $input->getValue(); |
85
|
|
|
} |
86
|
20 |
|
} elseif (isset($this->inputs[$param->getShort()])) { |
87
|
17 |
|
return empty($param->getDefaultValue()); |
88
|
|
|
} else { |
89
|
20 |
|
return $param->getDefaultValue(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $name |
95
|
|
|
* @return mixed|null |
96
|
|
|
*/ |
97
|
20 |
|
public function __get($name) |
98
|
|
|
{ |
99
|
20 |
|
return $this->__isset($name) ? $this->available[$name]->getValue() : null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $name |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
20 |
|
public function __isset($name): bool |
107
|
|
|
{ |
108
|
20 |
|
return isset($this->available[$name]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return Traversable<string, Params\Option> |
113
|
|
|
*/ |
114
|
4 |
|
public function getAvailableOptions(): Traversable |
115
|
|
|
{ |
116
|
4 |
|
yield from $this->available; |
117
|
4 |
|
} |
118
|
|
|
} |
119
|
|
|
|