Yaffs site version 1.1
[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      * Constructor.
48      *
49      * @param \DOMElement $node The node associated with this field
50      */
51     public function __construct(\DOMElement $node)
52     {
53         $this->node = $node;
54         $this->name = $node->getAttribute('name');
55         $this->xpath = new \DOMXPath($node->ownerDocument);
56
57         $this->initialize();
58     }
59
60     /**
61      * Returns the label tag associated to the field or null if none.
62      *
63      * @return \DOMElement|null
64      */
65     public function getLabel()
66     {
67         $xpath = new \DOMXPath($this->node->ownerDocument);
68
69         if ($this->node->hasAttribute('id')) {
70             $labels = $xpath->query(sprintf('descendant::label[@for="%s"]', $this->node->getAttribute('id')));
71             if ($labels->length > 0) {
72                 return $labels->item(0);
73             }
74         }
75
76         $labels = $xpath->query('ancestor::label[1]', $this->node);
77         if ($labels->length > 0) {
78             return $labels->item(0);
79         }
80
81         return;
82     }
83
84     /**
85      * Returns the name of the field.
86      *
87      * @return string The name of the field
88      */
89     public function getName()
90     {
91         return $this->name;
92     }
93
94     /**
95      * Gets the value of the field.
96      *
97      * @return string|array The value of the field
98      */
99     public function getValue()
100     {
101         return $this->value;
102     }
103
104     /**
105      * Sets the value of the field.
106      *
107      * @param string $value The value of the field
108      */
109     public function setValue($value)
110     {
111         $this->value = (string) $value;
112     }
113
114     /**
115      * Returns true if the field should be included in the submitted values.
116      *
117      * @return bool true if the field should be included in the submitted values, false otherwise
118      */
119     public function hasValue()
120     {
121         return true;
122     }
123
124     /**
125      * Check if the current field is disabled.
126      *
127      * @return bool
128      */
129     public function isDisabled()
130     {
131         return $this->node->hasAttribute('disabled');
132     }
133
134     /**
135      * Initializes the form field.
136      */
137     abstract protected function initialize();
138 }