mirror of
https://github.com/n3w/helpers-cli-input.git
synced 2025-12-19 12:43:23 +00:00
Made getCollection(), getInput(), find(), and validate() in AbstractInputHandler final. Removed categorising input by type. Abstracted most of validate() into it's own protected method called validateInput(). Removed $skipValidation argument from bind() and relaced with $flags. Added FLAG_BIND_SKIP_VALIDATION, FLAG_VALIDATION_SKIP_REQUIRED, FLAG_VALIDATION_SKIP_CUSTOM, and FLAG_VALIDATION_SKIP_UNRECOGNISED flags. Added check in validate() to look for unrecognised options and arguments.
This commit is contained in:
parent
1100d5d959
commit
9f5c5c7d4d
1 changed files with 85 additions and 46 deletions
|
|
@ -5,15 +5,40 @@ declare(strict_types=1);
|
||||||
namespace pointybeard\Helpers\Cli\Input;
|
namespace pointybeard\Helpers\Cli\Input;
|
||||||
|
|
||||||
use pointybeard\Helpers\Functions\Flags;
|
use pointybeard\Helpers\Functions\Flags;
|
||||||
|
use pointybeard\Helpers\Functions\Debug;
|
||||||
|
|
||||||
abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface
|
abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Will skip all validation when bind() is executed. Ignores all other flags
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
const FLAG_BIND_SKIP_VALIDATION = 0x0001;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will skip the required input and required values check
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
const FLAG_VALIDATION_SKIP_REQUIRED = 0x0002;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will skip running custom validators
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
const FLAG_VALIDATION_SKIP_CUSTOM = 0x0004;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will skip checking if an input is in the collection
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
const FLAG_VALIDATION_SKIP_UNRECOGNISED = 0x0008;
|
||||||
|
|
||||||
protected $input = [];
|
protected $input = [];
|
||||||
protected $collection = null;
|
protected $collection = null;
|
||||||
|
|
||||||
abstract protected function parse(): bool;
|
abstract protected function parse(): bool;
|
||||||
|
|
||||||
public function bind(InputCollection $inputCollection, bool $skipValidation = false): bool
|
final public function bind(InputCollection $inputCollection, ?int $flags = null): bool
|
||||||
{
|
{
|
||||||
// Do the binding stuff here
|
// Do the binding stuff here
|
||||||
$this->input = [];
|
$this->input = [];
|
||||||
|
|
@ -21,8 +46,8 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface
|
||||||
|
|
||||||
$this->parse();
|
$this->parse();
|
||||||
|
|
||||||
if (true !== $skipValidation) {
|
if (!Flags\is_flag_set($flags, self::FLAG_BIND_SKIP_VALIDATION)) {
|
||||||
$this->validate();
|
$this->validate($flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -39,71 +64,85 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function validate(): void
|
protected function validateInput(AbstractInputType $input, ?int $flags) {
|
||||||
{
|
if(!Flags\is_flag_set($flags, self::FLAG_VALIDATION_SKIP_REQUIRED)) {
|
||||||
foreach ($this->collection->getItems() as $type => $items) {
|
self::checkRequiredAndRequiredValue($input, $this->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 (
|
||||||
|
null !== $input->default() &&
|
||||||
|
null === $this->find($input->name()) &&
|
||||||
|
null === $input->validator()
|
||||||
|
) {
|
||||||
|
$result = $input->default();
|
||||||
|
|
||||||
// There is a default value, input has not been set, and there
|
// Input has been set and it has a validator. Skip this if
|
||||||
// is no validator
|
// FLAG_VALIDATION_SKIP_CUSTOM is set
|
||||||
if (
|
} elseif (null !== $this->find($input->name()) && null !== $input->validator() && !Flags\is_flag_set($flags, self::FLAG_VALIDATION_SKIP_CUSTOM)) {
|
||||||
null !== $input->default() &&
|
$validator = $input->validator();
|
||||||
null === $this->find($input->name()) &&
|
|
||||||
null === $input->validator()
|
|
||||||
) {
|
|
||||||
$result = $input->default();
|
|
||||||
|
|
||||||
// Input has been set and it has a validator
|
if ($validator instanceof \Closure) {
|
||||||
} elseif (null !== $this->find($input->name()) && null !== $input->validator()) {
|
$validator = new Validator($validator);
|
||||||
$validator = $input->validator();
|
} elseif (!($validator instanceof Validator)) {
|
||||||
|
throw new \Exception("Validator for '{$input->name()}' must be NULL or an instance of either Closure or Input\Validator.");
|
||||||
if ($validator instanceof \Closure) {
|
|
||||||
$validator = new Validator($validator);
|
|
||||||
} elseif (!($validator instanceof Validator)) {
|
|
||||||
throw new \Exception("Validator for '{$input->name()}' must be NULL or an instance of either Closure or Input\Validator.");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
$result = $validator->validate($input, $this);
|
|
||||||
} catch (\Exception $ex) {
|
|
||||||
throw new Exceptions\InputValidationFailedException($input, 0, $ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
// No default, no validator, but may or may not have been set
|
|
||||||
} else {
|
|
||||||
$result = $this->find($input->name());
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->input[$input->name()] = $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = $validator->validate($input, $this);
|
||||||
|
} catch (\Exception $ex) {
|
||||||
|
throw new Exceptions\InputValidationFailedException($input, 0, $ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
// No default, no validator, but may or may not have been set
|
||||||
|
} else {
|
||||||
|
$result = $this->find($input->name());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function isInputRecognised(string $name): bool {
|
||||||
|
return null === $this->collection->find($name) ? false : true;
|
||||||
|
}
|
||||||
|
|
||||||
|
final public function validate(?int $flags = null): void
|
||||||
|
{
|
||||||
|
if(!Flags\is_flag_set($flags, self::FLAG_VALIDATION_SKIP_UNRECOGNISED)) {
|
||||||
|
foreach($this->input as $name => $value) {
|
||||||
|
if(false == static::isInputRecognised((string)$name)) {
|
||||||
|
throw new Exceptions\UnrecognisedInputException("'{$name}' is not recognised");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->collection->getItems() as $input) {
|
||||||
|
$this->input[$input->name()] = static::validateInput($input, $flags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function find(string $name)
|
final public function find(string $name)
|
||||||
{
|
{
|
||||||
if (isset($this->input[$name])) {
|
if (isset($this->input[$name])) {
|
||||||
return $this->input[$name];
|
return $this->input[$name];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the collection to see if anything responds to $name
|
// Check the collection to see if anything responds to $name
|
||||||
foreach ($this->collection->getItems() as $type => $items) {
|
foreach ($this->collection->getItems() as $item) {
|
||||||
foreach ($items as $ii) {
|
if ($item->respondsTo($name) && isset($this->input[$item->name()])) {
|
||||||
if ($ii->respondsTo($name) && isset($this->input[$ii->name()])) {
|
return $this->input[$item->name()];
|
||||||
return $this->input[$ii->name()];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getInput(): array
|
final public function getInput(): array
|
||||||
{
|
{
|
||||||
return $this->input;
|
return $this->input;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCollection(): ?InputCollection
|
final public function getCollection(): ?InputCollection
|
||||||
{
|
{
|
||||||
return $this->collection;
|
return $this->collection;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue