mirror of
https://github.com/n3w/helpers-cli-input.git
synced 2025-12-19 12:43:23 +00:00
Code tidy
This commit is contained in:
parent
866a2e1c9e
commit
419a4a9f79
14 changed files with 53 additions and 55 deletions
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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()];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ class RequiredInputMissingValueException extends \Exception
|
|||
'a value is required for %s',
|
||||
$input->getDisplayName()
|
||||
), $code, $previous);
|
||||
|
||||
}
|
||||
|
||||
public function getInput(): Input\AbstractInputType
|
||||
|
|
|
|||
|
|
@ -148,7 +148,7 @@ class Argv extends Input\AbstractInputHandler
|
|||
? $a->name()
|
||||
: $argumentCount
|
||||
] = $token;
|
||||
$argumentCount++;
|
||||
++$argumentCount;
|
||||
break;
|
||||
}
|
||||
$it->next();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ 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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue