-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOnceFields.php
380 lines (345 loc) · 9.97 KB
/
OnceFields.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/*
The OnceForm - Write once HTML5 forms processing for PHP.
Copyright (C) 2012-2013 adcSTUDIO LLC
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
class FieldType
{
public $tag_name;
public $field_class;
public $validator_class;
public $enumerable;
public $xpath_query;
public function __construct( $tag_name, $field_class,
$validator_class = 'OnceValidator', $enumerable = true,
$xpath_query = NULL )
{
$this->tag_name = $tag_name;
$this->field_class = $field_class;
$this->validator_class = $validator_class;
$this->enumerable = $enumerable;
if ( is_null( $xpath_query ) )
$this->xpath_query = "//{$tag_name}[@name]";
else
$this->xpath_query = $xpath_query;
$this->enumerable = $enumerable;
}
public function extract( DOMXpath $xpath )
{
$fields = array();
foreach( $xpath->query( $this->xpath_query ) as $node )
{
$r = new ReflectionClass( $this->field_class );
$fields[ $node->getAttribute('name') ] =
$r->newInstanceArgs( array( $node, $this ) );
}
return $fields;
}
}
class SubFieldType extends FieldType
{
public $type;
public function __construct( $tag_name, $type, $field_class,
$validator_class = 'OnceValidator', $enumerable = true )
{
parent::__construct( $tag_name, $field_class, $validator_class,
$enumerable, "//{$tag_name}[@type='$type' and @name]"
);
$this->type = $type;
}
}
interface iOnceField {
public function default_value();
public function value( $value = NULL );
public function name();
public function validator( iOnceValidator $validator = NULL );
public function required( $required = NULL );
public function field_type( FieldType $field_type = NULL );
public function validity();
}
abstract class OnceField implements iOnceField
{
protected $default_value;
public function default_value() {
return $this->default_value;
}
//abstract public function value( $value = NULL );
public function name() {
return $this->node->getAttribute('name');
}
protected $validator;
public function validator( iOnceValidator $validator = NULL )
{
if ( ! is_null( $validator ) )
$this->validator = $validator;
elseif ( $validator === false )
$this->validator = NULL;
if ( is_null( $this->validator ) ) {
if ( is_null( $this->field_type ) ) {
$this->validator = new OnceValidator( $this );
} else {
$r = new ReflectionClass( $this->field_type->validator_class );
$this->validator = $r->newInstanceArgs( array( $this ) );
}
}
return $this->validator;
}
public function required( $required = NULL )
{
$attr = 'required';
if ( !is_null($required) ) {
if ( $required )
$this->node->addAttribute($attr,$attr);
else
$this->node->removeAttribute($attr);
}
return (bool)$this->node->hasAttribute($attr);
}
public function __construct( DOMNode $node = NULL,
FieldType $field_type = NULL )
{
$this->node( $node );
$this->field_type( $field_type );
}
protected $field_type;
public function field_type( FieldType $field_type = NULL )
{
if ( !is_null( $field_type ) )
$this->field_type = $field_type;
return $this->field_type;
}
protected $node;
public function node( DOMNode $node = NULL )
{
if ( !is_null( $node ) )
$this->node = $node;
$this->default_value = $this->value();
return $this->node;
}
public function validity() {
return $this->validator()->isValid();
}
}
class InputField extends OnceField
{
public function value( $value = NULL )
{
if ( !is_null( $value ) )
$this->node->setAttribute('value', $value );
return $this->node->getAttribute('value');
}
}
class TextareaField extends InputField
{
public function value( $value = NULL )
{
$node = $this->node;
if ( !is_null( $value ) ) {
// remove all child nodes (including text nodes)
foreach ( $node->childNodes as $child )
$node->removeChild( $child );
// create and append a new text node
$node->appendChild(
$node->ownerDocument->createTextNode( $value )
);
}
return ( $value = $node->nodeValue ) ? $value : '';
}
}
class SelectField extends OnceField
{
public function multiple( $multiple = NULL )
{
return $this->node->hasAttribute('multiple');
}
public function value( $value = NULL )
{
$options = $this->node->getElementsByTagName('option');
if ( is_array($value) ) {
foreach( $options as $option ) {
if ( $option->hasAttribute('selected') )
$option->removeAttribute('selected');
if ( in_array( $this->get_option_value( $option ), $value ) )
$option->setAttribute('selected', 'selected');
}
}
elseif ( !is_null( $value ) ) {
foreach( $options as $option ) {
if ( $option->hasAttribute('selected') )
$option->removeAttribute('selected');
if ( $this->get_option_value( $option ) == $value )
$option->setAttribute('selected', 'selected');
}
}
$values = array();
foreach( $options as $option ) {
if ( $option->hasAttribute('selected') )
$values[] = $this->get_option_value( $option );
}
// if nothing is selected return '' default value
if ( 0 == count($values) )
$value = '';
// only returns the single selected item
elseif ( 1 == count( $values ) )
$value = $values[0];
// if there is more than one, return an array
else $value = $values;
return $value;
}
/**
* Gets the value of an option, which can be either a value attribute
* or the actual contents (nodeValue) of the optino.
* @param DOMNode $option The DOMNode of the option.
* @return string The value of the option.
*/
protected function get_option_value( DOMNode $option )
{
return ( $option->hasAttribute('value') )
? $option->getAttribute('value')
: $option->nodeValue;
}
}
/**
* The Checkbox Field. This field
*/
class CheckboxField extends InputField
{
protected $checked;
public function checked( $checked = NULL )
{
$node = $this->node;
if ( !is_null( $checked ) ) {
if ( $checked )
$node->setAttribute('checked', 'checked');
else
$node->removeAttribute('checked');
}
return $node->hasAttribute('checked');
}
/**
* In order to simulate the way checkbox elements post to the server
* to some extent, the value property cannot be changed here. This
* property can only accept and return the value of the value
* attribute, or an empty string if the checkbox is not checked.
* If you must set or get the actual value of the value field, use
* raw_value. Think of the value property of a OnceField as a
* request_value property. It's meant to normalize the request
* value, and not just to expose the value property of the DOMNode.
* @param string $value The value of the value attribute.
* @return string The value of the value attribute if check
* or an empty string if unchecked.
*/
public function value( $value = NULL )
{
if ( !is_null( $value ) ) {
$this->checked( !empty( $value ) );
}
return ( $this->checked() )? parent::value(): '';
}
public function raw_value( $value = NULL ) {
return parent::value( $value );
}
}
class RadioSetFieldType extends SubFieldType
{
public function __construct( $validator_class = 'OnceValidator',
$enumerable = true )
{
parent::__construct( 'input', 'radio', 'RadioSetField',
$validator_class, $enumerable, "//input[@type='radio' and @name]"
);
}
public function extract( DOMXpath $xpath )
{
$fields = array();
// We need to loop through all named elements, just as we do for other
// field types...
foreach( $xpath->query( $this->xpath_query ) as $node )
{
$name = $node->getAttribute('name');
if ( !isset( $fields[ $name ] ) ) {
// but we need to make sure to send a list of all radios with
// the same name to RadioSetFields, rather than single nodes.
$fields[ $name ] = new RadioSetField(
$xpath->query( "//input[@type='radio' and @name='$name']" ),
$this );
}
}
return $fields;
}
}
class RadioSetField extends OnceField
{
public function name()
{
$name = '';
if ( 0 < $this->nodes->length )
$name = $this->nodes->item(0)->getAttribute('name');
return $name;
}
public function required( $required = NULL )
{
if ( !is_null( $required ) ) {
if ( !$required )
foreach( $this->nodes as $node )
$node->removeAttribute('required');
else
foreach( $this->nodes as $node )
$node->setAttribute('required', 'required');
}
$required = false;
foreach( $this->nodes as $node )
if ( $node->hasAttribute('required') )
$required = true;
return $required;
}
public function __construct( DOMNodeList $nodes = NULL,
FieldType $field_type = NULL )
{
$this->nodes( $nodes );
$this->field_type( $field_type );
}
protected $nodes;
public function nodes( DOMNodeList $nodes = NULL )
{
if ( !is_null( $nodes ) ) {
$this->nodes = $nodes;
$this->default_value = $this->value();
if ( 0 < $nodes->length ) {
$this->name = $nodes->item(0)->getAttribute('name');
}
}
return $nodes;
}
public function value( $value = NULL )
{
if ( !is_null( $value ) ) {
foreach( $this->nodes as $node )
$node->removeAttribute('checked');
foreach( $this->nodes as $node )
if ( $node->hasAttribute('value') &&
$value == $node->getAttribute('value') )
$node->setAttribute('checked', 'checked');
}
$value = '';
foreach( $this->nodes as $node ) {
if ( $node->hasAttribute('checked') ) {
$value = ( $node->hasAttribute('value') )
? $node->getAttribute('value') : '';
break;
}
}
return $value;
}
}