diff --git a/example/example.php b/example/example.php index 6f2ac71..77feffa 100644 --- a/example/example.php +++ b/example/example.php @@ -23,7 +23,7 @@ $collection = (new Input\InputCollection()) ->validator(new Input\Validator( function (Input\AbstractInputType $input, Input\AbstractInputHandler $context) { // Make sure verbosity level never goes above 3 - return min(3, (int)$context->find('v')); + return min(3, (int) $context->find('v')); } )) ) @@ -57,10 +57,10 @@ $collection = (new Input\InputCollection()) // Get the supplied input. Passing the collection will make the handler bind values // and validate the input according to our collection -try{ +try { $argv = Input\InputHandlerFactory::build('Argv', $collection); -} catch(\Exception $ex) { - echo "Error when attempting to bind values to collection. Returned: " . $ex->getMessage() . PHP_EOL; +} catch (\Exception $ex) { + echo 'Error when attempting to bind values to collection. Returned: '.$ex->getMessage().PHP_EOL; exit; } @@ -73,7 +73,7 @@ echo Cli\manpage( Colour::FG_GREEN, Colour::FG_WHITE, [ - 'Examples' => 'php -f example/example.php -- -vvv -d example/example.json import' + 'Examples' => 'php -f example/example.php -- -vvv -d example/example.json import', ] ).PHP_EOL.PHP_EOL; @@ -92,7 +92,6 @@ echo Cli\manpage( // Examples: // php -f example/example.php -- -vvvs -d example/example.json import - var_dump($argv->find('action')); // string(6) "import" diff --git a/src/Input/AbstractInputHandler.php b/src/Input/AbstractInputHandler.php index cb1a799..65dbeac 100644 --- a/src/Input/AbstractInputHandler.php +++ b/src/Input/AbstractInputHandler.php @@ -42,12 +42,12 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface public function validate(): void { foreach ($this->collection->getItems() as $type => $items) { - foreach($items as $input) { + foreach ($items as $input) { self::checkRequiredAndRequiredValue($input, $this->input); // There is a default value, input has not been set, and there // is no validator - if( + if ( null !== $input->default() && null === $this->find($input->name()) && null === $input->validator() @@ -55,7 +55,7 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface $result = $input->default(); // Input has been set and it has a validator - } elseif(null !== $this->find($input->name()) && null !== $input->validator()) { + } elseif (null !== $this->find($input->name()) && null !== $input->validator()) { $validator = $input->validator(); if ($validator instanceof \Closure) { @@ -66,11 +66,11 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface try { $result = $validator->validate($input, $this); - } catch(\Exception $ex) { + } catch (\Exception $ex) { throw new Exceptions\InputValidationFailedException($input, 0, $ex); } - // No default, no validator, but may or may not have been set + // No default, no validator, but may or may not have been set } else { $result = $this->find($input->name()); } @@ -82,14 +82,14 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface public function find(string $name) { - if(isset($this->input[$name])) { + if (isset($this->input[$name])) { return $this->input[$name]; } // Check the collection to see if anything responds to $name - foreach($this->collection->getItems() as $type => $items) { - foreach($items as $ii) { - if($ii->respondsTo($name) && isset($this->input[$ii->name()])) { + foreach ($this->collection->getItems() as $type => $items) { + foreach ($items as $ii) { + if ($ii->respondsTo($name) && isset($this->input[$ii->name()])) { return $this->input[$ii->name()]; } } diff --git a/src/Input/AbstractInputType.php b/src/Input/AbstractInputType.php index be3999c..0d5f1af 100644 --- a/src/Input/AbstractInputType.php +++ b/src/Input/AbstractInputType.php @@ -25,7 +25,7 @@ abstract class AbstractInputType implements Interfaces\InputTypeInterface $this->default = $default; } - public function __call($name, array $args=[]) + public function __call($name, array $args = []) { if (empty($args)) { return $this->$name; @@ -43,7 +43,7 @@ abstract class AbstractInputType implements Interfaces\InputTypeInterface public function respondsTo(string $name): bool { - return ($name == $this->name); + return $name == $this->name; } public function getType(): string diff --git a/src/Input/Exceptions/InputValidationFailedException.php b/src/Input/Exceptions/InputValidationFailedException.php index fe3fe81..6c3414c 100644 --- a/src/Input/Exceptions/InputValidationFailedException.php +++ b/src/Input/Exceptions/InputValidationFailedException.php @@ -3,12 +3,13 @@ declare(strict_types=1); namespace pointybeard\Helpers\Cli\Input\Exceptions; + use pointybeard\Helpers\Cli\Input\AbstractInputType; class InputValidationFailedException extends \Exception { public function __construct(AbstractInputType $input, $code = 0, \Exception $previous = null) { - return parent::__construct(sprintf("Validation failed for %s. Returned: %s", $input->getDisplayName(), $previous->getMessage()), $code, $previous); + return parent::__construct(sprintf('Validation failed for %s. Returned: %s', $input->getDisplayName(), $previous->getMessage()), $code, $previous); } } diff --git a/src/Input/Exceptions/RequiredInputMissingValueException.php b/src/Input/Exceptions/RequiredInputMissingValueException.php index 03f97a8..62be79b 100644 --- a/src/Input/Exceptions/RequiredInputMissingValueException.php +++ b/src/Input/Exceptions/RequiredInputMissingValueException.php @@ -18,7 +18,6 @@ class RequiredInputMissingValueException extends \Exception 'a value is required for %s', $input->getDisplayName() ), $code, $previous); - } public function getInput(): Input\AbstractInputType diff --git a/src/Input/Handlers/Argv.php b/src/Input/Handlers/Argv.php index 536284c..e97bea9 100644 --- a/src/Input/Handlers/Argv.php +++ b/src/Input/Handlers/Argv.php @@ -148,7 +148,7 @@ class Argv extends Input\AbstractInputHandler ? $a->name() : $argumentCount ] = $token; - $argumentCount++; + ++$argumentCount; break; } $it->next(); diff --git a/src/Input/InputCollection.php b/src/Input/InputCollection.php index bf45252..744de0b 100644 --- a/src/Input/InputCollection.php +++ b/src/Input/InputCollection.php @@ -30,42 +30,45 @@ class InputCollection return $this; } - public function find(string $name, array $restrictToType=null, array $excludeType=null, &$type = null, &$index = null): ?AbstractInputType + public function find(string $name, array $restrictToType = null, array $excludeType = null, &$type = null, &$index = null): ?AbstractInputType { - foreach($this->items as $type => $items) { - + foreach ($this->items as $type => $items) { // Check if we're restricting to or excluding specific types - if(null !== $restrictToType && !in_array($type, $restrictToType)) { + if (null !== $restrictToType && !in_array($type, $restrictToType)) { continue; - - } elseif(null !== $excludeType && in_array($type, $excludeType)) { + } elseif (null !== $excludeType && in_array($type, $excludeType)) { continue; } - foreach($items as $index => $item) { - if($item->respondsTo($name)) { + foreach ($items as $index => $item) { + if ($item->respondsTo($name)) { return $item; } } } $type = null; $index = null; + return null; } - public function getTypes(): array { + public function getTypes(): array + { return array_keys($this->items); } - public function getItems(): array { + public function getItems(): array + { return $this->items; } - public function getItemsByType(string $type): array { + public function getItemsByType(string $type): array + { return $this->items[$type] ?? []; } - public function getItemByIndex(string $type, int $index): ?AbstractInputType { + public function getItemByIndex(string $type, int $index): ?AbstractInputType + { return $this->items[$type][$index] ?? null; } @@ -74,8 +77,8 @@ class InputCollection $items = []; foreach ($collections as $c) { - foreach($c->items() as $type => $items) { - foreach($items as $item) { + foreach ($c->items() as $type => $items) { + foreach ($items as $item) { $items[] = $item; } } diff --git a/src/Input/InputTypeFactory.php b/src/Input/InputTypeFactory.php index 6d942dc..dda8115 100644 --- a/src/Input/InputTypeFactory.php +++ b/src/Input/InputTypeFactory.php @@ -4,13 +4,12 @@ declare(strict_types=1); namespace pointybeard\Helpers\Cli\Input; -use pointybeard\Helpers\Functions\Flags; use pointybeard\Helpers\Foundation\Factory; final class InputTypeFactory extends Factory\AbstractFactory { use Factory\Traits\hasSimpleFactoryBuildMethodTrait; - + public static function getTemplateNamespace(): string { return __NAMESPACE__.'\\Types\\%s'; diff --git a/src/Input/Interfaces/InputTypeInterface.php b/src/Input/Interfaces/InputTypeInterface.php index 92efb34..3a6bc13 100644 --- a/src/Input/Interfaces/InputTypeInterface.php +++ b/src/Input/Interfaces/InputTypeInterface.php @@ -16,7 +16,10 @@ interface InputTypeInterface const FLAG_TYPE_INCREMENTING = 0x0400; public function getType(): string; + public function respondsTo(string $name): bool; + public function __toString(): string; + public function getDisplayName(): string; } diff --git a/src/Input/Types/Argument.php b/src/Input/Types/Argument.php index 21a9a5d..52faec1 100644 --- a/src/Input/Types/Argument.php +++ b/src/Input/Types/Argument.php @@ -10,10 +10,9 @@ use pointybeard\Helpers\Functions\Cli; class Argument extends Input\AbstractInputType { - - public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default=null) + public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default = null) { - if(null === $validator) { + if (null === $validator) { $validator = function (Input\AbstractInputType $input, Input\AbstractInputHandler $context) { // This dummy validator is necessary otherwise the argument // value is ALWAYS set to default (most often NULL) regardless @@ -25,7 +24,8 @@ class Argument extends Input\AbstractInputType parent::__construct($name, $flags, $description, $validator, $default); } - public function getDisplayName(): string { + public function getDisplayName(): string + { return strtoupper($this->name()); } diff --git a/src/Input/Types/Flag.php b/src/Input/Types/Flag.php index fba5f55..a3f2329 100644 --- a/src/Input/Types/Flag.php +++ b/src/Input/Types/Flag.php @@ -5,16 +5,13 @@ declare(strict_types=1); namespace pointybeard\Helpers\Cli\Input\Types; use pointybeard\Helpers\Functions\Flags; -use pointybeard\Helpers\Functions\Strings; -use pointybeard\Helpers\Functions\Cli; -use pointybeard\Helpers\Cli\Input; class Flag extends Option { public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default = false) { - if(Flags\is_flag_set($flags, self::FLAG_VALUE_REQUIRED) || Flags\is_flag_set($flags, self::FLAG_VALUE_OPTIONAL)) { - throw new \Exception("The flags FLAG_VALUE_REQUIRED and FLAG_VALUE_OPTIONAL cannot be used on an input of type Flag"); + if (Flags\is_flag_set($flags, self::FLAG_VALUE_REQUIRED) || Flags\is_flag_set($flags, self::FLAG_VALUE_OPTIONAL)) { + throw new \Exception('The flags FLAG_VALUE_REQUIRED and FLAG_VALUE_OPTIONAL cannot be used on an input of type Flag'); } parent::__construct($name, null, $flags, $description, $validator, $default); } diff --git a/src/Input/Types/IncrementingFlag.php b/src/Input/Types/IncrementingFlag.php index 33df708..8d850a1 100644 --- a/src/Input/Types/IncrementingFlag.php +++ b/src/Input/Types/IncrementingFlag.php @@ -5,15 +5,12 @@ declare(strict_types=1); namespace pointybeard\Helpers\Cli\Input\Types; use pointybeard\Helpers\Functions\Flags; -use pointybeard\Helpers\Functions\Strings; -use pointybeard\Helpers\Functions\Cli; -use pointybeard\Helpers\Cli\Input; class IncrementingFlag extends Flag { public function __construct(string $name = null, int $flags = null, string $description = null, object $validator = null, $default = 0) { - if(Flags\is_flag_set($flags, self::FLAG_TYPE_INCREMENTING)) { + if (Flags\is_flag_set($flags, self::FLAG_TYPE_INCREMENTING)) { $flags = $flags | self::FLAG_TYPE_INCREMENTING; } parent::__construct($name, null, $flags, $description, $validator, $default); diff --git a/src/Input/Types/LongOption.php b/src/Input/Types/LongOption.php index 7d9af5b..5961768 100644 --- a/src/Input/Types/LongOption.php +++ b/src/Input/Types/LongOption.php @@ -21,18 +21,18 @@ class LongOption extends Input\AbstractInputType public function respondsTo(string $name): bool { - return ($name == $this->name || $name == $this->short); + return $name == $this->name || $name == $this->short; } - public function getDisplayName(): string { - + public function getDisplayName(): string + { $short = null !== $this->short() ? '-'.$this->short().', ' : null ; - return sprintf("%s--%s", $short, $this->name()); + return sprintf('%s--%s', $short, $this->name()); } public function __toString(): string diff --git a/src/Input/Types/Option.php b/src/Input/Types/Option.php index 2a193e3..ab081ab 100644 --- a/src/Input/Types/Option.php +++ b/src/Input/Types/Option.php @@ -4,14 +4,14 @@ declare(strict_types=1); namespace pointybeard\Helpers\Cli\Input\Types; -use pointybeard\Helpers\Functions\Flags; use pointybeard\Helpers\Functions\Strings; use pointybeard\Helpers\Functions\Cli; use pointybeard\Helpers\Cli\Input; class Option extends Input\AbstractInputType { - public function getDisplayName(): string { + public function getDisplayName(): string + { return '-'.$this->name(); }