ChoiceFormField.php 8.7 KB

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