Fix arguments/options validation when "IS_ARRAY" is true

master 3.3.1
Julien Rosset 2 years ago
parent 75a40b9c86
commit 98319dafb4

@ -67,20 +67,30 @@ trait TCommandWithValidation {
//region Get the argument value
$argumentValue = $input->getArgument($argumentName);
//endregion
//region Check the argument value is not Null
//region Check the argument value is not
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
//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));
}
//endregion
//region Replace the value by the one extracted from the validator
$input->setArgument($argumentName, $argumentValidator->getValue());
}
//endregion
}
}
@ -111,13 +121,23 @@ trait TCommandWithValidation {
}
//endregion
//region Check the value is valid
//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));
}
//endregion
//region Replace the value by the one extracted from the validator
$input->setOption($optionName, $optionValidator->getValue());
}
//endregion
}
}

Loading…
Cancel
Save