From ede43fb01c8ff7bf4ce8c586a8baee3349ab50ef Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Thu, 13 Jul 2023 17:42:48 +0200 Subject: [PATCH] Validator : do not check Null or False (default value / not provided) --- .../Validation/TCommandWithValidation.php | 54 +++++++++++++++---- tests/Commands/Hello.php | 9 ++-- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/CliProgram/Validation/TCommandWithValidation.php b/src/CliProgram/Validation/TCommandWithValidation.php index f4bfafd..07a8339 100644 --- a/src/CliProgram/Validation/TCommandWithValidation.php +++ b/src/CliProgram/Validation/TCommandWithValidation.php @@ -59,12 +59,29 @@ trait TCommandWithValidation { */ protected function validateArguments (InputInterface $input, OutputInterface $output): void { foreach ($this->argumentsValidator as $argumentName => $argumentValidator) { - if ($input->hasArgument($argumentName)) { - if (!$argumentValidator->validate($input->getArgument($argumentName))) { - throw new InvalidValueException(sprintf('The "%s" argument has not a valid value', $argumentName)); - } - $input->setArgument($argumentName, $argumentValidator->getValue()); + //region Check the argument still exists + 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)); + } + //endregion + //region Replace the value by the one extracted from the validator + $input->setArgument($argumentName, $argumentValidator->getValue()); + //endregion } } /** @@ -79,12 +96,29 @@ trait TCommandWithValidation { */ protected function validateOptions (InputInterface $input, OutputInterface $output): void { foreach ($this->optionsValidator as $optionName => $optionValidator) { - if ($input->hasOption($optionName)) { - if (!$optionValidator->validate($input->getOption($optionName))) { - throw new InvalidValueException(sprintf('The "--%s" option has not a valid value', $optionName)); - } - $input->setOption($optionName, $optionValidator->getValue()); + //region Check the option still exists + 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)); } + //endregion + //region Replace the value by the one extracted from the validator + $input->setOption($optionName, $optionValidator->getValue()); + //endregion } } diff --git a/tests/Commands/Hello.php b/tests/Commands/Hello.php index 4129fad..bd90823 100644 --- a/tests/Commands/Hello.php +++ b/tests/Commands/Hello.php @@ -13,6 +13,7 @@ use jrosset\Tests\Lang; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class Hello extends CommandWithValidation { @@ -45,15 +46,15 @@ class Hello extends CommandWithValidation { $this->addOption( 'lang', 'l', - InputArgument::OPTIONAL, + InputOption::VALUE_REQUIRED, 'The lang', - Lang::English, + null, new EnumValidator(Lang::class) ); $this->addOption( 'repeat', 'r', - InputArgument::OPTIONAL, + InputOption::VALUE_OPTIONAL, 'The number of repeat', 1, new IntegerValidator(1, null) @@ -66,7 +67,7 @@ class Hello extends CommandWithValidation { protected function execute (InputInterface $input, OutputInterface $output): int { $output->writeln('Command : ' . __CLASS__, OutputInterface::VERBOSITY_DEBUG); - $text = $input->getOption('lang')->value; + $text = ($input->getOption('lang') ?? Lang::English)->value; $repeat = $input->getOption('repeat'); /** @var DateTimeInterface $day */