@@ -31,28 +31,25 @@ class CollectionElement extends Element implements CollectionElementInterface
31
31
private array $ allErrors = [];
32
32
33
33
/**
34
- * @param ElementInterface[] $elements
35
- *
36
- * @return static
34
+ * {@inheritDoc}
37
35
*/
38
36
public function withElement (ElementInterface ...$ elements ): static
39
37
{
38
+ $ this ->assertNotSubmitted (__METHOD__ );
39
+
40
40
array_walk (
41
41
$ elements ,
42
42
function (ElementInterface $ element ): void {
43
43
$ this ->elements [$ element ->name ()] = $ element ;
44
+ $ element ->withParent ($ this );
44
45
}
45
46
);
46
47
47
48
return $ this ;
48
49
}
49
50
50
51
/**
51
- * @param string $name
52
- *
53
- * @return ElementInterface
54
- * @throws ElementNotFoundException
55
- *
52
+ * {@inheritDoc}
56
53
*/
57
54
public function element (string $ name ): ElementInterface
58
55
{
@@ -66,9 +63,7 @@ public function element(string $name): ElementInterface
66
63
}
67
64
68
65
/**
69
- * @param string $name
70
- *
71
- * @return bool
66
+ * {@inheritDoc}
72
67
*/
73
68
public function elementExists (string $ name ): bool
74
69
{
@@ -78,18 +73,24 @@ public function elementExists(string $name): bool
78
73
/**
79
74
* If the key is "value" and the $value an array, we assign all values to the children.
80
75
*
81
- * @param string $key
82
- * @param bool|int|string $value
83
- *
84
- * @return static
76
+ * {@inheritDoc}
85
77
*/
86
78
public function withAttribute (string $ key , $ value ): static
87
79
{
80
+ $ this ->assertNotSubmitted (__METHOD__ );
81
+
88
82
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 ;
91
90
}
92
91
92
+ $ this ->attributes ['value ' ] = $ assignedValues ;
93
+
93
94
return $ this ;
94
95
}
95
96
@@ -101,9 +102,7 @@ public function withAttribute(string $key, $value): static
101
102
/**
102
103
* Returns a list of values for each element inside the collection.
103
104
*
104
- * @param string $key
105
- *
106
- * @return array
105
+ * {@inheritDoc}
107
106
*/
108
107
public function attribute (string $ key )
109
108
{
@@ -118,7 +117,7 @@ public function attribute(string $key)
118
117
}
119
118
120
119
/**
121
- * @return array
120
+ * {@inheritDoc}
122
121
*/
123
122
public function elements (): array
124
123
{
@@ -128,16 +127,13 @@ public function elements(): array
128
127
/**
129
128
* Delegate errors down to the children.
130
129
*
131
- * @param array $errors
132
- *
133
- * @return static
130
+ * {@inheritDoc}
134
131
*/
135
132
public function withErrors (array $ errors = []): static
136
133
{
137
134
$ this ->allErrors = $ errors ;
138
135
139
- foreach ($ this ->elements as $ element ) {
140
- $ name = $ element ->name ();
136
+ foreach ($ this ->elements as $ name => $ element ) {
141
137
if (isset ($ errors [$ name ]) && $ element instanceof ErrorAwareInterface) {
142
138
$ element ->withErrors ((array ) $ errors [$ name ]);
143
139
unset($ errors [$ name ]);
@@ -151,7 +147,7 @@ public function withErrors(array $errors = []): static
151
147
}
152
148
153
149
/**
154
- * @return bool
150
+ * {@inheritDoc}
155
151
*/
156
152
public function hasErrors (): bool
157
153
{
@@ -167,6 +163,9 @@ public function hasErrors(): bool
167
163
return false ;
168
164
}
169
165
166
+ /**
167
+ * {@inheritDoc}
168
+ */
170
169
public function validate (): bool
171
170
{
172
171
$ isValid = parent ::validate ();
0 commit comments