567164d0461d6bca296607dcc85b2c1274787415
[yaffs-website] / vendor / symfony / dom-crawler / Field / FormField.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
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 <fabien@symfony.com>
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         if ($labels->length > 0) {
76             return $labels->item(0);
77         }
78
79         return;
80     }
81
82     /**
83      * Returns the name of the field.
84      *
85      * @return string The name of the field
86      */
87     public function getName()
88     {
89         return $this->name;
90     }
91
92     /**
93      * Gets the value of the field.
94      *
95      * @return string|array The value of the field
96      */
97     public function getValue()
98     {
99         return $this->value;
100     }
101
102     /**
103      * Sets the value of the field.
104      *
105      * @param string $value The value of the field
106      */
107     public function setValue($value)
108     {
109         $this->value = (string) $value;
110     }
111
112     /**
113      * Returns true if the field should be included in the submitted values.
114      *
115      * @return bool true if the field should be included in the submitted values, false otherwise
116      */
117     public function hasValue()
118     {
119         return true;
120     }
121
122     /**
123      * Check if the current field is disabled.
124      *
125      * @return bool
126      */
127     public function isDisabled()
128     {
129         return $this->node->hasAttribute('disabled');
130     }
131
132     /**
133      * Initializes the form field.
134      */
135     abstract protected function initialize();
136 }