Debian Codegraph @

vendor/ipl/html/src/FormElement/FieldsetElement.php

raw
1 <?php
2
3 namespace ipl\Html\FormElement;
4
5 use InvalidArgumentException;
6 use ipl\Html\Common\MultipleAttribute;
7 use ipl\Html\Contract\DefaultFormElementDecoration;
8 use ipl\Html\Contract\FormElement;
9 use ipl\Html\Contract\FormElementDecorator;
10 use ipl\Html\Contract\Wrappable;
11 use LogicException;
12
13 use function ipl\Stdlib\get_php_type;
14
15 class FieldsetElement extends BaseFormElement implements \ipl\Html\Contract\FormElements, DefaultFormElementDecoration
16 {
17 use FormElements {
18 FormElements::getValue as private getElementValue;
19 }
20
21 protected $tag = 'fieldset';
22
23 /**
24 * Get whether any of this set's elements has a value
25 *
26 * @return bool
27 */
28 public function hasValue()
29 {
30 $this->ensureAssembled();
31
32 foreach ($this->getElements() as $element) {
33 if ($element->hasValue()) {
34 return true;
35 }
36 }
37
38 return false;
39 }
40
41 public function getValue($name = null, $default = null)
42 {
43 if ($name === null) {
44 if ($default !== null) {
45 throw new LogicException("Can't provide default without a name");
46 }
47
48 return $this->getValues();
49 }
50
51 return $this->getElementValue($name, $default);
52 }
53
54 public function setValue($value)
55 {
56 if (! is_iterable($value)) {
57 throw new InvalidArgumentException(
58 sprintf(
59 '%s expects parameter $value to be an array|iterable, got %s instead',
60 __METHOD__,
61 get_php_type($value)
62 )
63 );
64 }
65
66 // We expect an array/iterable here,
67 // so call populate to loop through it and apply values to the child elements of the fieldset.
68 $this->populate($value);
69
70 return $this;
71 }
72
73 public function validate()
74 {
75 $this->ensureAssembled();
76
77 $this->valid = true;
78 foreach ($this->getElements() as $element) {
79 $element->validate();
80 if (! $element->isValid()) {
81 $this->valid = false;
82 }
83 }
84
85 if ($this->valid) {
86 parent::validate();
87 }
88
89 return $this;
90 }
91
92 public function getValueAttribute()
93 {
94 // Fieldsets do not have the value attribute.
95 return null;
96 }
97
98 public function setWrapper(Wrappable $wrapper)
99 {
100 // TODO(lippserd): Revise decorator implementation to properly implement decorator propagation
101 if (
102 ! $this->hasDefaultElementDecorator()
103 && $wrapper instanceof FormElementDecorator
104 ) {
105 $this->setDefaultElementDecorator(clone $wrapper);
106 }
107
108 return parent::setWrapper($wrapper);
109 }
110
111 protected function onElementRegistered(FormElement $element)
112 {
113 $element->getAttributes()->registerAttributeCallback('name', function () use ($element) {
114 $multiple = false;
115 if (in_array(MultipleAttribute::class, class_uses($element), true)) {
116 $multiple = $element->isMultiple();
117 }
118
119 /**
120 * We don't change the {@see BaseFormElement::$name} property of the element,
121 * otherwise methods like {@see FormElements::populate() and {@see FormElements::getElement()} would fail,
122 * but only change the name attribute to nest the names.
123 */
124 return sprintf(
125 '%s[%s]%s',
126 $this->getValueOfNameAttribute(),
127 $element->getName(),
128 $multiple ? '[]' : ''
129 );
130 });
131 }
132 }