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 //region Get the argument value
$argumentValue = $input->getArgument($argumentName); $argumentValue = $input->getArgument($argumentName);
//endregion //endregion
//region Check the argument value is not Null //region Check the argument value is not
if ($argumentValue === null) { if ($argumentValue === null) {
// If the value is strictly Null (the default value), don't check it → skip // If the value is strictly Null (the default value), don't check it → skip
continue; continue;
} }
//endregion //endregion
//region Check the value is valid //region Check the value is valid and replace it by the one extracted from the validator
if (!$argumentValidator->validate($argumentValue)) { if ($this->getDefinition()->getArgument($argumentName)->isArray() && is_array($argumentValue)) {
throw new InvalidValueException(sprintf('The "%s" argument has not a valid value', $argumentName)); // 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 //endregion
} }
} }
@ -111,13 +121,23 @@ trait TCommandWithValidation {
} }
//endregion //endregion
//region Check the value is valid //region Check the value is valid and replace it by the one extracted from the validator
if (!$optionValidator->validate($optionValue)) { if ($this->getDefinition()->getOption($optionName)->isArray() && is_array($optionValue)) {
throw new InvalidValueException(sprintf('The "--%s" option has not a valid value', $optionName)); // 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 //endregion
} }
} }

Loading…
Cancel
Save