forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldSet.php
More file actions
118 lines (101 loc) · 2.5 KB
/
Copy pathFieldSet.php
File metadata and controls
118 lines (101 loc) · 2.5 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
109
110
111
112
113
114
115
116
117
118
<?php
namespace PHPCensor\Form;
use PHPCensor\View;
class FieldSet extends Element
{
/**
* @var Element[]
*/
protected $children = [];
/**
* @return array
*/
public function getValues()
{
$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;
}
/**
* @param array $values
*/
public function setValues(array $values)
{
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]);
}
}
}
}
/**
* @param Element $field
*/
public function addField(Element $field)
{
$this->children[$field->getName()] = $field;
$field->setParent($this);
}
/**
* @return boolean
*/
public function validate()
{
$rtn = true;
foreach ($this->children as $child) {
if (!$child->validate()) {
$rtn = false;
}
}
return $rtn;
}
/**
* @param View $view
*/
protected function onPreRender(View &$view)
{
$rendered = [];
foreach ($this->children as $child) {
$rendered[] = $child->render();
}
$view->children = $rendered;
}
/**
* @return Element[]
*/
public function getChildren()
{
return $this->children;
}
/**
* @param string $fieldName
*
* @return Element
*/
public function getChild($fieldName)
{
return $this->children[$fieldName];
}
}