FormField.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * FormField is the abstract class for all form fields.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. abstract class FormField
  17. {
  18. /**
  19. * @var \DOMElement
  20. */
  21. protected $node;
  22. /**
  23. * @var string
  24. */
  25. protected $name;
  26. /**
  27. * @var string
  28. */
  29. protected $value;
  30. /**
  31. * @var \DOMDocument
  32. */
  33. protected $document;
  34. /**
  35. * @var \DOMXPath
  36. */
  37. protected $xpath;
  38. /**
  39. * @var bool
  40. */
  41. protected $disabled;
  42. /**
  43. * @param \DOMElement $node The node associated with this field
  44. */
  45. public function __construct(\DOMElement $node)
  46. {
  47. $this->node = $node;
  48. $this->name = $node->getAttribute('name');
  49. $this->xpath = new \DOMXPath($node->ownerDocument);
  50. $this->initialize();
  51. }
  52. /**
  53. * Returns the label tag associated to the field or null if none.
  54. *
  55. * @return \DOMElement|null
  56. */
  57. public function getLabel()
  58. {
  59. $xpath = new \DOMXPath($this->node->ownerDocument);
  60. if ($this->node->hasAttribute('id')) {
  61. $labels = $xpath->query(sprintf('descendant::label[@for="%s"]', $this->node->getAttribute('id')));
  62. if ($labels->length > 0) {
  63. return $labels->item(0);
  64. }
  65. }
  66. $labels = $xpath->query('ancestor::label[1]', $this->node);
  67. if ($labels->length > 0) {
  68. return $labels->item(0);
  69. }
  70. }
  71. /**
  72. * Returns the name of the field.
  73. *
  74. * @return string The name of the field
  75. */
  76. public function getName()
  77. {
  78. return $this->name;
  79. }
  80. /**
  81. * Gets the value of the field.
  82. *
  83. * @return string|array The value of the field
  84. */
  85. public function getValue()
  86. {
  87. return $this->value;
  88. }
  89. /**
  90. * Sets the value of the field.
  91. *
  92. * @param string $value The value of the field
  93. */
  94. public function setValue($value)
  95. {
  96. $this->value = (string) $value;
  97. }
  98. /**
  99. * Returns true if the field should be included in the submitted values.
  100. *
  101. * @return bool true if the field should be included in the submitted values, false otherwise
  102. */
  103. public function hasValue()
  104. {
  105. return true;
  106. }
  107. /**
  108. * Check if the current field is disabled.
  109. *
  110. * @return bool
  111. */
  112. public function isDisabled()
  113. {
  114. return $this->node->hasAttribute('disabled');
  115. }
  116. /**
  117. * Initializes the form field.
  118. */
  119. abstract protected function initialize();
  120. }