Validator : do not check Null or False (default value / not provided)

master 3.0.2
Julien Rosset 2 years ago
parent f69c7c8fa4
commit ede43fb01c

@ -59,12 +59,29 @@ trait TCommandWithValidation {
*/ */
protected function validateArguments (InputInterface $input, OutputInterface $output): void { protected function validateArguments (InputInterface $input, OutputInterface $output): void {
foreach ($this->argumentsValidator as $argumentName => $argumentValidator) { foreach ($this->argumentsValidator as $argumentName => $argumentValidator) {
if ($input->hasArgument($argumentName)) { //region Check the argument still exists
if (!$argumentValidator->validate($input->getArgument($argumentName))) { if (!$input->hasArgument($argumentName)) {
continue;
}
//endregion
//region Get the argument value
$argumentValue = $input->getArgument($argumentName);
//endregion
//region Check the argument value is not Null
if ($argumentValue === null) {
// If the value is strictly Null (the default value), don't check it → skip
continue;
}
//endregion
//region Check the value is valid
if (!$argumentValidator->validate($argumentValue)) {
throw new InvalidValueException(sprintf('The "%s" argument has not a valid value', $argumentName)); throw new InvalidValueException(sprintf('The "%s" argument has not a valid value', $argumentName));
} }
//endregion
//region Replace the value by the one extracted from the validator
$input->setArgument($argumentName, $argumentValidator->getValue()); $input->setArgument($argumentName, $argumentValidator->getValue());
} //endregion
} }
} }
/** /**
@ -79,12 +96,29 @@ trait TCommandWithValidation {
*/ */
protected function validateOptions (InputInterface $input, OutputInterface $output): void { protected function validateOptions (InputInterface $input, OutputInterface $output): void {
foreach ($this->optionsValidator as $optionName => $optionValidator) { foreach ($this->optionsValidator as $optionName => $optionValidator) {
if ($input->hasOption($optionName)) { //region Check the option still exists
if (!$optionValidator->validate($input->getOption($optionName))) { if (!$input->hasOption($optionName)) {
continue;
}
//endregion
//region Get and check the option value
$optionValue = $input->getOption($optionName);
//endregion
//region Check the option value is NOT Null or False
if ($optionValue === null || $optionValue === false) {
// If the value is strictly Null (the default value or not provided) or False (the default value), don't check it → skip
continue;
}
//endregion
//region Check the value is valid
if (!$optionValidator->validate($optionValue)) {
throw new InvalidValueException(sprintf('The "--%s" option has not a valid value', $optionName)); throw new InvalidValueException(sprintf('The "--%s" option has not a valid value', $optionName));
} }
//endregion
//region Replace the value by the one extracted from the validator
$input->setOption($optionName, $optionValidator->getValue()); $input->setOption($optionName, $optionValidator->getValue());
} //endregion
} }
} }

@ -13,6 +13,7 @@ use jrosset\Tests\Lang;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class Hello extends CommandWithValidation { class Hello extends CommandWithValidation {
@ -45,15 +46,15 @@ class Hello extends CommandWithValidation {
$this->addOption( $this->addOption(
'lang', 'lang',
'l', 'l',
InputArgument::OPTIONAL, InputOption::VALUE_REQUIRED,
'The lang', 'The lang',
Lang::English, null,
new EnumValidator(Lang::class) new EnumValidator(Lang::class)
); );
$this->addOption( $this->addOption(
'repeat', 'repeat',
'r', 'r',
InputArgument::OPTIONAL, InputOption::VALUE_OPTIONAL,
'The number of repeat', 'The number of repeat',
1, 1,
new IntegerValidator(1, null) new IntegerValidator(1, null)
@ -66,7 +67,7 @@ class Hello extends CommandWithValidation {
protected function execute (InputInterface $input, OutputInterface $output): int { protected function execute (InputInterface $input, OutputInterface $output): int {
$output->writeln('Command : ' . __CLASS__, OutputInterface::VERBOSITY_DEBUG); $output->writeln('Command : ' . __CLASS__, OutputInterface::VERBOSITY_DEBUG);
$text = $input->getOption('lang')->value; $text = ($input->getOption('lang') ?? Lang::English)->value;
$repeat = $input->getOption('repeat'); $repeat = $input->getOption('repeat');
/** @var DateTimeInterface $day */ /** @var DateTimeInterface $day */

Loading…
Cancel
Save