private function ChoiceQuestion::getDefaultValidator in Zircon Profile 8
Same name and namespace in other branches
- 8.0 vendor/symfony/console/Question/ChoiceQuestion.php \Symfony\Component\Console\Question\ChoiceQuestion::getDefaultValidator()
Returns the default answer validator.
Return value
callable
3 calls to ChoiceQuestion::getDefaultValidator()
- ChoiceQuestion::setErrorMessage in vendor/
symfony/ console/ Question/ ChoiceQuestion.php - Sets the error message for invalid values.
- ChoiceQuestion::setMultiselect in vendor/
symfony/ console/ Question/ ChoiceQuestion.php - Sets multiselect option.
- ChoiceQuestion::__construct in vendor/
symfony/ console/ Question/ ChoiceQuestion.php - Constructor.
File
- vendor/
symfony/ console/ Question/ ChoiceQuestion.php, line 115
Class
- ChoiceQuestion
- Represents a choice question.
Namespace
Symfony\Component\Console\QuestionCode
private function getDefaultValidator() {
$choices = $this->choices;
$errorMessage = $this->errorMessage;
$multiselect = $this->multiselect;
$isAssoc = $this
->isAssoc($choices);
return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
// Collapse all spaces.
$selectedChoices = str_replace(' ', '', $selected);
if ($multiselect) {
// Check for a separated comma values
if (!preg_match('/^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$/', $selectedChoices, $matches)) {
throw new \InvalidArgumentException(sprintf($errorMessage, $selected));
}
$selectedChoices = explode(',', $selectedChoices);
}
else {
$selectedChoices = array(
$selected,
);
}
$multiselectChoices = array();
foreach ($selectedChoices as $value) {
$results = array();
foreach ($choices as $key => $choice) {
if ($choice === $value) {
$results[] = $key;
}
}
if (count($results) > 1) {
throw new \InvalidArgumentException(sprintf('The provided answer is ambiguous. Value should be one of %s.', implode(' or ', $results)));
}
$result = array_search($value, $choices);
if (!$isAssoc) {
if (false !== $result) {
$result = $choices[$result];
}
elseif (isset($choices[$value])) {
$result = $choices[$value];
}
}
elseif (false === $result && isset($choices[$value])) {
$result = $value;
}
if (false === $result) {
throw new \InvalidArgumentException(sprintf($errorMessage, $value));
}
$multiselectChoices[] = (string) $result;
}
if ($multiselect) {
return $multiselectChoices;
}
return current($multiselectChoices);
};
}