2019-05-20 15:08:41 +10:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace pointybeard\Helpers\Cli\Input;
|
|
|
|
|
|
|
|
|
|
class InputCollection
|
|
|
|
|
{
|
|
|
|
|
private $arguments = [];
|
|
|
|
|
private $options = [];
|
|
|
|
|
|
|
|
|
|
// Prevents the class from being instanciated
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function append(Interfaces\InputTypeInterface $input, bool $replace = false): self
|
|
|
|
|
{
|
|
|
|
|
$class = new \ReflectionClass($input);
|
|
|
|
|
$this->{'append'.$class->getShortName()}($input, $replace);
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function findArgument(string $name, ?int &$index = null): ?AbstractInputType
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->arguments as $index => $a) {
|
|
|
|
|
if ($a->name() == $name) {
|
|
|
|
|
return $a;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$index = null;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function findOption(string $name, ?int &$index = null): ?AbstractInputType
|
|
|
|
|
{
|
|
|
|
|
$type = 1 == strlen($name) ? 'name' : 'long';
|
|
|
|
|
|
|
|
|
|
foreach ($this->options as $index => $o) {
|
|
|
|
|
if ($o->$type() == $name) {
|
|
|
|
|
return $o;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$index = null;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function appendArgument(Interfaces\InputTypeInterface $argument, bool $replace = false): void
|
|
|
|
|
{
|
|
|
|
|
if (null !== $this->findArgument($argument->name(), $index) && !$replace) {
|
|
|
|
|
throw new \Exception("Argument {$argument->name()} already exists in collection");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (true == $replace && null !== $index) {
|
|
|
|
|
$this->arguments[$index] = $argument;
|
|
|
|
|
} else {
|
|
|
|
|
$this->arguments[] = $argument;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function appendOption(Interfaces\InputTypeInterface $option, bool $replace = false): void
|
|
|
|
|
{
|
|
|
|
|
if (null !== $this->findOption($option->name(), $index) && !$replace) {
|
|
|
|
|
throw new \Exception("Option -{$option->name()} already exists in collection");
|
|
|
|
|
}
|
|
|
|
|
if (true == $replace && null !== $index) {
|
|
|
|
|
$this->options[$index] = $option;
|
|
|
|
|
} else {
|
|
|
|
|
$this->options[] = $option;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getArgumentsByIndex(int $index): ?AbstractInputType
|
|
|
|
|
{
|
2019-05-23 23:44:24 +10:00
|
|
|
return $this->arguments[$index] ?? null;
|
2019-05-20 15:08:41 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getArguments(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->arguments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getOptions(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function merge(self ...$collections): self
|
|
|
|
|
{
|
|
|
|
|
$arguments = [];
|
|
|
|
|
$options = [];
|
|
|
|
|
|
|
|
|
|
foreach ($collections as $c) {
|
|
|
|
|
$arguments = array_merge($arguments, $c->getArguments());
|
|
|
|
|
$options = array_merge($options, $c->getOptions());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$mergedCollection = new self();
|
|
|
|
|
|
|
|
|
|
$it = new \AppendIterator();
|
|
|
|
|
$it->append(new \ArrayIterator($arguments));
|
|
|
|
|
$it->append(new \ArrayIterator($options));
|
|
|
|
|
|
|
|
|
|
foreach ($it as $input) {
|
|
|
|
|
try {
|
|
|
|
|
$mergedCollection->append($input, true);
|
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
|
// Already exists, so skip it.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $mergedCollection;
|
|
|
|
|
}
|
|
|
|
|
}
|