-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathFieldSet.php
More file actions
108 lines (88 loc) · 2.47 KB
/
Copy pathFieldSet.php
File metadata and controls
108 lines (88 loc) · 2.47 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
<?php
declare(strict_types=1);
namespace PHPCensor\Form;
use PHPCensor\View;
/**
* @package PHP Censor
* @subpackage Application
*
* @author Dan Cryer <dan@block8.co.uk>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class FieldSet extends Element
{
/**
* @var Element[]
*/
protected array $children = [];
public function getValues(): array
{
$rtn = [];
foreach ($this->children as $field) {
if ($field instanceof FieldSet) {
$fieldName = $field->getName();
if (empty($fieldName)) {
$rtn = \array_merge($rtn, $field->getValues());
} else {
$rtn[$fieldName] = $field->getValues();
}
} elseif ($field instanceof Input) {
if ($field->getName()) {
$rtn[$field->getName()] = $field->getValue();
}
}
}
return $rtn;
}
public function setValues(array $values): self
{
foreach ($this->children as $field) {
if ($field instanceof FieldSet) {
$fieldName = $field->getName();
if (empty($fieldName) || !isset($values[$fieldName])) {
$field->setValues($values);
} else {
$field->setValues($values[$fieldName]);
}
} elseif ($field instanceof Input) {
$fieldName = $field->getName();
if (isset($values[$fieldName])) {
$field->setValue($values[$fieldName]);
}
}
}
return $this;
}
public function addField(Element $field): self
{
$this->children[$field->getName()] = $field;
$field->setParent($this);
return $this;
}
public function validate(): bool
{
$rtn = true;
foreach ($this->children as $child) {
if (!$child->validate()) {
$rtn = false;
}
}
return $rtn;
}
protected function onPreRender(View &$view): void
{
$rendered = [];
foreach ($this->children as $child) {
$rendered[] = $child->render();
}
$view->children = $rendered;
}
public function getChildren(): array
{
return $this->children;
}
public function getChild(string $fieldName): Element
{
return $this->children[$fieldName];
}
}