Fix arguments/options validation when "IS_ARRAY" is true

2.x 2.3.2
Julien Rosset 2 years ago
parent c151e8038f
commit 56e05cdc2e

@ -73,13 +73,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 (!$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
} }
} }
@ -110,13 +120,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