Index: branches/salvatoreingala/Gadgets/backend/GadgetPrefs.php |
— | — | @@ -10,128 +10,142 @@ |
11 | 11 | |
12 | 12 | class GadgetPrefs { |
13 | 13 | |
14 | | - //Syntax specifications of preference description language |
| 14 | + //Syntax specifications of preference description language. |
| 15 | + //Each element describes a type, and it has a 'description' and may have a 'checker'. |
| 16 | + // - 'description' is an array that describes the fields of that option. |
| 17 | + // - 'checker' is an optional function that does validation of the entire preference description, |
| 18 | + // when more complex semantics are needed. |
15 | 19 | private static $prefsDescriptionSpecifications = array( |
16 | | - 'boolean' => array( |
17 | | - 'default' => array( |
18 | | - 'isMandatory' => true, |
19 | | - 'checker' => 'is_bool' |
20 | | - ), |
21 | | - 'label' => array( |
22 | | - 'isMandatory' => true, |
23 | | - 'checker' => 'is_string' |
| 20 | + 'boolean' => array( |
| 21 | + 'description' => array( |
| 22 | + 'default' => array( |
| 23 | + 'isMandatory' => true, |
| 24 | + 'checker' => 'is_bool' |
| 25 | + ), |
| 26 | + 'label' => array( |
| 27 | + 'isMandatory' => true, |
| 28 | + 'checker' => 'is_string' |
| 29 | + ) |
24 | 30 | ) |
25 | 31 | ), |
26 | 32 | 'string' => array( |
27 | | - 'default' => array( |
28 | | - 'isMandatory' => true, |
29 | | - 'checker' => 'is_string' |
| 33 | + 'description' => array( |
| 34 | + 'default' => array( |
| 35 | + 'isMandatory' => true, |
| 36 | + 'checker' => 'is_string' |
| 37 | + ), |
| 38 | + 'label' => array( |
| 39 | + 'isMandatory' => true, |
| 40 | + 'checker' => 'is_string' |
| 41 | + ), |
| 42 | + 'required' => array( |
| 43 | + 'isMandatory' => false, |
| 44 | + 'checker' => 'is_bool' |
| 45 | + ), |
| 46 | + 'minlength' => array( |
| 47 | + 'isMandatory' => false, |
| 48 | + 'checker' => 'is_integer' |
| 49 | + ), |
| 50 | + 'maxlength' => array( |
| 51 | + 'isMandatory' => false, |
| 52 | + 'checker' => 'is_integer' |
| 53 | + ) |
30 | 54 | ), |
31 | | - 'label' => array( |
32 | | - 'isMandatory' => true, |
33 | | - 'checker' => 'is_string' |
34 | | - ), |
35 | | - 'required' => array( |
36 | | - 'isMandatory' => false, |
37 | | - 'checker' => 'is_bool' |
38 | | - ), |
39 | | - 'minlength' => array( |
40 | | - 'isMandatory' => false, |
41 | | - 'checker' => 'is_integer' |
42 | | - ), |
43 | | - 'maxlength' => array( |
44 | | - 'isMandatory' => false, |
45 | | - 'checker' => 'is_integer' |
46 | | - ) |
| 55 | + 'checker' => 'GadgetPrefs::checkStringOptionDefinition' |
47 | 56 | ), |
48 | 57 | 'number' => array( |
49 | | - 'default' => array( |
50 | | - 'isMandatory' => true, |
51 | | - 'checker' => 'GadgetPrefs::isFloatOrIntOrNull' |
| 58 | + 'description' => array( |
| 59 | + 'default' => array( |
| 60 | + 'isMandatory' => true, |
| 61 | + 'checker' => 'GadgetPrefs::isFloatOrIntOrNull' |
| 62 | + ), |
| 63 | + 'label' => array( |
| 64 | + 'isMandatory' => true, |
| 65 | + 'checker' => 'is_string' |
| 66 | + ), |
| 67 | + 'required' => array( |
| 68 | + 'isMandatory' => false, |
| 69 | + 'checker' => 'is_bool' |
| 70 | + ), |
| 71 | + 'integer' => array( |
| 72 | + 'isMandatory' => false, |
| 73 | + 'checker' => 'is_bool' |
| 74 | + ), |
| 75 | + 'min' => array( |
| 76 | + 'isMandatory' => false, |
| 77 | + 'checker' => 'GadgetPrefs::isFloatOrInt' |
| 78 | + ), |
| 79 | + 'max' => array( |
| 80 | + 'isMandatory' => false, |
| 81 | + 'checker' => 'GadgetPrefs::isFloatOrInt' |
| 82 | + ) |
52 | 83 | ), |
53 | | - 'label' => array( |
54 | | - 'isMandatory' => true, |
55 | | - 'checker' => 'is_string' |
56 | | - ), |
57 | | - 'required' => array( |
58 | | - 'isMandatory' => false, |
59 | | - 'checker' => 'is_bool' |
60 | | - ), |
61 | | - 'integer' => array( |
62 | | - 'isMandatory' => false, |
63 | | - 'checker' => 'is_bool' |
64 | | - ), |
65 | | - 'min' => array( |
66 | | - 'isMandatory' => false, |
67 | | - 'checker' => 'GadgetPrefs::isFloatOrInt' |
68 | | - ), |
69 | | - 'max' => array( |
70 | | - 'isMandatory' => false, |
71 | | - 'checker' => 'GadgetPrefs::isFloatOrInt' |
72 | | - ) |
| 84 | + 'checker' => 'GadgetPrefs::checkNumberOptionDefinition' |
73 | 85 | ), |
74 | 86 | 'select' => array( |
75 | | - 'default' => array( |
76 | | - 'isMandatory' => true |
| 87 | + 'description' => array( |
| 88 | + 'default' => array( |
| 89 | + 'isMandatory' => true |
| 90 | + ), |
| 91 | + 'label' => array( |
| 92 | + 'isMandatory' => true, |
| 93 | + 'checker' => 'is_string' |
| 94 | + ), |
| 95 | + 'options' => array( |
| 96 | + 'isMandatory' => true, |
| 97 | + 'checker' => 'is_array' |
| 98 | + ) |
77 | 99 | ), |
78 | | - 'label' => array( |
79 | | - 'isMandatory' => true, |
80 | | - 'checker' => 'is_string' |
81 | | - ), |
82 | | - 'options' => array( |
83 | | - 'isMandatory' => true, |
84 | | - 'checker' => 'is_array' |
85 | | - ) |
| 100 | + 'checker' => 'GadgetPrefs::checkSelectOptionDefinition' |
86 | 101 | ), |
87 | 102 | 'range' => array( |
88 | | - 'default' => array( |
89 | | - 'isMandatory' => true, |
90 | | - 'checker' => 'GadgetPrefs::isFloatOrIntOrNull' |
| 103 | + 'description' => array( |
| 104 | + 'default' => array( |
| 105 | + 'isMandatory' => true, |
| 106 | + 'checker' => 'GadgetPrefs::isFloatOrIntOrNull' |
| 107 | + ), |
| 108 | + 'label' => array( |
| 109 | + 'isMandatory' => true, |
| 110 | + 'checker' => 'is_string' |
| 111 | + ), |
| 112 | + 'min' => array( |
| 113 | + 'isMandatory' => true, |
| 114 | + 'checker' => 'GadgetPrefs::isFloatOrInt' |
| 115 | + ), |
| 116 | + 'max' => array( |
| 117 | + 'isMandatory' => true, |
| 118 | + 'checker' => 'GadgetPrefs::isFloatOrInt' |
| 119 | + ), |
| 120 | + 'step' => array( |
| 121 | + 'isMandatory' => false, |
| 122 | + 'checker' => 'GadgetPrefs::isFloatOrInt' |
| 123 | + ) |
91 | 124 | ), |
92 | | - 'label' => array( |
93 | | - 'isMandatory' => true, |
94 | | - 'checker' => 'is_string' |
95 | | - ), |
96 | | - 'min' => array( |
97 | | - 'isMandatory' => true, |
98 | | - 'checker' => 'GadgetPrefs::isFloatOrInt' |
99 | | - ), |
100 | | - 'max' => array( |
101 | | - 'isMandatory' => true, |
102 | | - 'checker' => 'GadgetPrefs::isFloatOrInt' |
103 | | - ), |
104 | | - 'step' => array( |
105 | | - 'isMandatory' => false, |
106 | | - 'checker' => 'GadgetPrefs::isFloatOrInt' |
107 | | - ) |
| 125 | + 'checker' => 'GadgetPrefs::checkRangeOptionDefinition' |
108 | 126 | ), |
109 | 127 | 'date' => array( |
110 | | - 'default' => array( |
111 | | - 'isMandatory' => true |
112 | | - ), |
113 | | - 'label' => array( |
114 | | - 'isMandatory' => true, |
115 | | - 'checker' => 'is_string' |
| 128 | + 'description' => array( |
| 129 | + 'default' => array( |
| 130 | + 'isMandatory' => true |
| 131 | + ), |
| 132 | + 'label' => array( |
| 133 | + 'isMandatory' => true, |
| 134 | + 'checker' => 'is_string' |
| 135 | + ) |
116 | 136 | ) |
117 | 137 | ), |
118 | 138 | 'color' => array( |
119 | | - 'default' => array( |
120 | | - 'isMandatory' => true |
121 | | - ), |
122 | | - 'label' => array( |
123 | | - 'isMandatory' => true, |
124 | | - 'checker' => 'is_string' |
| 139 | + 'description' => array( |
| 140 | + 'default' => array( |
| 141 | + 'isMandatory' => true |
| 142 | + ), |
| 143 | + 'label' => array( |
| 144 | + 'isMandatory' => true, |
| 145 | + 'checker' => 'is_string' |
| 146 | + ) |
125 | 147 | ) |
126 | 148 | ) |
127 | 149 | ); |
128 | | - |
129 | | - //Type-specific checkers for finer validation |
130 | | - private static $typeCheckers = array( |
131 | | - 'string' => 'GadgetPrefs::checkStringOptionDefinition', |
132 | | - 'number' => 'GadgetPrefs::checkNumberOptionDefinition', |
133 | | - 'select' => 'GadgetPrefs::checkSelectOptionDefinition', |
134 | | - 'range' => 'GadgetPrefs::checkRangeOptionDefinition' |
135 | | - ); |
136 | 150 | |
137 | 151 | //Further checks for 'string' options |
138 | 152 | private static function checkStringOptionDefinition( $option ) { |
— | — | @@ -234,7 +248,7 @@ |
235 | 249 | $mandatoryCount = array(); |
236 | 250 | foreach ( self::$prefsDescriptionSpecifications as $type => $typeSpec ) { |
237 | 251 | $mandatoryCount[$type] = 0; |
238 | | - foreach ( $typeSpec as $fieldName => $fieldSpec ) { |
| 252 | + foreach ( $typeSpec['description'] as $fieldName => $fieldSpec ) { |
239 | 253 | if ( $fieldSpec['isMandatory'] === true ) { |
240 | 254 | ++$mandatoryCount[$type]; |
241 | 255 | } |
— | — | @@ -265,6 +279,7 @@ |
266 | 280 | |
267 | 281 | //Check if all fields satisfy specification |
268 | 282 | $typeSpec = self::$prefsDescriptionSpecifications[$type]; |
| 283 | + $typeDescription = $typeSpec['description']; |
269 | 284 | $count = 0; //count of present mandatory members |
270 | 285 | foreach ( $optionDefinition as $fieldName => $fieldValue ) { |
271 | 286 | |
— | — | @@ -272,16 +287,16 @@ |
273 | 288 | continue; //'type' must not be checked |
274 | 289 | } |
275 | 290 | |
276 | | - if ( !isset( $typeSpec[$fieldName] ) ) { |
| 291 | + if ( !isset( $typeDescription[$fieldName] ) ) { |
277 | 292 | return false; |
278 | 293 | } |
279 | 294 | |
280 | | - if ( $typeSpec[$fieldName]['isMandatory'] ) { |
| 295 | + if ( $typeDescription[$fieldName]['isMandatory'] ) { |
281 | 296 | ++$count; |
282 | 297 | } |
283 | 298 | |
284 | | - if ( isset( $typeSpec[$fieldName]['checker'] ) ) { |
285 | | - $checker = $typeSpec[$fieldName]['checker']; |
| 299 | + if ( isset( $typeDescription[$fieldName]['checker'] ) ) { |
| 300 | + $checker = $typeDescription[$fieldName]['checker']; |
286 | 301 | if ( !call_user_func( $checker, $fieldValue ) ) { |
287 | 302 | return false; |
288 | 303 | } |
— | — | @@ -292,9 +307,9 @@ |
293 | 308 | return false; //not all mandatory members are given |
294 | 309 | } |
295 | 310 | |
296 | | - if ( isset( self::$typeCheckers[$type] ) ) { |
| 311 | + if ( isset( $typeSpec['checker'] ) ) { |
297 | 312 | //Call type-specific checker for finer validation |
298 | | - if ( !call_user_func( self::$typeCheckers[$type], $optionDefinition ) ) { |
| 313 | + if ( !call_user_func( $typeSpec['checker'], $optionDefinition ) ) { |
299 | 314 | return false; |
300 | 315 | } |
301 | 316 | } |