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