ChoiceFormField.php 8.7 KB

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