Skip to content

Commit bb27332

Browse files
author
Christian Leucht
committed
Element::parent // introduce new methods Element::withParent() and Element::parent() to keep track of the parent CollectionElement/Form and track "submission"-state on all Elements.
Element::attributesForView // introduced new method which prepares the "id" and "name" for rendering to take the Element parents into account. Form, CollectionElement, Element // reduced amount of methods which are overwritten and ensure that with*() methods are not callable after submission.
1 parent 1a805c2 commit bb27332

22 files changed

+297
-170
lines changed

src/Element/ChoiceElement.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@ class ChoiceElement extends Element implements ChoiceElementInterface
1515
protected ?ChoiceListInterface $list = null;
1616

1717
/**
18-
* @param ChoiceListInterface $list
19-
*
20-
* @return static
18+
* {@inheritDoc}
2119
*/
2220
public function withChoices(ChoiceListInterface $list): static
2321
{
22+
$this->assertNotSubmitted(__METHOD__);
2423
$this->list = $list;
2524

2625
return $this;
2726
}
2827

2928
/**
30-
* @return ChoiceListInterface
29+
* {@inheritDoc}
3130
*/
3231
public function choices(): ChoiceListInterface
3332
{

src/Element/CollectionElement.php

+26-27
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,25 @@ class CollectionElement extends Element implements CollectionElementInterface
3131
private array $allErrors = [];
3232

3333
/**
34-
* @param ElementInterface[] $elements
35-
*
36-
* @return static
34+
* {@inheritDoc}
3735
*/
3836
public function withElement(ElementInterface ...$elements): static
3937
{
38+
$this->assertNotSubmitted(__METHOD__);
39+
4040
array_walk(
4141
$elements,
4242
function (ElementInterface $element): void {
4343
$this->elements[$element->name()] = $element;
44+
$element->withParent($this);
4445
}
4546
);
4647

4748
return $this;
4849
}
4950

5051
/**
51-
* @param string $name
52-
*
53-
* @return ElementInterface
54-
* @throws ElementNotFoundException
55-
*
52+
* {@inheritDoc}
5653
*/
5754
public function element(string $name): ElementInterface
5855
{
@@ -66,9 +63,7 @@ public function element(string $name): ElementInterface
6663
}
6764

6865
/**
69-
* @param string $name
70-
*
71-
* @return bool
66+
* {@inheritDoc}
7267
*/
7368
public function elementExists(string $name): bool
7469
{
@@ -78,18 +73,24 @@ public function elementExists(string $name): bool
7873
/**
7974
* If the key is "value" and the $value an array, we assign all values to the children.
8075
*
81-
* @param string $key
82-
* @param bool|int|string $value
83-
*
84-
* @return static
76+
* {@inheritDoc}
8577
*/
8678
public function withAttribute(string $key, $value): static
8779
{
80+
$this->assertNotSubmitted(__METHOD__);
81+
8882
if ($key === 'value' && is_array($value)) {
89-
foreach ($this->elements as $name => $element) {
90-
$this->elements[$name]->withValue($value[$name] ?? '');
83+
$assignedValues = [];
84+
foreach ($value as $elementName => $elementValue) {
85+
if (!$this->elementExists($elementName)) {
86+
continue;
87+
}
88+
$this->element($elementName)->withValue($elementValue);
89+
$assignedValues[$elementName] = $elementValue;
9190
}
9291

92+
$this->attributes['value'] = $assignedValues;
93+
9394
return $this;
9495
}
9596

@@ -101,9 +102,7 @@ public function withAttribute(string $key, $value): static
101102
/**
102103
* Returns a list of values for each element inside the collection.
103104
*
104-
* @param string $key
105-
*
106-
* @return array
105+
* {@inheritDoc}
107106
*/
108107
public function attribute(string $key)
109108
{
@@ -118,7 +117,7 @@ public function attribute(string $key)
118117
}
119118

120119
/**
121-
* @return array
120+
* {@inheritDoc}
122121
*/
123122
public function elements(): array
124123
{
@@ -128,16 +127,13 @@ public function elements(): array
128127
/**
129128
* Delegate errors down to the children.
130129
*
131-
* @param array $errors
132-
*
133-
* @return static
130+
* {@inheritDoc}
134131
*/
135132
public function withErrors(array $errors = []): static
136133
{
137134
$this->allErrors = $errors;
138135

139-
foreach ($this->elements as $element) {
140-
$name = $element->name();
136+
foreach ($this->elements as $name => $element) {
141137
if (isset($errors[$name]) && $element instanceof ErrorAwareInterface) {
142138
$element->withErrors((array) $errors[$name]);
143139
unset($errors[$name]);
@@ -151,7 +147,7 @@ public function withErrors(array $errors = []): static
151147
}
152148

153149
/**
154-
* @return bool
150+
* {@inheritDoc}
155151
*/
156152
public function hasErrors(): bool
157153
{
@@ -167,6 +163,9 @@ public function hasErrors(): bool
167163
return false;
168164
}
169165

166+
/**
167+
* {@inheritDoc}
168+
*/
170169
public function validate(): bool
171170
{
172171
$isValid = parent::validate();

0 commit comments

Comments
 (0)