{'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] ?? null; } 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; } }