This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Form.php
183 lines (152 loc) · 3.44 KB
/
Form.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace Formjack;
use Formjack\Field\AbstractField;
use Formjack\Layout\AbstractLayout;
use Formjack\Layout\DefaultLayout;
class Form {
/**
* @var string Form name
*/
private $name;
/**
* @var AbstractField[] Array of form fields
*/
private $fields;
/**
* @var AbstractLayout Layout instance
*/
private $layout;
/**
* @var array Array of validation errors
*/
private $errors;
/**
* @param array Array of form fields
*/
public function __construct($name, array $fields = array()) {
$this
->setName($name)
->setFields($fields)
->setLayout(new DefaultLayout())
;
}
/**
* @param string $name
*/
public function setName($name) {
$this->name = str_replace(' ', '_', $name);
return $this;
}
/**
* @return string
*/
public function getName() {
return $this->name;
}
/**
* @param array $fields
*/
public function setFields(array $fields) {
foreach ($fields as $field) {
$this->addField($field);
}
return $this;
}
/**
* @param AbstractField $field
* @return $this
*/
public function addField(AbstractField $field) {
$field->setParent($this);
$this->fields[$field->getId()] = $field;
return $this;
}
/**
* @param string $name
* @return $this
*/
public function removeField($name) {
if (isset($this->fields[$name])) {
unset($this->fields[$name]);
}
return $this;
}
/**
* @return array
*/
public function getFields() {
return $this->fields;
}
/**
* @param AbstractLayout $layout
* @return $this
*/
public function setLayout(AbstractLayout $layout) {
$this->layout = $layout;
return $this;
}
/**
* @param array $data
* @return $this
*/
public function bind(array $data = array()) {
foreach ($this->fields as $field) {
if (isset($data[$field->getId()])) {
$field->bind($data[$field->getId()]);
} else {
$field->bind(null);
}
}
return $this;
}
/**
* @return bool
*/
public function isValid() {
foreach ($this->fields as $field) {
$result = Validator::validateField($field);
if (!$result->valid) {
$this->errors[$field->getId()] = $result->trace;
}
}
return empty($this->errors);
}
/**
* @return bool
*/
public function hasErrors() {
return !empty($this->errors);
}
/**
* @return array
*/
public function getErrors() {
return $this->errors;
}
/**
* @return array
*/
public function getData() {
$result = array();
foreach ($this->fields as $field) {
$result[$field->getId()] = $field->getValue();
}
return $result;
}
/**
* @return void
*/
public function render() {
foreach ($this->fields as $name => $instance) {
$this->renderField($name);
}
}
/**
* @return void
*/
public function renderField($name) {
if (isset($this->fields[$name])) {
echo $this->layout->renderField($this->fields[$name]);
}
}
}