1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Amarkal\UI; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Implements a Form controller. |
7
|
|
|
* |
8
|
|
|
* The form object is used to encapsulate UI components and the update/validation |
9
|
|
|
* process into a single entity. |
10
|
|
|
*/ |
11
|
|
|
class Form |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The list of AbstractComponent type objects to be updated. |
15
|
|
|
* |
16
|
|
|
* @var AbstractComponent[] objects array. |
17
|
|
|
*/ |
18
|
|
|
private $components; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The old component values array. These values are used if the new values |
22
|
|
|
* are invalid. |
23
|
|
|
* Structure: component_name => component_value |
24
|
|
|
* |
25
|
|
|
* @var array Old values array. |
26
|
|
|
*/ |
27
|
|
|
private $old_instance; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The new component values array (retrieved from the POST request). |
31
|
|
|
* Structure: component_name => component_value |
32
|
|
|
* |
33
|
|
|
* @var array New values array. |
34
|
|
|
*/ |
35
|
|
|
private $new_instance; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The final values array, after filtering and validation. |
39
|
|
|
* This is returned by the update function. |
40
|
|
|
* |
41
|
|
|
* @var array Final values array. |
42
|
|
|
*/ |
43
|
|
|
private $final_instance = array(); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Array of names of components that were invalid, |
47
|
|
|
* and the error message recieved. |
48
|
|
|
* Structure: component_name => error_message |
49
|
|
|
* |
50
|
|
|
* @var string[] Array of error messages. |
51
|
|
|
*/ |
52
|
|
|
private $errors = array(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* When instantiating a form, a list of component argument arrays must be provided. |
56
|
|
|
* Each argument array must have the following arguments, in addition to the |
57
|
|
|
* component's original arguments: |
58
|
|
|
* |
59
|
|
|
* type (string) - the component's type |
60
|
|
|
* default (mix) - the component's default value |
61
|
|
|
* filter (callable) - (optional) the component's value filter callback |
62
|
|
|
* validation (callable) - (optional) the component's value validation callback |
63
|
|
|
* |
64
|
|
|
* @param array $components An array of arrays of component arguments |
65
|
|
|
*/ |
66
|
|
|
public function __construct( array $components = array() ) |
67
|
|
|
{ |
68
|
|
|
foreach( $components as $args ) |
69
|
|
|
{ |
70
|
|
|
$name = $args['name']; |
71
|
|
|
if(array_key_exists($name, $components)) |
72
|
|
|
{ |
73
|
|
|
throw new \RuntimeException("A component with the name <b>$name</b> has already been created"); |
74
|
|
|
} |
75
|
|
|
$this->components[$name] = ComponentFactory::create($args['type'], $args); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the updated component values (validated, filtered or ignored). |
81
|
|
|
* |
82
|
|
|
* Loops through each component and acts according to its type: |
83
|
|
|
* - Disableable components are ignored if they are disabled. |
84
|
|
|
* - Validatable components are validated using their validation function. |
85
|
|
|
* If the new value is invalid, the old value will be used. |
86
|
|
|
* - Filterable components are filtered using their filter function. |
87
|
|
|
* - Non-value components are skipped altogether. |
88
|
|
|
* |
89
|
|
|
* Each component is also set with its new value. |
90
|
|
|
* |
91
|
|
|
* @param array $new_instance The new component values array. |
92
|
|
|
* @param array $old_instance The old component values array. |
93
|
|
|
* |
94
|
|
|
* @return array The updated values array. |
95
|
|
|
*/ |
96
|
|
|
public function update( array $new_instance, array $old_instance = array() ) |
97
|
|
|
{ |
98
|
|
|
$this->old_instance = array_merge($this->get_defaults(),$old_instance); |
99
|
|
|
$this->new_instance = array_merge($this->old_instance,$new_instance); |
100
|
|
|
$this->final_instance = $this->new_instance; |
101
|
|
|
|
102
|
|
|
foreach ( $this->components as $component ) |
103
|
|
|
{ |
104
|
|
|
// Update individual fields, as well as the composite parent field. |
105
|
|
|
if ( $component instanceof ValueComponentInterface ) |
106
|
|
|
{ |
107
|
|
|
$this->update_component( $component ); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->final_instance; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Reset all fields to their default values. |
116
|
|
|
* |
117
|
|
|
* @param array $names List of component names to be set to their defaults. If no names are specified, all components will be reset |
118
|
|
|
* @return array The updated values array. |
119
|
|
|
*/ |
120
|
|
|
public function reset( array $names = array() ) |
121
|
|
|
{ |
122
|
|
|
if( array() === $names ) |
123
|
|
|
{ |
124
|
|
|
// Unset new instance to force reset |
125
|
|
|
$this->new_instance = array(); |
126
|
|
|
return $this->update(); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
else |
129
|
|
|
{ |
130
|
|
|
foreach( $this->components as $c ) |
131
|
|
|
{ |
132
|
|
|
if( in_array($c->name, $names) ) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$this->new_instance[$c->name] = $c->default; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
return $this->update(); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the list of error messages for components that could not be validated. |
143
|
|
|
* Structure: components_name => error_message |
144
|
|
|
* |
145
|
|
|
* @return string[] Array of error messages. |
146
|
|
|
*/ |
147
|
|
|
public function get_errors() |
148
|
|
|
{ |
149
|
|
|
return $this->errors; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Update the component's value with the new value. |
154
|
|
|
* NOTE: this function also updates the $final_instance |
155
|
|
|
* array. |
156
|
|
|
* |
157
|
|
|
* @param ValueComponentInterface $component The component to validate. |
158
|
|
|
*/ |
159
|
|
|
private function update_component( ValueComponentInterface $component ) |
160
|
|
|
{ |
161
|
|
|
$component->value = $this->final_instance[$component->name]; |
|
|
|
|
162
|
|
|
|
163
|
|
|
// Skip if this field is disabled |
164
|
|
|
if($component instanceof DisableableComponentInterface && |
165
|
|
|
true === $component->disabled) { |
166
|
|
|
return; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// Apply user-defined filter |
170
|
|
|
if( $component instanceof FilterableComponentInterface ) |
171
|
|
|
{ |
172
|
|
|
$this->filter( $component ); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// Validate value |
176
|
|
|
if( $component instanceof ValidatableComponentInterface ) |
177
|
|
|
{ |
178
|
|
|
$this->validate( $component ); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Filter the component's value using its filter function (if applicable) |
184
|
|
|
* |
185
|
|
|
* @param UI\FilterableComponentInterface $component |
186
|
|
|
*/ |
187
|
|
|
private function filter( FilterableComponentInterface $component ) |
188
|
|
|
{ |
189
|
|
|
$filter = $component->filter; |
|
|
|
|
190
|
|
|
|
191
|
|
|
if( is_callable( $filter ) ) |
192
|
|
|
{ |
193
|
|
|
$component->value = $filter( $this->final_instance[$component->name] ); |
|
|
|
|
194
|
|
|
$this->final_instance[$component->name] = $component->value; |
|
|
|
|
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Validate the component's value using its validation function. |
200
|
|
|
* |
201
|
|
|
* If the value is invalid, the old value is used, and an error message is |
202
|
|
|
* saved into the errors array as component_name => error_message. |
203
|
|
|
* |
204
|
|
|
* @param ValidatableComponentInterface $component The component to validate. |
205
|
|
|
*/ |
206
|
|
|
private function validate( ValidatableComponentInterface $component ) |
207
|
|
|
{ |
208
|
|
|
$name = $component->name; |
|
|
|
|
209
|
|
|
$validate = $component->validation; |
|
|
|
|
210
|
|
|
$valid = true; |
|
|
|
|
211
|
|
|
|
212
|
|
|
$component->validity = $component::VALID; |
|
|
|
|
213
|
|
|
|
214
|
|
|
if(is_callable($validate)) |
215
|
|
|
{ |
216
|
|
|
$valid = $validate($this->new_instance[$name]); |
217
|
|
|
|
218
|
|
|
// Invalid input, use old instance or default value |
219
|
|
|
if ( true !== $valid ) |
220
|
|
|
{ |
221
|
|
|
$this->errors[$name] = $valid; |
222
|
|
|
$component->value = $this->old_instance[$name]; |
|
|
|
|
223
|
|
|
$component->validity = $component::INVALID; |
|
|
|
|
224
|
|
|
$this->final_instance[$name] = $this->old_instance[$name]; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
private function get_defaults() |
230
|
|
|
{ |
231
|
|
|
$defaults = array(); |
232
|
|
|
|
233
|
|
|
foreach( $this->components as $component ) |
234
|
|
|
{ |
235
|
|
|
$defaults[$component->name] = $component->default; |
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $defaults; |
239
|
|
|
} |
240
|
|
|
} |
This check looks for function calls that miss required arguments.