mirror of
https://github.com/n3w/helpers-cli-input.git
synced 2025-12-19 12:43:23 +00:00
Initial commit
This commit is contained in:
commit
0620d00f08
24 changed files with 1054 additions and 0 deletions
119
src/Input/InputCollection.php
Normal file
119
src/Input/InputCollection.php
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
<?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
|
||||
{
|
||||
return $this->arguments[$index];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue