Upgrade framework
This commit is contained in:
@@ -20,19 +20,17 @@ use Symfony\Component\Console\Exception\InvalidArgumentException;
|
||||
*/
|
||||
class ChoiceQuestion extends Question
|
||||
{
|
||||
private $choices;
|
||||
private $multiselect = false;
|
||||
private $prompt = ' > ';
|
||||
private $errorMessage = 'Value "%s" is invalid';
|
||||
private array $choices;
|
||||
private bool $multiselect = false;
|
||||
private string $prompt = ' > ';
|
||||
private string $errorMessage = 'Value "%s" is invalid';
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $question The question to ask to the user
|
||||
* @param array $choices The list of available choices
|
||||
* @param mixed $default The default answer to return
|
||||
*/
|
||||
public function __construct($question, array $choices, $default = null)
|
||||
public function __construct(string $question, array $choices, mixed $default = null)
|
||||
{
|
||||
if (!$choices) {
|
||||
throw new \LogicException('Choice question must have at least 1 choice available.');
|
||||
@@ -47,10 +45,8 @@ class ChoiceQuestion extends Question
|
||||
|
||||
/**
|
||||
* Returns available choices.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getChoices()
|
||||
public function getChoices(): array
|
||||
{
|
||||
return $this->choices;
|
||||
}
|
||||
@@ -60,11 +56,9 @@ class ChoiceQuestion extends Question
|
||||
*
|
||||
* When multiselect is set to true, multiple choices can be answered.
|
||||
*
|
||||
* @param bool $multiselect
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setMultiselect($multiselect)
|
||||
public function setMultiselect(bool $multiselect): static
|
||||
{
|
||||
$this->multiselect = $multiselect;
|
||||
$this->setValidator($this->getDefaultValidator());
|
||||
@@ -74,20 +68,16 @@ class ChoiceQuestion extends Question
|
||||
|
||||
/**
|
||||
* Returns whether the choices are multiselect.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isMultiselect()
|
||||
public function isMultiselect(): bool
|
||||
{
|
||||
return $this->multiselect;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the prompt for choices.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPrompt()
|
||||
public function getPrompt(): string
|
||||
{
|
||||
return $this->prompt;
|
||||
}
|
||||
@@ -95,11 +85,9 @@ class ChoiceQuestion extends Question
|
||||
/**
|
||||
* Sets the prompt for choices.
|
||||
*
|
||||
* @param string $prompt
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setPrompt($prompt)
|
||||
public function setPrompt(string $prompt): static
|
||||
{
|
||||
$this->prompt = $prompt;
|
||||
|
||||
@@ -111,11 +99,9 @@ class ChoiceQuestion extends Question
|
||||
*
|
||||
* The error message has a string placeholder (%s) for the invalid value.
|
||||
*
|
||||
* @param string $errorMessage
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setErrorMessage($errorMessage)
|
||||
public function setErrorMessage(string $errorMessage): static
|
||||
{
|
||||
$this->errorMessage = $errorMessage;
|
||||
$this->setValidator($this->getDefaultValidator());
|
||||
@@ -123,12 +109,7 @@ class ChoiceQuestion extends Question
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default answer validator.
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
private function getDefaultValidator()
|
||||
private function getDefaultValidator(): callable
|
||||
{
|
||||
$choices = $this->choices;
|
||||
$errorMessage = $this->errorMessage;
|
||||
@@ -136,30 +117,34 @@ class ChoiceQuestion extends Question
|
||||
$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('/^[^,]+(?:,[^,]+)*$/', $selectedChoices, $matches)) {
|
||||
if (!preg_match('/^[^,]+(?:,[^,]+)*$/', (string) $selected, $matches)) {
|
||||
throw new InvalidArgumentException(sprintf($errorMessage, $selected));
|
||||
}
|
||||
$selectedChoices = explode(',', $selectedChoices);
|
||||
|
||||
$selectedChoices = explode(',', (string) $selected);
|
||||
} else {
|
||||
$selectedChoices = array($selected);
|
||||
$selectedChoices = [$selected];
|
||||
}
|
||||
|
||||
$multiselectChoices = array();
|
||||
if ($this->isTrimmable()) {
|
||||
foreach ($selectedChoices as $k => $v) {
|
||||
$selectedChoices[$k] = trim((string) $v);
|
||||
}
|
||||
}
|
||||
|
||||
$multiselectChoices = [];
|
||||
foreach ($selectedChoices as $value) {
|
||||
$results = array();
|
||||
$results = [];
|
||||
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)));
|
||||
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);
|
||||
@@ -178,7 +163,8 @@ class ChoiceQuestion extends Question
|
||||
throw new InvalidArgumentException(sprintf($errorMessage, $value));
|
||||
}
|
||||
|
||||
$multiselectChoices[] = (string) $result;
|
||||
// For associative choices, consistently return the key as string:
|
||||
$multiselectChoices[] = $isAssoc ? (string) $result : $result;
|
||||
}
|
||||
|
||||
if ($multiselect) {
|
||||
|
||||
@@ -18,18 +18,16 @@ namespace Symfony\Component\Console\Question;
|
||||
*/
|
||||
class ConfirmationQuestion extends Question
|
||||
{
|
||||
private $trueAnswerRegex;
|
||||
private string $trueAnswerRegex;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $question The question to ask to the user
|
||||
* @param bool $default The default answer to return, true or false
|
||||
* @param string $trueAnswerRegex A regex to match the "yes" answer
|
||||
*/
|
||||
public function __construct($question, $default = true, $trueAnswerRegex = '/^y/i')
|
||||
public function __construct(string $question, bool $default = true, string $trueAnswerRegex = '/^y/i')
|
||||
{
|
||||
parent::__construct($question, (bool) $default);
|
||||
parent::__construct($question, $default);
|
||||
|
||||
$this->trueAnswerRegex = $trueAnswerRegex;
|
||||
$this->setNormalizer($this->getDefaultNormalizer());
|
||||
@@ -37,16 +35,14 @@ class ConfirmationQuestion extends Question
|
||||
|
||||
/**
|
||||
* Returns the default answer normalizer.
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
private function getDefaultNormalizer()
|
||||
private function getDefaultNormalizer(): callable
|
||||
{
|
||||
$default = $this->getDefault();
|
||||
$regex = $this->trueAnswerRegex;
|
||||
|
||||
return function ($answer) use ($default, $regex) {
|
||||
if (is_bool($answer)) {
|
||||
if (\is_bool($answer)) {
|
||||
return $answer;
|
||||
}
|
||||
|
||||
@@ -55,7 +51,7 @@ class ConfirmationQuestion extends Question
|
||||
return $answer && $answerIsTrue;
|
||||
}
|
||||
|
||||
return !$answer || $answerIsTrue;
|
||||
return '' === $answer || $answerIsTrue;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
185
vendor/symfony/console/Question/Question.php
vendored
185
vendor/symfony/console/Question/Question.php
vendored
@@ -21,22 +21,22 @@ use Symfony\Component\Console\Exception\LogicException;
|
||||
*/
|
||||
class Question
|
||||
{
|
||||
private $question;
|
||||
private $attempts;
|
||||
private $hidden = false;
|
||||
private $hiddenFallback = true;
|
||||
private $autocompleterValues;
|
||||
private $validator;
|
||||
private $default;
|
||||
private $normalizer;
|
||||
private string $question;
|
||||
private ?int $attempts = null;
|
||||
private bool $hidden = false;
|
||||
private bool $hiddenFallback = true;
|
||||
private ?\Closure $autocompleterCallback = null;
|
||||
private ?\Closure $validator = null;
|
||||
private string|int|bool|null|float $default;
|
||||
private ?\Closure $normalizer = null;
|
||||
private bool $trimmable = true;
|
||||
private bool $multiline = false;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $question The question to ask to the user
|
||||
* @param mixed $default The default answer to return if the user enters nothing
|
||||
* @param string $question The question to ask to the user
|
||||
* @param string|bool|int|float|null $default The default answer to return if the user enters nothing
|
||||
*/
|
||||
public function __construct($question, $default = null)
|
||||
public function __construct(string $question, string|bool|int|float $default = null)
|
||||
{
|
||||
$this->question = $question;
|
||||
$this->default = $default;
|
||||
@@ -44,30 +44,44 @@ class Question
|
||||
|
||||
/**
|
||||
* Returns the question.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getQuestion()
|
||||
public function getQuestion(): string
|
||||
{
|
||||
return $this->question;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default answer.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDefault()
|
||||
public function getDefault(): string|bool|int|float|null
|
||||
{
|
||||
return $this->default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the user response must be hidden.
|
||||
*
|
||||
* @return bool
|
||||
* Returns whether the user response accepts newline characters.
|
||||
*/
|
||||
public function isHidden()
|
||||
public function isMultiline(): bool
|
||||
{
|
||||
return $this->multiline;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether the user response should accept newline characters.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setMultiline(bool $multiline): static
|
||||
{
|
||||
$this->multiline = $multiline;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the user response must be hidden.
|
||||
*/
|
||||
public function isHidden(): bool
|
||||
{
|
||||
return $this->hidden;
|
||||
}
|
||||
@@ -75,84 +89,100 @@ class Question
|
||||
/**
|
||||
* Sets whether the user response must be hidden or not.
|
||||
*
|
||||
* @param bool $hidden
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws LogicException In case the autocompleter is also used
|
||||
*/
|
||||
public function setHidden($hidden)
|
||||
public function setHidden(bool $hidden): static
|
||||
{
|
||||
if ($this->autocompleterValues) {
|
||||
if ($this->autocompleterCallback) {
|
||||
throw new LogicException('A hidden question cannot use the autocompleter.');
|
||||
}
|
||||
|
||||
$this->hidden = (bool) $hidden;
|
||||
$this->hidden = $hidden;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* In case the response can not be hidden, whether to fallback on non-hidden question or not.
|
||||
*
|
||||
* @return bool
|
||||
* In case the response cannot be hidden, whether to fallback on non-hidden question or not.
|
||||
*/
|
||||
public function isHiddenFallback()
|
||||
public function isHiddenFallback(): bool
|
||||
{
|
||||
return $this->hiddenFallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether to fallback on non-hidden question if the response can not be hidden.
|
||||
*
|
||||
* @param bool $fallback
|
||||
* Sets whether to fallback on non-hidden question if the response cannot be hidden.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setHiddenFallback($fallback)
|
||||
public function setHiddenFallback(bool $fallback): static
|
||||
{
|
||||
$this->hiddenFallback = (bool) $fallback;
|
||||
$this->hiddenFallback = $fallback;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets values for the autocompleter.
|
||||
*
|
||||
* @return null|array|\Traversable
|
||||
*/
|
||||
public function getAutocompleterValues()
|
||||
public function getAutocompleterValues(): ?iterable
|
||||
{
|
||||
return $this->autocompleterValues;
|
||||
$callback = $this->getAutocompleterCallback();
|
||||
|
||||
return $callback ? $callback('') : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets values for the autocompleter.
|
||||
*
|
||||
* @param null|array|\Traversable $values
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @throws LogicException
|
||||
*/
|
||||
public function setAutocompleterValues($values)
|
||||
public function setAutocompleterValues(?iterable $values): static
|
||||
{
|
||||
if (is_array($values)) {
|
||||
if (\is_array($values)) {
|
||||
$values = $this->isAssoc($values) ? array_merge(array_keys($values), array_values($values)) : array_values($values);
|
||||
|
||||
$callback = static function () use ($values) {
|
||||
return $values;
|
||||
};
|
||||
} elseif ($values instanceof \Traversable) {
|
||||
$valueCache = null;
|
||||
$callback = static function () use ($values, &$valueCache) {
|
||||
return $valueCache ?? $valueCache = iterator_to_array($values, false);
|
||||
};
|
||||
} else {
|
||||
$callback = null;
|
||||
}
|
||||
|
||||
if (null !== $values && !is_array($values)) {
|
||||
if (!$values instanceof \Traversable || !$values instanceof \Countable) {
|
||||
throw new InvalidArgumentException('Autocompleter values can be either an array, `null` or an object implementing both `Countable` and `Traversable` interfaces.');
|
||||
}
|
||||
}
|
||||
return $this->setAutocompleterCallback($callback);
|
||||
}
|
||||
|
||||
if ($this->hidden) {
|
||||
/**
|
||||
* Gets the callback function used for the autocompleter.
|
||||
*/
|
||||
public function getAutocompleterCallback(): ?callable
|
||||
{
|
||||
return $this->autocompleterCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the callback function used for the autocompleter.
|
||||
*
|
||||
* The callback is passed the user input as argument and should return an iterable of corresponding suggestions.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setAutocompleterCallback(callable $callback = null): static
|
||||
{
|
||||
if ($this->hidden && null !== $callback) {
|
||||
throw new LogicException('A hidden question cannot use the autocompleter.');
|
||||
}
|
||||
|
||||
$this->autocompleterValues = $values;
|
||||
$this->autocompleterCallback = null === $callback || $callback instanceof \Closure ? $callback : \Closure::fromCallable($callback);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -160,23 +190,19 @@ class Question
|
||||
/**
|
||||
* Sets a validator for the question.
|
||||
*
|
||||
* @param null|callable $validator
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setValidator(callable $validator = null)
|
||||
public function setValidator(callable $validator = null): static
|
||||
{
|
||||
$this->validator = $validator;
|
||||
$this->validator = null === $validator || $validator instanceof \Closure ? $validator : \Closure::fromCallable($validator);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the validator for the question.
|
||||
*
|
||||
* @return null|callable
|
||||
*/
|
||||
public function getValidator()
|
||||
public function getValidator(): ?callable
|
||||
{
|
||||
return $this->validator;
|
||||
}
|
||||
@@ -186,13 +212,11 @@ class Question
|
||||
*
|
||||
* Null means an unlimited number of attempts.
|
||||
*
|
||||
* @param null|int $attempts
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws InvalidArgumentException In case the number of attempts is invalid.
|
||||
* @throws InvalidArgumentException in case the number of attempts is invalid
|
||||
*/
|
||||
public function setMaxAttempts($attempts)
|
||||
public function setMaxAttempts(?int $attempts): static
|
||||
{
|
||||
if (null !== $attempts && $attempts < 1) {
|
||||
throw new InvalidArgumentException('Maximum number of attempts must be a positive value.');
|
||||
@@ -207,10 +231,8 @@ class Question
|
||||
* Gets the maximum number of attempts.
|
||||
*
|
||||
* Null means an unlimited number of attempts.
|
||||
*
|
||||
* @return null|int
|
||||
*/
|
||||
public function getMaxAttempts()
|
||||
public function getMaxAttempts(): ?int
|
||||
{
|
||||
return $this->attempts;
|
||||
}
|
||||
@@ -220,13 +242,11 @@ class Question
|
||||
*
|
||||
* The normalizer can be a callable (a string), a closure or a class implementing __invoke.
|
||||
*
|
||||
* @param callable $normalizer
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setNormalizer(callable $normalizer)
|
||||
public function setNormalizer(callable $normalizer): static
|
||||
{
|
||||
$this->normalizer = $normalizer;
|
||||
$this->normalizer = $normalizer instanceof \Closure ? $normalizer : \Closure::fromCallable($normalizer);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -235,16 +255,29 @@ class Question
|
||||
* Gets the normalizer for the response.
|
||||
*
|
||||
* The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
public function getNormalizer()
|
||||
public function getNormalizer(): ?callable
|
||||
{
|
||||
return $this->normalizer;
|
||||
}
|
||||
|
||||
protected function isAssoc($array)
|
||||
protected function isAssoc(array $array)
|
||||
{
|
||||
return (bool) count(array_filter(array_keys($array), 'is_string'));
|
||||
return (bool) \count(array_filter(array_keys($array), 'is_string'));
|
||||
}
|
||||
|
||||
public function isTrimmable(): bool
|
||||
{
|
||||
return $this->trimmable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setTrimmable(bool $trimmable): static
|
||||
{
|
||||
$this->trimmable = $trimmable;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user