PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.2
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / vendor / prefixed / symfony / console / Question / ChoiceQuestion.php
matomo / app / vendor / prefixed / symfony / console / Question Last commit date
ChoiceQuestion.php 1 year ago ConfirmationQuestion.php 1 year ago Question.php 1 year ago
ChoiceQuestion.php
158 lines
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 namespace Matomo\Dependencies\Symfony\Component\Console\Question;
12
13 use Matomo\Dependencies\Symfony\Component\Console\Exception\InvalidArgumentException;
14 /**
15 * Represents a choice question.
16 *
17 * @author Fabien Potencier <fabien@symfony.com>
18 */
19 class ChoiceQuestion extends Question
20 {
21 private $choices;
22 private $multiselect = \false;
23 private $prompt = ' > ';
24 private $errorMessage = 'Value "%s" is invalid';
25 /**
26 * @param string $question The question to ask to the user
27 * @param array $choices The list of available choices
28 * @param mixed $default The default answer to return
29 */
30 public function __construct(string $question, array $choices, $default = null)
31 {
32 if (!$choices) {
33 throw new \LogicException('Choice question must have at least 1 choice available.');
34 }
35 parent::__construct($question, $default);
36 $this->choices = $choices;
37 $this->setValidator($this->getDefaultValidator());
38 $this->setAutocompleterValues($choices);
39 }
40 /**
41 * Returns available choices.
42 *
43 * @return array
44 */
45 public function getChoices()
46 {
47 return $this->choices;
48 }
49 /**
50 * Sets multiselect option.
51 *
52 * When multiselect is set to true, multiple choices can be answered.
53 *
54 * @return $this
55 */
56 public function setMultiselect(bool $multiselect)
57 {
58 $this->multiselect = $multiselect;
59 $this->setValidator($this->getDefaultValidator());
60 return $this;
61 }
62 /**
63 * Returns whether the choices are multiselect.
64 *
65 * @return bool
66 */
67 public function isMultiselect()
68 {
69 return $this->multiselect;
70 }
71 /**
72 * Gets the prompt for choices.
73 *
74 * @return string
75 */
76 public function getPrompt()
77 {
78 return $this->prompt;
79 }
80 /**
81 * Sets the prompt for choices.
82 *
83 * @return $this
84 */
85 public function setPrompt(string $prompt)
86 {
87 $this->prompt = $prompt;
88 return $this;
89 }
90 /**
91 * Sets the error message for invalid values.
92 *
93 * The error message has a string placeholder (%s) for the invalid value.
94 *
95 * @return $this
96 */
97 public function setErrorMessage(string $errorMessage)
98 {
99 $this->errorMessage = $errorMessage;
100 $this->setValidator($this->getDefaultValidator());
101 return $this;
102 }
103 private function getDefaultValidator() : callable
104 {
105 $choices = $this->choices;
106 $errorMessage = $this->errorMessage;
107 $multiselect = $this->multiselect;
108 $isAssoc = $this->isAssoc($choices);
109 return function ($selected) use($choices, $errorMessage, $multiselect, $isAssoc) {
110 if ($multiselect) {
111 // Check for a separated comma values
112 if (!preg_match('/^[^,]+(?:,[^,]+)*$/', (string) $selected, $matches)) {
113 throw new InvalidArgumentException(sprintf($errorMessage, $selected));
114 }
115 $selectedChoices = explode(',', (string) $selected);
116 } else {
117 $selectedChoices = [$selected];
118 }
119 if ($this->isTrimmable()) {
120 foreach ($selectedChoices as $k => $v) {
121 $selectedChoices[$k] = trim((string) $v);
122 }
123 }
124 $multiselectChoices = [];
125 foreach ($selectedChoices as $value) {
126 $results = [];
127 foreach ($choices as $key => $choice) {
128 if ($choice === $value) {
129 $results[] = $key;
130 }
131 }
132 if (\count($results) > 1) {
133 throw new InvalidArgumentException(sprintf('The provided answer is ambiguous. Value should be one of "%s".', implode('" or "', $results)));
134 }
135 $result = array_search($value, $choices);
136 if (!$isAssoc) {
137 if (\false !== $result) {
138 $result = $choices[$result];
139 } elseif (isset($choices[$value])) {
140 $result = $choices[$value];
141 }
142 } elseif (\false === $result && isset($choices[$value])) {
143 $result = $value;
144 }
145 if (\false === $result) {
146 throw new InvalidArgumentException(sprintf($errorMessage, $value));
147 }
148 // For associative choices, consistently return the key as string:
149 $multiselectChoices[] = $isAssoc ? (string) $result : $result;
150 }
151 if ($multiselect) {
152 return $multiselectChoices;
153 }
154 return current($multiselectChoices);
155 };
156 }
157 }
158