InputFormField.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. * InputFormField represents an input form field (an HTML input tag).
  13. *
  14. * For inputs with type of file, checkbox, or radio, there are other more
  15. * specialized classes (cf. FileFormField and ChoiceFormField).
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class InputFormField extends FormField
  20. {
  21. /**
  22. * Initializes the form field.
  23. *
  24. * @throws \LogicException When node type is incorrect
  25. */
  26. protected function initialize()
  27. {
  28. if ('input' !== $this->node->nodeName && 'button' !== $this->node->nodeName) {
  29. throw new \LogicException(sprintf('An InputFormField can only be created from an input or button tag (%s given).', $this->node->nodeName));
  30. }
  31. $type = strtolower($this->node->getAttribute('type'));
  32. if ('checkbox' === $type) {
  33. throw new \LogicException('Checkboxes should be instances of ChoiceFormField.');
  34. }
  35. if ('file' === $type) {
  36. throw new \LogicException('File inputs should be instances of FileFormField.');
  37. }
  38. $this->value = $this->node->getAttribute('value');
  39. }
  40. }