|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.6 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace WebHemi\Form\Element\Web; |
|
13
|
|
|
|
|
14
|
|
|
use WebHemi\Form\Element\FormElementInterface; |
|
15
|
|
|
use WebHemi\Form\Element\NestedElementInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class FormElement. |
|
19
|
|
|
*/ |
|
20
|
|
|
class FormElement extends AbstractElement implements NestedElementInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
protected $type = 'form'; |
|
24
|
|
|
/** @var array<FormElementInterface> */ |
|
25
|
|
|
protected $nodes = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Set the child nodes for the element. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array<FormElementInterface> $nodeElements |
|
31
|
|
|
* @return FormElement |
|
32
|
|
|
*/ |
|
33
|
10 |
|
public function setNodes(array $nodeElements) |
|
34
|
|
|
{ |
|
35
|
|
|
/** @var NestedElementInterface $this */ |
|
36
|
10 |
|
$this->nodes = []; |
|
37
|
|
|
|
|
38
|
10 |
|
foreach ($nodeElements as $nodeElement) { |
|
39
|
10 |
|
$this->addNode($nodeElement); |
|
40
|
10 |
|
} |
|
41
|
|
|
|
|
42
|
10 |
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Set child node for the element. |
|
47
|
|
|
* |
|
48
|
|
|
* @param FormElementInterface $nodeElement |
|
49
|
|
|
* @return FormElement |
|
50
|
|
|
*/ |
|
51
|
10 |
|
protected function addNode(FormElementInterface $nodeElement) |
|
52
|
|
|
{ |
|
53
|
10 |
|
$nodeElement->setParentNode($this); |
|
54
|
10 |
|
$this->nodes[$nodeElement->getName(false)] = $nodeElement; |
|
55
|
|
|
|
|
56
|
10 |
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Checks if there are child elements. |
|
61
|
|
|
* |
|
62
|
|
|
* @return boolean |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function hasNodes() |
|
65
|
|
|
{ |
|
66
|
1 |
|
return !empty($this->nodes); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Gets the child nodes of the element. |
|
71
|
|
|
* |
|
72
|
|
|
* @return array<FormElementInterface> |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function getNodes() |
|
75
|
|
|
{ |
|
76
|
4 |
|
return $this->nodes; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Sets element value. |
|
81
|
|
|
* |
|
82
|
|
|
* @param mixed $value |
|
83
|
|
|
* @return FormElement |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function setValue($value) |
|
86
|
|
|
{ |
|
87
|
1 |
|
$children = $this->getNodes(); |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @var string $simpleName |
|
91
|
|
|
* @var FormElementInterface $child |
|
92
|
|
|
*/ |
|
93
|
1 |
|
foreach ($children as $simpleName => $child) { |
|
94
|
1 |
|
if (isset($value[$simpleName])) { |
|
95
|
1 |
|
$child->setValue($value[$simpleName]); |
|
96
|
1 |
|
} |
|
97
|
1 |
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Returns element value. |
|
104
|
|
|
* |
|
105
|
|
|
* @return mixed |
|
106
|
|
|
*/ |
|
107
|
1 |
|
public function getValue() |
|
108
|
|
|
{ |
|
109
|
1 |
|
$children = $this->getNodes(); |
|
110
|
1 |
|
$value = []; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @var string $simpleName |
|
114
|
|
|
* @var FormElementInterface $child |
|
115
|
|
|
*/ |
|
116
|
1 |
|
foreach ($children as $simpleName => $child) { |
|
117
|
1 |
|
$value[$simpleName] = $child->getValue(); |
|
118
|
1 |
|
} |
|
119
|
|
|
|
|
120
|
1 |
|
return $value; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Validates element value. |
|
125
|
|
|
* |
|
126
|
|
|
* @param bool $reValidate |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
2 |
|
public function isValid($reValidate = false) |
|
130
|
|
|
{ |
|
131
|
2 |
|
$children = $this->getNodes(); |
|
132
|
2 |
|
$isValid = true; |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @var string $simpleName |
|
136
|
|
|
* @var FormElementInterface $child |
|
137
|
|
|
*/ |
|
138
|
2 |
|
foreach ($children as $child) { |
|
139
|
2 |
|
$isValid = $isValid && $child->isValid($reValidate); |
|
140
|
2 |
|
} |
|
141
|
|
|
|
|
142
|
2 |
|
return $isValid; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Returns element value. |
|
147
|
|
|
* |
|
148
|
|
|
* @return mixed |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function getErrors() |
|
151
|
|
|
{ |
|
152
|
1 |
|
$children = $this->getNodes(); |
|
153
|
1 |
|
$error = []; |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @var string $simpleName |
|
157
|
|
|
* @var FormElementInterface $child |
|
158
|
|
|
*/ |
|
159
|
1 |
|
foreach ($children as $simpleName => $child) { |
|
160
|
1 |
|
$childErrors = $child->getErrors(); |
|
161
|
1 |
|
if (!empty($childErrors)) { |
|
162
|
1 |
|
$error[$simpleName] = $childErrors; |
|
163
|
1 |
|
} |
|
164
|
1 |
|
} |
|
165
|
|
|
|
|
166
|
1 |
|
return $error; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|