1 | <?php |
||
13 | class Driver_Simple_Forms |
||
14 | { |
||
15 | /** |
||
16 | * DOMXPath object to perform queries with |
||
17 | * @var DOMXPath |
||
18 | */ |
||
19 | protected $_xpath; |
||
20 | |||
21 | 48 | function __construct($xpath) |
|
25 | |||
26 | /** |
||
27 | * Get the value of a DOMElement with a given xpath |
||
28 | * @param string $xpath |
||
29 | * @return mixed |
||
30 | */ |
||
31 | 7 | public function get_value($xpath) |
|
32 | { |
||
33 | 7 | $node = $this->xpath->find($xpath); |
|
34 | |||
35 | 7 | switch ($node->tagName) |
|
36 | { |
||
37 | 7 | case 'textarea': |
|
38 | 6 | return $node->textContent; |
|
39 | break; |
||
40 | |||
41 | 6 | case 'select': |
|
42 | 5 | $options = array(); |
|
43 | 5 | foreach ($this->xpath->query(".//option[@selected]", $node) as $option) |
|
44 | { |
||
45 | 5 | $options[] = $option->hasAttribute('value') ? $option->getAttribute('value') : $option->textContent; |
|
46 | } |
||
47 | 5 | return $node->hasAttribute('multiple') ? $options : (isset($options[0]) ? $options[0] : NULL); |
|
48 | break; |
||
49 | default: |
||
50 | 6 | return $node->getAttribute('value'); |
|
51 | } |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Set the value of a DOMElement, identified by an xpath, calls one of the stter methods |
||
56 | * @param string $xpath |
||
57 | * @param mixed $value |
||
58 | */ |
||
59 | 4 | public function set_value($xpath, $value) |
|
60 | { |
||
61 | 4 | $node = $this->xpath->find($xpath); |
|
62 | |||
63 | 4 | $setter = 'set_value_input'; |
|
64 | 4 | $type = $node->getAttribute('type'); |
|
65 | |||
66 | 4 | if ($node->tagName == 'input' AND $type == 'checkbox') |
|
67 | { |
||
68 | 3 | $setter = 'set_value_checkbox'; |
|
69 | } |
||
70 | |||
71 | 4 | elseif ($node->tagName == 'input' AND $type == 'radio') |
|
72 | { |
||
73 | 3 | $setter = 'set_value_radio'; |
|
74 | } |
||
75 | |||
76 | 4 | elseif ($node->tagName == 'textarea') |
|
77 | { |
||
78 | 3 | $setter = 'set_value_textarea'; |
|
79 | } |
||
80 | |||
81 | 4 | elseif ($node->tagName == 'option') |
|
82 | { |
||
83 | 4 | $setter = 'set_value_option'; |
|
84 | } |
||
85 | |||
86 | 4 | $this->{$setter}($node, $value); |
|
87 | 4 | } |
|
88 | |||
89 | /** |
||
90 | * Set the value of a checkbos DOMNode |
||
91 | * @param DOMNode $checkbox |
||
92 | * @param boolean $value |
||
93 | */ |
||
94 | 3 | public function set_value_checkbox(\DOMNode $checkbox, $value) |
|
95 | { |
||
96 | 3 | if ($value) |
|
97 | { |
||
98 | 3 | $checkbox->setAttribute('checked', 'checked'); |
|
99 | } |
||
100 | else |
||
101 | { |
||
102 | 1 | $checkbox->removeAttribute('checked'); |
|
103 | } |
||
104 | 3 | } |
|
105 | |||
106 | /** |
||
107 | * Set the value of a radio DOMNode, uncheck any other radio input in the same group |
||
108 | * @param DOMNode $radio |
||
109 | * @param boolean $value |
||
110 | */ |
||
111 | 3 | public function set_value_radio(\DOMNode $radio, $value) |
|
112 | { |
||
113 | 3 | $name = $radio->getAttribute('name'); |
|
114 | 3 | foreach ($this->xpath->query("//input[@type='radio' and @name='$name' and @checked]") as $other_radio) |
|
115 | { |
||
116 | 3 | $other_radio->removeAttribute('checked'); |
|
117 | } |
||
118 | 3 | if ($value) |
|
119 | { |
||
120 | 2 | $radio->setAttribute('checked', 'checked'); |
|
121 | } |
||
122 | 3 | } |
|
123 | |||
124 | /** |
||
125 | * Set the value of a normal input |
||
126 | * @param DOMNode $input |
||
127 | * @param string $value |
||
128 | */ |
||
129 | 2 | public function set_value_input(\DOMNode $input, $value) |
|
133 | |||
134 | /** |
||
135 | * Set the value of a normal textarea |
||
136 | * @param DOMNode $textarea |
||
137 | * @param string $value |
||
138 | */ |
||
139 | 3 | public function set_value_textarea(\DOMNode $textarea, $value) |
|
143 | |||
144 | /** |
||
145 | * Set the value of an option DOMNode, unselect other options in this select, if it is not multiple |
||
146 | * @param DOMNode $option |
||
147 | * @param boolean $value |
||
148 | */ |
||
149 | 4 | public function set_value_option(\DOMNode $option, $value) |
|
170 | |||
171 | /** |
||
172 | * Return all the contents of file inputs in a form, identified by an xpath |
||
173 | * @param string $xpath |
||
174 | * @return string |
||
175 | */ |
||
176 | 2 | public function serialize_files($xpath) |
|
189 | |||
190 | /** |
||
191 | * Return the contents of all the inputs from a form, identified by an xpath. |
||
192 | * Don't include file inputs or disabled inputs |
||
193 | * @param string $xpath |
||
194 | * @return string |
||
195 | */ |
||
196 | 3 | public function serialize_form($xpath) |
|
227 | } |
||
228 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.