From 56e05cdc2ed3b1f5732ffd0272d7fa578029545c Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Sun, 24 Sep 2023 16:43:30 +0200 Subject: [PATCH] Fix arguments/options validation when "IS_ARRAY" is true --- .../Validation/TCommandWithValidation.php | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/CliProgram/Validation/TCommandWithValidation.php b/src/CliProgram/Validation/TCommandWithValidation.php index d6fb186..e5bb0f6 100644 --- a/src/CliProgram/Validation/TCommandWithValidation.php +++ b/src/CliProgram/Validation/TCommandWithValidation.php @@ -73,13 +73,23 @@ trait TCommandWithValidation { } //endregion - //region Check the value is valid - if (!$argumentValidator->validate($argumentValue)) { - throw new InvalidValueException(sprintf('The "%s" argument has not a valid value', $argumentName)); + //region Check the value is valid and replace it by the one extracted from the validator + if ($this->getDefinition()->getArgument($argumentName)->isArray() && is_array($argumentValue)) { + // This is an “array” argument → check and replace each value instead of the array itself + foreach ($argumentValue as &$argumentValueEntry) { + if (!$argumentValidator->validate($argumentValueEntry)) { + throw new InvalidValueException(sprintf('The "%s" argument has not a valid value', $argumentName)); + } + $argumentValueEntry = $argumentValidator->getValue(); + } + $input->setArgument($argumentName, $argumentValue); + } + else { + if (!$argumentValidator->validate($argumentValue)) { + throw new InvalidValueException(sprintf('The "%s" argument has not a valid value', $argumentName)); + } + $input->setArgument($argumentName, $argumentValidator->getValue()); } - //endregion - //region Replace the value by the one extracted from the validator - $input->setArgument($argumentName, $argumentValidator->getValue()); //endregion } } @@ -110,13 +120,23 @@ trait TCommandWithValidation { } //endregion - //region Check the value is valid - if (!$optionValidator->validate($optionValue)) { - throw new InvalidValueException(sprintf('The "--%s" option has not a valid value', $optionName)); + //region Check the value is valid and replace it by the one extracted from the validator + if ($this->getDefinition()->getOption($optionName)->isArray() && is_array($optionValue)) { + // This is an “array” argument → check and replace each value instead of the array itself + foreach ($optionValue as &$optionValueEntry) { + if (!$optionValidator->validate($optionValueEntry)) { + throw new InvalidValueException(sprintf('The "--%s" option has not a valid value', $optionName)); + } + $optionValueEntry = $optionValidator->getValue(); + } + $input->setArgument($optionName, $optionValue); + } + else { + if (!$optionValidator->validate($optionValue)) { + throw new InvalidValueException(sprintf('The "--%s" option has not a valid value', $optionName)); + } + $input->setOption($optionName, $optionValidator->getValue()); } - //endregion - //region Replace the value by the one extracted from the validator - $input->setOption($optionName, $optionValidator->getValue()); //endregion } }