11
0
Fork 0
mirror of https://github.com/n3w/helpers-cli-input.git synced 2025-12-19 12:43:23 +00:00

Minor improvement to logic in AbstractInputHandler::validateInput(). Ensures that an input with a validator, but with a default value and no user suplied input, will have the default value used.

This commit is contained in:
Alannah Kearney 2019-08-06 09:02:03 +10:00
commit 326a979302

View file

@ -68,17 +68,17 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface
if(!Flags\is_flag_set($flags, self::FLAG_VALIDATION_SKIP_REQUIRED)) { if(!Flags\is_flag_set($flags, self::FLAG_VALIDATION_SKIP_REQUIRED)) {
self::checkRequiredAndRequiredValue($input, $this->input); self::checkRequiredAndRequiredValue($input, $this->input);
} }
// There is a default value, input has not been set, and there // There is a default value and input has not been set. Assign the
// is no validator // default value to the result.
if ( if (
null !== $input->default() && null !== $input->default() &&
null === $this->find($input->name()) && null === $this->find($input->name())
null === $input->validator()
) { ) {
$result = $input->default(); $result = $input->default();
// Input has been set and it has a validator. Skip this if // Input has been set AND it has a validator. Run the validator over the
// FLAG_VALIDATION_SKIP_CUSTOM is set // input. Note, this will be skipped if FLAG_VALIDATION_SKIP_CUSTOM is
// set
} elseif (null !== $this->find($input->name()) && null !== $input->validator() && !Flags\is_flag_set($flags, self::FLAG_VALIDATION_SKIP_CUSTOM)) { } elseif (null !== $this->find($input->name()) && null !== $input->validator() && !Flags\is_flag_set($flags, self::FLAG_VALIDATION_SKIP_CUSTOM)) {
$validator = $input->validator(); $validator = $input->validator();
@ -94,7 +94,8 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface
throw new Exceptions\InputValidationFailedException($input, 0, $ex); throw new Exceptions\InputValidationFailedException($input, 0, $ex);
} }
// No default, no validator, but may or may not have been set // No default, but may or may not have been set so assign whatever value
// it might have to the result
} else { } else {
$result = $this->find($input->name()); $result = $this->find($input->name());
} }