1 | <?php |
||
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() ) |
||
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() ) |
||
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() ) |
||
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() |
||
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 ) |
||
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 ) |
||
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 ) |
||
228 | |||
229 | private function get_defaults() |
||
240 | } |
This check looks for function calls that miss required arguments.