Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\DomCrawler\Field; Chris@0: Chris@0: /** Chris@0: * ChoiceFormField represents a choice form field. Chris@0: * Chris@0: * It is constructed from a HTML select tag, or a HTML checkbox, or radio inputs. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class ChoiceFormField extends FormField Chris@0: { Chris@0: /** Chris@0: * @var string Chris@0: */ Chris@0: private $type; Chris@0: /** Chris@0: * @var bool Chris@0: */ Chris@0: private $multiple; Chris@0: /** Chris@0: * @var array Chris@0: */ Chris@0: private $options; Chris@0: /** Chris@0: * @var bool Chris@0: */ Chris@0: private $validationDisabled = false; Chris@0: Chris@0: /** Chris@0: * Returns true if the field should be included in the submitted values. Chris@0: * Chris@0: * @return bool true if the field should be included in the submitted values, false otherwise Chris@0: */ Chris@0: public function hasValue() Chris@0: { Chris@0: // don't send a value for unchecked checkboxes Chris@17: if (\in_array($this->type, ['checkbox', 'radio']) && null === $this->value) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check if the current selected option is disabled. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isDisabled() Chris@0: { Chris@0: if (parent::isDisabled() && 'select' === $this->type) { Chris@0: return true; Chris@0: } Chris@0: Chris@0: foreach ($this->options as $option) { Chris@0: if ($option['value'] == $this->value && $option['disabled']) { Chris@0: return true; Chris@0: } Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the value of the field. Chris@0: * Chris@17: * @param string|array $value The value of the field Chris@0: */ Chris@0: public function select($value) Chris@0: { Chris@0: $this->setValue($value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ticks a checkbox. Chris@0: * Chris@0: * @throws \LogicException When the type provided is not correct Chris@0: */ Chris@0: public function tick() Chris@0: { Chris@0: if ('checkbox' !== $this->type) { Chris@0: throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->name, $this->type)); Chris@0: } Chris@0: Chris@0: $this->setValue(true); Chris@0: } Chris@0: Chris@0: /** Chris@13: * Unticks a checkbox. Chris@0: * Chris@0: * @throws \LogicException When the type provided is not correct Chris@0: */ Chris@0: public function untick() Chris@0: { Chris@0: if ('checkbox' !== $this->type) { Chris@13: throw new \LogicException(sprintf('You cannot untick "%s" as it is not a checkbox (%s).', $this->name, $this->type)); Chris@0: } Chris@0: Chris@0: $this->setValue(false); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the value of the field. Chris@0: * Chris@13: * @param string|array $value The value of the field Chris@0: * Chris@0: * @throws \InvalidArgumentException When value type provided is not correct Chris@0: */ Chris@0: public function setValue($value) Chris@0: { Chris@0: if ('checkbox' === $this->type && false === $value) { Chris@0: // uncheck Chris@0: $this->value = null; Chris@0: } elseif ('checkbox' === $this->type && true === $value) { Chris@0: // check Chris@0: $this->value = $this->options[0]['value']; Chris@0: } else { Chris@17: if (\is_array($value)) { Chris@0: if (!$this->multiple) { Chris@0: throw new \InvalidArgumentException(sprintf('The value for "%s" cannot be an array.', $this->name)); Chris@0: } Chris@0: Chris@0: foreach ($value as $v) { Chris@0: if (!$this->containsOption($v, $this->options)) { Chris@0: throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: %s).', $this->name, $v, implode(', ', $this->availableOptionValues()))); Chris@0: } Chris@0: } Chris@0: } elseif (!$this->containsOption($value, $this->options)) { Chris@0: throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: %s).', $this->name, $value, implode(', ', $this->availableOptionValues()))); Chris@0: } Chris@0: Chris@0: if ($this->multiple) { Chris@0: $value = (array) $value; Chris@0: } Chris@0: Chris@17: if (\is_array($value)) { Chris@0: $this->value = $value; Chris@0: } else { Chris@0: parent::setValue($value); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a choice to the current ones. Chris@0: * Chris@0: * @param \DOMElement $node Chris@0: * Chris@0: * @throws \LogicException When choice provided is not multiple nor radio Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: public function addChoice(\DOMElement $node) Chris@0: { Chris@0: if (!$this->multiple && 'radio' !== $this->type) { Chris@0: throw new \LogicException(sprintf('Unable to add a choice for "%s" as it is not multiple or is not a radio button.', $this->name)); Chris@0: } Chris@0: Chris@0: $option = $this->buildOptionValue($node); Chris@0: $this->options[] = $option; Chris@0: Chris@0: if ($node->hasAttribute('checked')) { Chris@0: $this->value = $option['value']; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the type of the choice field (radio, select, or checkbox). Chris@0: * Chris@0: * @return string The type Chris@0: */ Chris@0: public function getType() Chris@0: { Chris@0: return $this->type; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the field accepts multiple values. Chris@0: * Chris@0: * @return bool true if the field accepts multiple values, false otherwise Chris@0: */ Chris@0: public function isMultiple() Chris@0: { Chris@0: return $this->multiple; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Initializes the form field. Chris@0: * Chris@0: * @throws \LogicException When node type is incorrect Chris@0: */ Chris@0: protected function initialize() Chris@0: { Chris@0: if ('input' !== $this->node->nodeName && 'select' !== $this->node->nodeName) { Chris@0: throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $this->node->nodeName)); Chris@0: } Chris@0: Chris@0: if ('input' === $this->node->nodeName && 'checkbox' !== strtolower($this->node->getAttribute('type')) && 'radio' !== strtolower($this->node->getAttribute('type'))) { Chris@0: 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'))); Chris@0: } Chris@0: Chris@0: $this->value = null; Chris@17: $this->options = []; Chris@0: $this->multiple = false; Chris@0: Chris@0: if ('input' == $this->node->nodeName) { Chris@0: $this->type = strtolower($this->node->getAttribute('type')); Chris@0: $optionValue = $this->buildOptionValue($this->node); Chris@0: $this->options[] = $optionValue; Chris@0: Chris@0: if ($this->node->hasAttribute('checked')) { Chris@0: $this->value = $optionValue['value']; Chris@0: } Chris@0: } else { Chris@0: $this->type = 'select'; Chris@0: if ($this->node->hasAttribute('multiple')) { Chris@0: $this->multiple = true; Chris@17: $this->value = []; Chris@0: $this->name = str_replace('[]', '', $this->name); Chris@0: } Chris@0: Chris@0: $found = false; Chris@0: foreach ($this->xpath->query('descendant::option', $this->node) as $option) { Chris@0: $optionValue = $this->buildOptionValue($option); Chris@0: $this->options[] = $optionValue; Chris@0: Chris@0: if ($option->hasAttribute('selected')) { Chris@0: $found = true; Chris@0: if ($this->multiple) { Chris@0: $this->value[] = $optionValue['value']; Chris@0: } else { Chris@0: $this->value = $optionValue['value']; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // if no option is selected and if it is a simple select box, take the first option as the value Chris@0: if (!$found && !$this->multiple && !empty($this->options)) { Chris@0: $this->value = $this->options[0]['value']; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns option value with associated disabled flag. Chris@0: * Chris@0: * @param \DOMElement $node Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: private function buildOptionValue(\DOMElement $node) Chris@0: { Chris@17: $option = []; Chris@0: Chris@0: $defaultDefaultValue = 'select' === $this->node->nodeName ? '' : 'on'; Chris@0: $defaultValue = (isset($node->nodeValue) && !empty($node->nodeValue)) ? $node->nodeValue : $defaultDefaultValue; Chris@0: $option['value'] = $node->hasAttribute('value') ? $node->getAttribute('value') : $defaultValue; Chris@0: $option['disabled'] = $node->hasAttribute('disabled'); Chris@0: Chris@0: return $option; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether given value is in the existing options. Chris@0: * Chris@0: * @param string $optionValue Chris@0: * @param array $options Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function containsOption($optionValue, $options) Chris@0: { Chris@0: if ($this->validationDisabled) { Chris@0: return true; Chris@0: } Chris@0: Chris@0: foreach ($options as $option) { Chris@0: if ($option['value'] == $optionValue) { Chris@0: return true; Chris@0: } Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns list of available field options. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function availableOptionValues() Chris@0: { Chris@17: $values = []; Chris@0: Chris@0: foreach ($this->options as $option) { Chris@0: $values[] = $option['value']; Chris@0: } Chris@0: Chris@0: return $values; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Disables the internal validation of the field. Chris@0: * Chris@0: * @return self Chris@0: */ Chris@0: public function disableValidation() Chris@0: { Chris@0: $this->validationDisabled = true; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: }