| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\DomCrawler\Field; |
| 13 |
|
| 14 |
/** |
| 15 |
* FormField is the abstract class for all form fields. |
| 16 |
* |
| 17 |
* @author Fabien Potencier <[email protected]> |
| 18 |
*/ |
| 19 |
abstract class FormField |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var \DOMElement |
| 23 |
*/ |
| 24 |
protected $node; |
| 25 |
/** |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $name; |
| 29 |
/** |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $value; |
| 33 |
/** |
| 34 |
* @var \DOMDocument |
| 35 |
*/ |
| 36 |
protected $document; |
| 37 |
/** |
| 38 |
* @var \DOMXPath |
| 39 |
*/ |
| 40 |
protected $xpath; |
| 41 |
/** |
| 42 |
* @var bool |
| 43 |
*/ |
| 44 |
protected $disabled; |
| 45 |
|
| 46 |
/** |
| 47 |
* @param \DOMElement $node The node associated with this field |
| 48 |
*/ |
| 49 |
public function __construct(\DOMElement $node) |
| 50 |
{ |
| 51 |
$this->node = $node; |
| 52 |
$this->name = $node->getAttribute('name'); |
| 53 |
$this->xpath = new \DOMXPath($node->ownerDocument); |
| 54 |
|
| 55 |
$this->initialize(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns the label tag associated to the field or null if none. |
| 60 |
* |
| 61 |
* @return \DOMElement|null |
| 62 |
*/ |
| 63 |
public function getLabel() |
| 64 |
{ |
| 65 |
$xpath = new \DOMXPath($this->node->ownerDocument); |
| 66 |
|
| 67 |
if ($this->node->hasAttribute('id')) { |
| 68 |
$labels = $xpath->query(sprintf('descendant::label[@for="%s"]', $this->node->getAttribute('id'))); |
| 69 |
if ($labels->length > 0) { |
| 70 |
return $labels->item(0); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
$labels = $xpath->query('ancestor::label[1]', $this->node); |
| 75 |
|
| 76 |
return $labels->length > 0 ? $labels->item(0) : null; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Returns the name of the field. |
| 81 |
* |
| 82 |
* @return string |
| 83 |
*/ |
| 84 |
public function getName() |
| 85 |
{ |
| 86 |
return $this->name; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Gets the value of the field. |
| 91 |
* |
| 92 |
* @return string|array|null |
| 93 |
*/ |
| 94 |
public function getValue() |
| 95 |
{ |
| 96 |
return $this->value; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Sets the value of the field. |
| 101 |
*/ |
| 102 |
public function setValue(?string $value) |
| 103 |
{ |
| 104 |
$this->value = $value ?? ''; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Returns true if the field should be included in the submitted values. |
| 109 |
* |
| 110 |
* @return bool |
| 111 |
*/ |
| 112 |
public function hasValue() |
| 113 |
{ |
| 114 |
return true; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Check if the current field is disabled. |
| 119 |
* |
| 120 |
* @return bool |
| 121 |
*/ |
| 122 |
public function isDisabled() |
| 123 |
{ |
| 124 |
return $this->node->hasAttribute('disabled'); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Initializes the form field. |
| 129 |
*/ |
| 130 |
abstract protected function initialize(); |
| 131 |
} |
| 132 |
|