blob: 0d70ea4c7e067b5cc58ef95610502c2fec77e08f (
plain)
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
|
<?php
namespace ipl\Html\FormElement;
use InvalidArgumentException;
use ipl\Html\Contract\FormElement;
use ipl\Html\Contract\FormElementDecorator;
use ipl\Html\Contract\Wrappable;
use LogicException;
use function ipl\Stdlib\get_php_type;
class FieldsetElement extends BaseFormElement
{
use FormElements {
FormElements::getValue as private getElementValue;
}
protected $tag = 'fieldset';
/**
* Get whether any of this set's elements has a value
*
* @return bool
*/
public function hasValue()
{
foreach ($this->getElements() as $element) {
if ($element->hasValue()) {
return true;
}
}
return false;
}
public function getValue($name = null, $default = null)
{
if ($name === null) {
if ($default !== null) {
throw new LogicException("Can't provide default without a name");
}
return $this->getValues();
}
return $this->getElementValue($name, $default);
}
public function setValue($value)
{
if (! is_iterable($value)) {
throw new InvalidArgumentException(
sprintf(
'%s expects parameter $value to be an array|iterable, got %s instead',
__METHOD__,
get_php_type($value)
)
);
}
// We expect an array/iterable here,
// so call populate to loop through it and apply values to the child elements of the fieldset.
$this->populate($value);
return $this;
}
public function validate()
{
$this->ensureAssembled();
$this->valid = true;
foreach ($this->getElements() as $element) {
$element->validate();
if (! $element->isValid()) {
$this->valid = false;
}
}
if ($this->valid) {
parent::validate();
}
return $this;
}
public function getValueAttribute()
{
// Fieldsets do not have the value attribute.
return null;
}
public function setWrapper(Wrappable $wrapper)
{
// TODO(lippserd): Revise decorator implementation to properly implement decorator propagation
if (
! $this->hasDefaultElementDecorator()
&& $wrapper instanceof FormElementDecorator
) {
$this->setDefaultElementDecorator(clone $wrapper);
}
return parent::setWrapper($wrapper);
}
protected function onElementRegistered(FormElement $element)
{
$element->getAttributes()->registerAttributeCallback('name', function () use ($element) {
/**
* We don't change the {@see BaseFormElement::$name} property of the element,
* otherwise methods like {@see FormElements::populate() and {@see FormElements::getElement()} would fail,
* but only change the name attribute to nest the names.
*/
return sprintf(
'%s[%s]',
$this->getValueOfNameAttribute(),
$element->getName()
);
});
}
}
|