PluginProbe
BeyondWords – AI audio for publishers / 7.2.0
BeyondWords – AI audio for publishers v7.2.0
7.2.0 7.1.0 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 All 44 releases
speechkit / vendor / symfony / dom-crawler / Field / ChoiceFormField.php

ChoiceFormField.php in BeyondWords – AI audio for publishers 7.2.0, at vendor/symfony/dom-crawler/Field/ChoiceFormField.php

322 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\DomCrawler\Field;
13
14 /**
15 * ChoiceFormField represents a choice form field.
16 *
17 * It is constructed from an HTML select tag, or an HTML checkbox, or radio inputs.
18 *
19 * @author Fabien Potencier <[email protected]>
20 */
21 class ChoiceFormField extends FormField
22 {
23 /**
24 * @var string
25 */
26 private $type;
27 /**
28 * @var bool
29 */
30 private $multiple;
31 /**
32 * @var array
33 */
34 private $options;
35 /**
36 * @var bool
37 */
38 private $validationDisabled = false;
39
40 /**
41 * Returns true if the field should be included in the submitted values.
42 *
43 * @return bool true if the field should be included in the submitted values, false otherwise
44 */
45 public function hasValue()
46 {
47 // don't send a value for unchecked checkboxes
48 if (\in_array($this->type, ['checkbox', 'radio']) && null === $this->value) {
49 return false;
50 }
51
52 return true;
53 }
54
55 /**
56 * Check if the current selected option is disabled.
57 *
58 * @return bool
59 */
60 public function isDisabled()
61 {
62 if (parent::isDisabled() && 'select' === $this->type) {
63 return true;
64 }
65
66 foreach ($this->options as $option) {
67 if ($option['value'] == $this->value && $option['disabled']) {
68 return true;
69 }
70 }
71
72 return false;
73 }
74
75 /**
76 * Sets the value of the field.
77 *
78 * @param string|array $value The value of the field
79 */
80 public function select($value)
81 {
82 $this->setValue($value);
83 }
84
85 /**
86 * Ticks a checkbox.
87 *
88 * @throws \LogicException When the type provided is not correct
89 */
90 public function tick()
91 {
92 if ('checkbox' !== $this->type) {
93 throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->name, $this->type));
94 }
95
96 $this->setValue(true);
97 }
98
99 /**
100 * Unticks a checkbox.
101 *
102 * @throws \LogicException When the type provided is not correct
103 */
104 public function untick()
105 {
106 if ('checkbox' !== $this->type) {
107 throw new \LogicException(sprintf('You cannot untick "%s" as it is not a checkbox (%s).', $this->name, $this->type));
108 }
109
110 $this->setValue(false);
111 }
112
113 /**
114 * Sets the value of the field.
115 *
116 * @param string|array|bool|null $value The value of the field
117 *
118 * @throws \InvalidArgumentException When value type provided is not correct
119 */
120 public function setValue($value)
121 {
122 if ('checkbox' === $this->type && false === $value) {
123 // uncheck
124 $this->value = null;
125 } elseif ('checkbox' === $this->type && true === $value) {
126 // check
127 $this->value = $this->options[0]['value'];
128 } else {
129 if (\is_array($value)) {
130 if (!$this->multiple) {
131 throw new \InvalidArgumentException(sprintf('The value for "%s" cannot be an array.', $this->name));
132 }
133
134 foreach ($value as $v) {
135 if (!$this->containsOption($v, $this->options)) {
136 throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: "%s").', $this->name, $v, implode('", "', $this->availableOptionValues())));
137 }
138 }
139 } elseif (!$this->containsOption($value, $this->options)) {
140 throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: "%s").', $this->name, $value, implode('", "', $this->availableOptionValues())));
141 }
142
143 if ($this->multiple) {
144 $value = (array) $value;
145 }
146
147 if (\is_array($value)) {
148 $this->value = $value;
149 } else {
150 parent::setValue($value);
151 }
152 }
153 }
154
155 /**
156 * Adds a choice to the current ones.
157 *
158 * @throws \LogicException When choice provided is not multiple nor radio
159 *
160 * @internal
161 */
162 public function addChoice(\DOMElement $node)
163 {
164 if (!$this->multiple && 'radio' !== $this->type) {
165 throw new \LogicException(sprintf('Unable to add a choice for "%s" as it is not multiple or is not a radio button.', $this->name));
166 }
167
168 $option = $this->buildOptionValue($node);
169 $this->options[] = $option;
170
171 if ($node->hasAttribute('checked')) {
172 $this->value = $option['value'];
173 }
174 }
175
176 /**
177 * Returns the type of the choice field (radio, select, or checkbox).
178 *
179 * @return string
180 */
181 public function getType()
182 {
183 return $this->type;
184 }
185
186 /**
187 * Returns true if the field accepts multiple values.
188 *
189 * @return bool
190 */
191 public function isMultiple()
192 {
193 return $this->multiple;
194 }
195
196 /**
197 * Initializes the form field.
198 *
199 * @throws \LogicException When node type is incorrect
200 */
201 protected function initialize()
202 {
203 if ('input' !== $this->node->nodeName && 'select' !== $this->node->nodeName) {
204 throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $this->node->nodeName));
205 }
206
207 if ('input' === $this->node->nodeName && 'checkbox' !== strtolower($this->node->getAttribute('type')) && 'radio' !== strtolower($this->node->getAttribute('type'))) {
208 throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input tag with a type of checkbox or radio (given type is "%s").', $this->node->getAttribute('type')));
209 }
210
211 $this->value = null;
212 $this->options = [];
213 $this->multiple = false;
214
215 if ('input' == $this->node->nodeName) {
216 $this->type = strtolower($this->node->getAttribute('type'));
217 $optionValue = $this->buildOptionValue($this->node);
218 $this->options[] = $optionValue;
219
220 if ($this->node->hasAttribute('checked')) {
221 $this->value = $optionValue['value'];
222 }
223 } else {
224 $this->type = 'select';
225 if ($this->node->hasAttribute('multiple')) {
226 $this->multiple = true;
227 $this->value = [];
228 $this->name = str_replace('[]', '', $this->name);
229 }
230
231 $found = false;
232 foreach ($this->xpath->query('descendant::option', $this->node) as $option) {
233 $optionValue = $this->buildOptionValue($option);
234 $this->options[] = $optionValue;
235
236 if ($option->hasAttribute('selected')) {
237 $found = true;
238 if ($this->multiple) {
239 $this->value[] = $optionValue['value'];
240 } else {
241 $this->value = $optionValue['value'];
242 }
243 }
244 }
245
246 // if no option is selected and if it is a simple select box, take the first option as the value
247 if (!$found && !$this->multiple && !empty($this->options)) {
248 $this->value = $this->options[0]['value'];
249 }
250 }
251 }
252
253 /**
254 * Returns option value with associated disabled flag.
255 */
256 private function buildOptionValue(\DOMElement $node): array
257 {
258 $option = [];
259
260 $defaultDefaultValue = 'select' === $this->node->nodeName ? '' : 'on';
261 $defaultValue = (isset($node->nodeValue) && !empty($node->nodeValue)) ? $node->nodeValue : $defaultDefaultValue;
262 $option['value'] = $node->hasAttribute('value') ? $node->getAttribute('value') : $defaultValue;
263 $option['disabled'] = $node->hasAttribute('disabled');
264
265 return $option;
266 }
267
268 /**
269 * Checks whether given value is in the existing options.
270 *
271 * @internal since Symfony 5.3
272 *
273 * @return bool
274 */
275 public function containsOption(string $optionValue, array $options)
276 {
277 if ($this->validationDisabled) {
278 return true;
279 }
280
281 foreach ($options as $option) {
282 if ($option['value'] == $optionValue) {
283 return true;
284 }
285 }
286
287 return false;
288 }
289
290 /**
291 * Returns list of available field options.
292 *
293 * @internal since Symfony 5.3
294 *
295 * @return array
296 */
297 public function availableOptionValues()
298 {
299 $values = [];
300
301 foreach ($this->options as $option) {
302 $values[] = $option['value'];
303 }
304
305 return $values;
306 }
307
308 /**
309 * Disables the internal validation of the field.
310 *
311 * @internal since Symfony 5.3
312 *
313 * @return $this
314 */
315 public function disableValidation()
316 {
317 $this->validationDisabled = true;
318
319 return $this;
320 }
321 }
322