PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.2.0
Independent Analytics – WordPress Analytics Plugin v2.2.0
2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / symfony / console / Question / ChoiceQuestion.php
independent-analytics / vendor / symfony / console / Question Last commit date
ChoiceQuestion.php 2 years ago ConfirmationQuestion.php 2 years ago Question.php 2 years ago
ChoiceQuestion.php
159 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 IAWP_SCOPED\Symfony\Component\Console\Question;
12
13 use IAWP_SCOPED\Symfony\Component\Console\Exception\InvalidArgumentException;
14 /**
15 * Represents a choice question.
16 *
17 * @author Fabien Potencier <fabien@symfony.com>
18 * @internal
19 */
20 class ChoiceQuestion extends Question
21 {
22 private $choices;
23 private $multiselect = \false;
24 private $prompt = ' > ';
25 private $errorMessage = 'Value "%s" is invalid';
26 /**
27 * @param string $question The question to ask to the user
28 * @param array $choices The list of available choices
29 * @param mixed $default The default answer to return
30 */
31 public function __construct(string $question, array $choices, $default = null)
32 {
33 if (!$choices) {
34 throw new \LogicException('Choice question must have at least 1 choice available.');
35 }
36 parent::__construct($question, $default);
37 $this->choices = $choices;
38 $this->setValidator($this->getDefaultValidator());
39 $this->setAutocompleterValues($choices);
40 }
41 /**
42 * Returns available choices.
43 *
44 * @return array
45 */
46 public function getChoices()
47 {
48 return $this->choices;
49 }
50 /**
51 * Sets multiselect option.
52 *
53 * When multiselect is set to true, multiple choices can be answered.
54 *
55 * @return $this
56 */
57 public function setMultiselect(bool $multiselect)
58 {
59 $this->multiselect = $multiselect;
60 $this->setValidator($this->getDefaultValidator());
61 return $this;
62 }
63 /**
64 * Returns whether the choices are multiselect.
65 *
66 * @return bool
67 */
68 public function isMultiselect()
69 {
70 return $this->multiselect;
71 }
72 /**
73 * Gets the prompt for choices.
74 *
75 * @return string
76 */
77 public function getPrompt()
78 {
79 return $this->prompt;
80 }
81 /**
82 * Sets the prompt for choices.
83 *
84 * @return $this
85 */
86 public function setPrompt(string $prompt)
87 {
88 $this->prompt = $prompt;
89 return $this;
90 }
91 /**
92 * Sets the error message for invalid values.
93 *
94 * The error message has a string placeholder (%s) for the invalid value.
95 *
96 * @return $this
97 */
98 public function setErrorMessage(string $errorMessage)
99 {
100 $this->errorMessage = $errorMessage;
101 $this->setValidator($this->getDefaultValidator());
102 return $this;
103 }
104 private function getDefaultValidator() : callable
105 {
106 $choices = $this->choices;
107 $errorMessage = $this->errorMessage;
108 $multiselect = $this->multiselect;
109 $isAssoc = $this->isAssoc($choices);
110 return function ($selected) use($choices, $errorMessage, $multiselect, $isAssoc) {
111 if ($multiselect) {
112 // Check for a separated comma values
113 if (!\preg_match('/^[^,]+(?:,[^,]+)*$/', (string) $selected, $matches)) {
114 throw new InvalidArgumentException(\sprintf($errorMessage, $selected));
115 }
116 $selectedChoices = \explode(',', (string) $selected);
117 } else {
118 $selectedChoices = [$selected];
119 }
120 if ($this->isTrimmable()) {
121 foreach ($selectedChoices as $k => $v) {
122 $selectedChoices[$k] = \trim((string) $v);
123 }
124 }
125 $multiselectChoices = [];
126 foreach ($selectedChoices as $value) {
127 $results = [];
128 foreach ($choices as $key => $choice) {
129 if ($choice === $value) {
130 $results[] = $key;
131 }
132 }
133 if (\count($results) > 1) {
134 throw new InvalidArgumentException(\sprintf('The provided answer is ambiguous. Value should be one of "%s".', \implode('" or "', $results)));
135 }
136 $result = \array_search($value, $choices);
137 if (!$isAssoc) {
138 if (\false !== $result) {
139 $result = $choices[$result];
140 } elseif (isset($choices[$value])) {
141 $result = $choices[$value];
142 }
143 } elseif (\false === $result && isset($choices[$value])) {
144 $result = $value;
145 }
146 if (\false === $result) {
147 throw new InvalidArgumentException(\sprintf($errorMessage, $value));
148 }
149 // For associative choices, consistently return the key as string:
150 $multiselectChoices[] = $isAssoc ? (string) $result : $result;
151 }
152 if ($multiselect) {
153 return $multiselectChoices;
154 }
155 return \current($multiselectChoices);
156 };
157 }
158 }
159