-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathNode.php
More file actions
795 lines (720 loc) · 23.8 KB
/
Node.php
File metadata and controls
795 lines (720 loc) · 23.8 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
<?php
/**
* Base class for all HTML_QuickForm2 elements
*
* PHP version 5
*
* LICENSE
*
* This source file is subject to BSD 3-Clause License that is bundled
* with this package in the file LICENSE and available at the URL
* https://raw.githubusercontent.com/pear/HTML_QuickForm2/trunk/docs/LICENSE
*
* @category HTML
* @package HTML_QuickForm2
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <golgote@mamasam.com>
* @copyright 2006-2025 Alexey Borzov <avb@php.net>, Bertrand Mansion <golgote@mamasam.com>
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @link https://pear.php.net/package/HTML_QuickForm2
*/
// pear-package-only /**
// pear-package-only * HTML_Common2 - base class for HTML elements
// pear-package-only */
// pear-package-only require_once 'HTML/Common2.php';
// pear-package-only /**
// pear-package-only * Exception classes for HTML_QuickForm2
// pear-package-only */
// pear-package-only require_once 'HTML/QuickForm2/Exception.php';
// pear-package-only /**
// pear-package-only * Static factory class for QuickForm2 elements
// pear-package-only */
// pear-package-only require_once 'HTML/QuickForm2/Factory.php';
// pear-package-only /**
// pear-package-only * Base class for HTML_QuickForm2 rules
// pear-package-only */
// pear-package-only require_once 'HTML/QuickForm2/Rule.php';
/**
* Abstract base class for all QuickForm2 Elements and Containers
*
* This class is mostly here to define the interface that should be implemented
* by the subclasses. It also contains static methods handling generation
* of unique ids for elements which do not have ids explicitly set.
*
* @category HTML
* @package HTML_QuickForm2
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <golgote@mamasam.com>
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @version Release: @package_version@
* @link https://pear.php.net/package/HTML_QuickForm2
*/
abstract class HTML_QuickForm2_Node extends HTML_Common2
{
/**
* Name of option containing default language for various elements' messages
*/
const OPTION_LANGUAGE = 'language';
/**
* Name of option that toggles always appending a numeric index to generated id values
*
* By default, we generate element IDs with numeric indexes appended even for
* elements with unique names. If you want IDs to be equal to the element
* names by default, set this configuration option to false.
*/
const OPTION_ID_FORCE_APPEND_INDEX = 'id_force_append_index';
/**
* Name of option containing a value for "nonce" attribute of generated <script> tags
*/
const OPTION_NONCE = 'nonce';
/**
* Array containing the parts of element ids
* @var array
*/
protected static $ids = [];
/**
* Element's "frozen" status
* @var boolean
*/
protected $frozen = false;
/**
* Whether element's value should persist when element is frozen
* @var boolean
*/
protected $persistent = false;
/**
* Element containing current
* @var HTML_QuickForm2_Container|null
*/
protected $container = null;
/**
* Contains options and data used for the element creation
* @var array
*/
protected $data = [];
/**
* Validation rules for element
* @var array<int, array{HTML_QuickForm2_Rule, int}>
*/
protected $rules = [];
/**
* An array of callback filters for element
* @var array<int, array{callable, array}>
*/
protected $filters = [];
/**
* Recursive filter callbacks for element
*
* These are recursively applied for array values of element or propagated
* to contained elements if the element is a Container
*
* @var array<int, array{callable, array}>
*/
protected $recursiveFilters = [];
/**
* Error message (usually set via Rule if validation fails)
* @var string
*/
protected $error = '';
/**
* Changing 'name' and 'id' attributes requires some special handling
* @var string[]
*/
protected $watchedAttributes = ['id', 'name'];
/**
* Intercepts setting 'name' and 'id' attributes
*
* These attributes should always be present and thus trying to remove them
* will result in an exception. Changing their values is delegated to
* setName() and setId() methods, respectively
*
* @param string $name Attribute name
* @param string $value Attribute value, null if attribute is being removed
*
* @throws HTML_QuickForm2_InvalidArgumentException if trying to
* remove a required attribute
*/
protected function onAttributeChange($name, $value = null)
{
if ('name' == $name) {
if (null === $value) {
throw new HTML_QuickForm2_InvalidArgumentException(
"Required attribute 'name' can not be removed"
);
} else {
$this->setName($value);
}
} elseif ('id' == $name) {
if (null === $value) {
throw new HTML_QuickForm2_InvalidArgumentException(
"Required attribute 'id' can not be removed"
);
} else {
$this->setId($value);
}
}
}
/**
* Class constructor
*
* @param string $name Element name
* @param string|array $attributes HTML attributes (either a string or an array)
* @param array $data Element data (label, options used for element setup)
*/
public function __construct($name = null, $attributes = null, array $data = [])
{
parent::__construct($attributes);
$this->setName($name);
// Autogenerating the id if not set on previous steps
if ('' === (string)$this->getId()) {
$this->setId();
}
if (!empty($data)) {
$this->data = array_merge($this->data, $data);
}
}
/**
* Generates an id for the element
*
* Called when an element is created without explicitly given id
*
* @param string $elementName Element name
*
* @return string The generated element id
*/
protected static function generateId($elementName)
{
$stop = !self::getOption(self::OPTION_ID_FORCE_APPEND_INDEX);
$tokens = '' !== $elementName
? explode('[', str_replace(']', '', $elementName))
: ($stop? ['qfauto', ''] : ['qfauto']);
$container =& self::$ids;
$id = '';
do {
$token = array_shift($tokens);
// prevent generated ids starting with numbers
if ('' == $id && is_numeric($token)) {
$token = 'qf' . $token;
}
// Handle the 'array[]' names
if ('' === $token) {
if (empty($container)) {
$token = 0;
} else {
$keys = array_filter(array_keys($container), 'is_numeric');
$token = empty($keys) ? 0 : end($keys);
while (isset($container[$token])) {
$token++;
}
}
}
$id .= '-' . $token;
if (!isset($container[$token])) {
$container[$token] = [];
// Handle duplicate names when not having mandatory indexes
} elseif (empty($tokens) && $stop) {
$tokens[] = '';
}
// Handle mandatory indexes
if (empty($tokens) && !$stop) {
$tokens[] = '';
$stop = true;
}
$container =& $container[$token];
} while (!empty($tokens));
return (string)substr($id, 1);
}
/**
* Stores the explicitly given id to prevent duplicate id generation
*
* @param string $id Element id
*/
protected static function storeId($id)
{
$tokens = explode('-', $id);
$container =& self::$ids;
do {
$token = array_shift($tokens);
if (!isset($container[$token])) {
$container[$token] = [];
}
$container =& $container[$token];
} while (!empty($tokens));
}
/**
* Returns the element options
*
* @return array
*/
public function getData()
{
return $this->data;
}
/**
* Returns the element's type
*
* @return string
*/
abstract public function getType();
/**
* Returns the element's name
*
* @return string|null
*/
public function getName()
{
return $this->attributes['name'] ?? null;
}
/**
* Sets the element's name
*
* @param string|null $name
*
* @return $this
*/
abstract public function setName($name);
/**
* Returns the element's id
*
* @return string|null
*/
public function getId()
{
return $this->attributes['id'] ?? null;
}
/**
* Sets the element's id
*
* Please note that elements should always have an id in QuickForm2 and
* therefore it will not be possible to remove the element's id or set it to
* an empty value. If id is not explicitly given, it will be autogenerated.
*
* @param string|null $id Element's id, will be autogenerated if not given
*
* @return $this
* @throws HTML_QuickForm2_InvalidArgumentException if id contains invalid
* characters (i.e. spaces)
*/
public function setId($id = null)
{
if (is_null($id)) {
$id = self::generateId((string)$this->getName());
// HTML5 specification only disallows having space characters in id,
// so we don't do stricter checks here
} elseif (strpbrk($id, " \r\n\t\x0C")) {
throw new HTML_QuickForm2_InvalidArgumentException(
"The value of 'id' attribute should not contain space characters"
);
} else {
self::storeId($id);
}
$this->attributes['id'] = (string)$id;
return $this;
}
/**
* Returns the element's value without filters applied
*
* @return mixed
*/
abstract public function getRawValue();
/**
* Returns the element's value, possibly with filters applied
*
* @return mixed
*/
public function getValue()
{
$value = $this->getRawValue();
return is_null($value)? null: $this->applyFilters($value);
}
/**
* Sets the element's value
*
* @param mixed $value
*
* @return $this
*/
abstract public function setValue($value);
/**
* Returns the element's label(s)
*
* @return string|string[]|null
*/
public function getLabel()
{
if (isset($this->data['label'])) {
return $this->data['label'];
}
return null;
}
/**
* Sets the element's label(s)
*
* @param string|string[]|null $label Label for the element (may be an array of labels)
*
* @return $this
*/
public function setLabel($label)
{
$this->data['label'] = $label;
return $this;
}
/**
* Changes the element's frozen status
*
* @param bool $freeze Whether the element should be frozen or editable. If
* omitted, the method will not change the frozen status,
* just return its current value
*
* @return bool Old value of element's frozen status
*/
public function toggleFrozen($freeze = null)
{
$old = $this->frozen;
if (null !== $freeze) {
$this->frozen = (bool)$freeze;
}
return $old;
}
/**
* Changes the element's persistent freeze behaviour
*
* If persistent freeze is on, the element's value will be kept (and
* submitted) in a hidden field when the element is frozen.
*
* @param bool $persistent New value for "persistent freeze". If omitted, the
* method will not set anything, just return the current
* value of the flag.
*
* @return bool Old value of "persistent freeze" flag
*/
public function persistentFreeze($persistent = null)
{
$old = $this->persistent;
if (null !== $persistent) {
$this->persistent = (bool)$persistent;
}
return $old;
}
/**
* Adds the link to the element containing current
*
* @param ?HTML_QuickForm2_Container $container Element containing
* the current one, null if the link should
* really be removed (if removing from container)
*
* @throws HTML_QuickForm2_InvalidArgumentException If trying to set a
* child of an element as its container
*/
protected function setContainer(?HTML_QuickForm2_Container $container = null)
{
if (null !== $container) {
$check = $container;
do {
if ($this === $check) {
throw new HTML_QuickForm2_InvalidArgumentException(
'Cannot set an element or its child as its own container'
);
}
} while ($check = $check->getContainer());
if (null !== $this->container && $container !== $this->container) {
$this->container->removeChild($this);
}
}
$this->container = $container;
if (null !== $container) {
$this->updateValue();
}
}
/**
* Returns the element containing current
*
* @return HTML_QuickForm2_Container|null
*/
public function getContainer()
{
return $this->container;
}
/**
* Returns the data sources for this element
*
* @return array
*/
protected function getDataSources()
{
if (empty($this->container)) {
return [];
} else {
return $this->container->getDataSources();
}
}
/**
* Called when the element needs to update its value from form's data sources
*/
abstract protected function updateValue();
/**
* Adds a validation rule
*
* @param HTML_QuickForm2_Rule|string $rule Validation rule or rule type
* @param string|int $messageOrRunAt If first parameter is rule type,
* then message to display if validation fails, otherwise constant showing
* whether to perfom validation client-side and/or server-side
* @param mixed $options Configuration data for the rule
* @param int $runAt Whether to perfom validation
* server-side and/or client side. Combination of
* HTML_QuickForm2_Rule::SERVER and HTML_QuickForm2_Rule::CLIENT constants
*
* @return HTML_QuickForm2_Rule The added rule
* @throws HTML_QuickForm2_InvalidArgumentException if $rule is of a
* wrong type or rule name isn't registered with Factory
* @throws HTML_QuickForm2_NotFoundException if class for a given rule
* name cannot be found
*/
public function addRule(
$rule, $messageOrRunAt = '', $options = null,
$runAt = HTML_QuickForm2_Rule::SERVER
) {
if ($rule instanceof HTML_QuickForm2_Rule) {
$rule->setOwner($this);
$runAt = is_int($messageOrRunAt) ? $messageOrRunAt : HTML_QuickForm2_Rule::SERVER;
} elseif (is_string($rule)) {
$rule = HTML_QuickForm2_Factory::createRule($rule, $this, (string)$messageOrRunAt, $options);
} else {
throw new HTML_QuickForm2_InvalidArgumentException(
'addRule() expects either a rule type or ' .
'a HTML_QuickForm2_Rule instance'
);
}
$this->rules[] = [$rule, $runAt];
return $rule;
}
/**
* Removes a validation rule
*
* The method will *not* throw an Exception if the rule wasn't added to the
* element.
*
* @param HTML_QuickForm2_Rule $rule Validation rule to remove
*
* @return HTML_QuickForm2_Rule Removed rule
*/
public function removeRule(HTML_QuickForm2_Rule $rule)
{
foreach ($this->rules as $i => $r) {
if ($r[0] === $rule) {
unset($this->rules[$i]);
break;
}
}
return $rule;
}
/**
* Creates a validation rule
*
* This method is mostly useful when when chaining several rules together
* via {@link HTML_QuickForm2_Rule::and_()} and {@link HTML_QuickForm2_Rule::or_()}
* methods:
* <code>
* $first->addRule('nonempty', 'Fill in either first or second field')
* ->or_($second->createRule('nonempty'));
* </code>
*
* @param string $type Rule type
* @param string $message Message to display if validation fails
* @param mixed $options Configuration data for the rule
*
* @return HTML_QuickForm2_Rule The created rule
* @throws HTML_QuickForm2_InvalidArgumentException If rule type is unknown
* @throws HTML_QuickForm2_NotFoundException If class for the rule
* can't be found and/or loaded from file
*/
public function createRule($type, $message = '', $options = null)
{
return HTML_QuickForm2_Factory::createRule($type, $this, $message, $options);
}
/**
* Checks whether an element is required
*
* @return boolean
*/
public function isRequired()
{
foreach ($this->rules as $rule) {
if ($rule[0] instanceof HTML_QuickForm2_Rule_Required) {
return true;
}
}
return false;
}
/**
* Adds element's client-side validation rules to a builder object
*
* @param HTML_QuickForm2_JavascriptBuilder $builder
*/
protected function renderClientRules(HTML_QuickForm2_JavascriptBuilder $builder)
{
if ($this->toggleFrozen()) {
return;
}
$onblur = HTML_QuickForm2_Rule::ONBLUR_CLIENT ^ HTML_QuickForm2_Rule::CLIENT;
foreach ($this->rules as $rule) {
if (0 !== ($rule[1] & HTML_QuickForm2_Rule::CLIENT)) {
$builder->addRule($rule[0], 0 !== ($rule[1] & $onblur));
}
}
}
/**
* Performs the server-side validation
*
* @return boolean Whether the element is valid
*/
protected function validate()
{
foreach ($this->rules as $rule) {
if ('' !== $this->error) {
return false;
}
if (0 !== ($rule[1] & HTML_QuickForm2_Rule::SERVER)) {
$rule[0]->validate();
}
}
return '' === $this->error;
}
/**
* Sets the error message to the element
*
* @param string $error
*
* @return $this
*/
public function setError($error = null)
{
$this->error = (string)$error;
return $this;
}
/**
* Returns the error message for the element
*
* @return string
*/
public function getError()
{
return $this->error;
}
/**
* Returns Javascript code for getting the element's value
*
* @param bool $inContainer Whether it should return a parameter for
* qf.form.getContainerValue()
*
* @return string
*/
abstract public function getJavascriptValue($inContainer = false);
/**
* Returns IDs of form fields that should trigger "live" Javascript validation
*
* Rules added to this element with parameter HTML_QuickForm2_Rule::ONBLUR_CLIENT
* will be run by after these form elements change or lose focus
*
* @return array
*/
abstract public function getJavascriptTriggers();
/**
* Adds a filter
*
* A filter is simply a PHP callback which will be applied to the element value
* when getValue() is called.
*
* @param callable $callback The PHP callback used for filter
* @param array $options Optional arguments for the callback. The first parameter
* will always be the element value, then these options will
* be used as parameters for the callback.
*
* @return $this The element
* @throws HTML_QuickForm2_InvalidArgumentException If callback is incorrect
*/
public function addFilter($callback, array $options = [])
{
if (!is_callable($callback, false, $callbackName)) {
throw new HTML_QuickForm2_InvalidArgumentException(
"Filter should be a valid callback, '{$callbackName}' was given"
);
}
$this->filters[] = [$callback, $options];
return $this;
}
/**
* Adds a recursive filter
*
* A filter is simply a PHP callback which will be applied to the element value
* when getValue() is called. If the element value is an array, for example with
* selects of type 'multiple', the filter is applied to all values recursively.
* A filter on a container will not be applied on a container value but
* propagated to all contained elements instead.
*
* If the element is not a container and its value is not an array the behaviour
* will be identical to filters added via addFilter().
*
* @param callable $callback The PHP callback used for filter
* @param array $options Optional arguments for the callback. The first parameter
* will always be the element value, then these options will
* be used as parameters for the callback.
*
* @return $this The element
* @throws HTML_QuickForm2_InvalidArgumentException If callback is incorrect
*/
public function addRecursiveFilter($callback, array $options = [])
{
if (!is_callable($callback, false, $callbackName)) {
throw new HTML_QuickForm2_InvalidArgumentException(
"Filter should be a valid callback, '{$callbackName}' was given"
);
}
$this->recursiveFilters[] = [$callback, $options];
return $this;
}
/**
* Helper function for applying filter callback to a value
*
* @param mixed &$value Value being filtered
* @param mixed $key Array key (not used, present to be able to use this
* method as a callback to array_walk_recursive())
* @param array $filter Array containing callback and additional callback
* parameters
*/
protected static function applyFilter(&$value, $key, $filter)
{
[$callback, $options] = $filter;
array_unshift($options, $value);
$value = call_user_func_array($callback, $options);
}
/**
* Applies non-recursive filters on element value
*
* @param mixed $value Element value
*
* @return mixed Filtered value
*/
protected function applyFilters($value)
{
foreach ($this->filters as $filter) {
self::applyFilter($value, null, $filter);
}
return $value;
}
/**
* Renders the element using the given renderer
*
* @param HTML_QuickForm2_Renderer $renderer
* @return HTML_QuickForm2_Renderer
*/
abstract public function render(HTML_QuickForm2_Renderer $renderer);
}
// set default values for document-wide options
if (null === HTML_Common2::getOption(HTML_QuickForm2_Node::OPTION_ID_FORCE_APPEND_INDEX)) {
HTML_Common2::setOption(HTML_QuickForm2_Node::OPTION_ID_FORCE_APPEND_INDEX, true);
}
if (null === HTML_Common2::getOption(HTML_QuickForm2_Node::OPTION_LANGUAGE)) {
HTML_Common2::setOption(HTML_QuickForm2_Node::OPTION_LANGUAGE, 'en');
}
?>