FormField.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 \DOMNode
  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 Boolean
  40. */
  41. protected $disabled;
  42. /**
  43. * Constructor.
  44. *
  45. * @param \DOMNode $node The node associated with this field
  46. */
  47. public function __construct(\DOMNode $node)
  48. {
  49. $this->node = $node;
  50. $this->name = $node->getAttribute('name');
  51. $this->document = new \DOMDocument('1.0', 'UTF-8');
  52. $this->node = $this->document->importNode($this->node, true);
  53. $root = $this->document->appendChild($this->document->createElement('_root'));
  54. $root->appendChild($this->node);
  55. $this->xpath = new \DOMXPath($this->document);
  56. $this->initialize();
  57. }
  58. /**
  59. * Returns the name of the field.
  60. *
  61. * @return string The name of the field
  62. */
  63. public function getName()
  64. {
  65. return $this->name;
  66. }
  67. /**
  68. * Gets the value of the field.
  69. *
  70. * @return string|array The value of the field
  71. */
  72. public function getValue()
  73. {
  74. return $this->value;
  75. }
  76. /**
  77. * Sets the value of the field.
  78. *
  79. * @param string $value The value of the field
  80. *
  81. * @api
  82. */
  83. public function setValue($value)
  84. {
  85. $this->value = (string) $value;
  86. }
  87. /**
  88. * Returns true if the field should be included in the submitted values.
  89. *
  90. * @return Boolean true if the field should be included in the submitted values, false otherwise
  91. */
  92. public function hasValue()
  93. {
  94. return true;
  95. }
  96. /**
  97. * Check if the current field is disabled
  98. *
  99. * @return Boolean
  100. */
  101. public function isDisabled()
  102. {
  103. return $this->node->hasAttribute('disabled');
  104. }
  105. /**
  106. * Initializes the form field.
  107. */
  108. abstract protected function initialize();
  109. }