Index: trunk/extensions/Validator/Validator.class.php |
— | — | @@ -24,6 +24,8 @@ |
25 | 25 | * this will allow for the special behaviour of the default parameter of display_points in Maps |
26 | 26 | * where the actual alias influences the handling |
27 | 27 | * TODO: break on fatal errors, such as missing required parameters that are dependencies |
| 28 | + * |
| 29 | + * FIXME: lists are broken |
28 | 30 | */ |
29 | 31 | final class Validator { |
30 | 32 | |
— | — | @@ -98,15 +100,42 @@ |
99 | 101 | 'filtered_array' => array( 'ValidatorFormats', 'format_filtered_array' ), |
100 | 102 | ); |
101 | 103 | |
| 104 | + /** |
| 105 | + * An array containing the parameter definitions. The keys are main parameter names, |
| 106 | + * and the values are associative arrays themselves, consisting out of elements that |
| 107 | + * can be seen as properties of the parameter as they would be in the case of objects. |
| 108 | + * |
| 109 | + * @var associative array |
| 110 | + */ |
102 | 111 | private $mParameterInfo; |
103 | 112 | |
104 | | - private $mRawParameters = array(); |
105 | | - |
| 113 | + /** |
| 114 | + * An array initially containing the user provided values. Adittional data about |
| 115 | + * the validation and formatting processes gets added later on, and so stays |
| 116 | + * available for validation and formatting of other parameters. |
| 117 | + * |
| 118 | + * original-value |
| 119 | + * default |
| 120 | + * position |
| 121 | + * original-name |
| 122 | + * |
| 123 | + * |
| 124 | + * @var associative array |
| 125 | + */ |
106 | 126 | private $mParameters = array(); |
| 127 | + |
| 128 | + /** |
| 129 | + * Arrays for holding the (main) names of valid, invalid and unknown parameters. |
| 130 | + */ |
107 | 131 | private $mValidParams = array(); |
108 | 132 | private $mInvalidParams = array(); |
109 | 133 | private $mUnknownParams = array(); |
110 | 134 | |
| 135 | + /** |
| 136 | + * Holds all errors and their meta data. |
| 137 | + * |
| 138 | + * @var associative array |
| 139 | + */ |
111 | 140 | private $mErrors = array(); |
112 | 141 | |
113 | 142 | /** |
— | — | @@ -125,8 +154,6 @@ |
126 | 155 | * |
127 | 156 | * @param array $rawParams |
128 | 157 | * @param array $defaultParams |
129 | | - * |
130 | | - * TODO: retain info about the changing of defaults |
131 | 158 | */ |
132 | 159 | public function parseAndSetParams( array $rawParams, array $defaultParams = array() ) { |
133 | 160 | $parameters = array(); |
— | — | @@ -143,7 +170,7 @@ |
144 | 171 | if ( count( $defaultParams ) > 0 ) { |
145 | 172 | $defaultParam = array_shift( $defaultParams ); |
146 | 173 | $parameters[$defaultParam] = array( |
147 | | - 'value' => trim( $parts[0] ), |
| 174 | + 'original-value' => trim( $parts[0] ), |
148 | 175 | 'default' => $defaultNr, |
149 | 176 | 'position' => $nr |
150 | 177 | ); |
— | — | @@ -152,7 +179,7 @@ |
153 | 180 | } else { |
154 | 181 | $name = strtolower( trim( array_shift( $parts ) ) ); |
155 | 182 | $parameters[$name] = array( |
156 | | - 'value' => trim( implode( '=', $parts ) ), |
| 183 | + 'original-value' => trim( implode( '=', $parts ) ), |
157 | 184 | 'default' => false, |
158 | 185 | 'position' => $nr |
159 | 186 | ); |
— | — | @@ -161,47 +188,67 @@ |
162 | 189 | $nr++; |
163 | 190 | } |
164 | 191 | |
165 | | - $this->mRawParameters = $parameters; |
166 | | - } |
167 | | - |
168 | | - /** |
169 | | - * @new |
170 | | - * |
171 | | - * TODO: merge meta data and value arrays |
172 | | - */ |
173 | | - public function validateAndFormatParameters() { |
174 | 192 | // Loop through all the user provided parameters, and destinguise between those that are allowed and those that are not. |
175 | | - foreach ( $this->mRawParameters as $paramName => $paramData ) { |
176 | | - $paramValue = $paramData['value']; |
| 193 | + foreach ( $parameters as $paramName => $paramData ) { |
177 | 194 | // Attempt to get the main parameter name (takes care of aliases). |
178 | 195 | $mainName = self::getMainParamName( $paramName ); |
| 196 | + |
179 | 197 | // If the parameter is found in the list of allowed ones, add it to the $mParameters array. |
180 | 198 | if ( $mainName ) { |
181 | 199 | // Check for parameter overriding. In most cases, this has already largely been taken care off, |
182 | 200 | // in the form of later parameters overriding earlier ones. This is not true for different aliases though. |
183 | 201 | if ( !array_key_exists( $mainName, $this->mParameters ) || self::$acceptOverriding ) { |
184 | | - $this->mParameters[$mainName] = array( |
185 | | - 'value' => $paramValue, |
186 | | - 'original-name' => $paramName, |
187 | | - 'default' => $paramData['default'], |
188 | | - 'position' => $paramData['position'] |
189 | | - ); |
| 202 | + $paramData['original-name'] = $paramName; |
| 203 | + $this->mParameters[$mainName] = $paramData; |
190 | 204 | } |
191 | 205 | else { |
192 | 206 | $this->errors[] = array( 'type' => 'override', 'name' => $mainName ); |
193 | 207 | } |
194 | 208 | } |
195 | 209 | else { // If the parameter is not found in the list of allowed ones, add an item to the $this->mErrors array. |
196 | | - if ( self::$storeUnknownParameters ) $this->mUnknownParams[$paramName] = $paramValue; |
| 210 | + if ( self::$storeUnknownParameters ) $this->mUnknownParams[$paramName] = $paramData['original-value']; |
197 | 211 | $this->mErrors[] = array( 'type' => 'unknown', 'name' => $paramName ); |
198 | 212 | } |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Returns the main parameter name for a given parameter or alias, or false |
| 218 | + * when it is not recognized as main parameter or alias. |
| 219 | + * |
| 220 | + * @param string $paramName |
| 221 | + * |
| 222 | + * @return string or false |
| 223 | + */ |
| 224 | + private function getMainParamName( $paramName ) { |
| 225 | + $result = false; |
| 226 | + |
| 227 | + if ( array_key_exists( $paramName, $this->mParameterInfo ) ) { |
| 228 | + $result = $paramName; |
199 | 229 | } |
200 | | - |
| 230 | + else { |
| 231 | + foreach ( $this->mParameterInfo as $name => $data ) { |
| 232 | + if ( array_key_exists( 'aliases', $data ) && in_array( $paramName, $data['aliases'] ) ) { |
| 233 | + $result = $name; |
| 234 | + break; |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + return $result; |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * @new |
| 244 | + * |
| 245 | + * TODO: further impelement new meta data structure from this point on |
| 246 | + */ |
| 247 | + public function validateAndFormatParameters() { |
201 | 248 | $dependencyList = array(); |
202 | 249 | |
203 | 250 | foreach ( $this->mParameterInfo as $paramName => $paramInfo ) { |
204 | | - $dependencyList[$paramName] = array_key_exists( 'dependencies', $paramInfo ) ? |
205 | | - (array)$paramInfo['dependencies'] : array(); |
| 251 | + $dependencyList[$paramName] = |
| 252 | + array_key_exists( 'dependencies', $paramInfo ) ? (array)$paramInfo['dependencies'] : array(); |
206 | 253 | } |
207 | 254 | |
208 | 255 | $sorter = new TopologicalSort( $dependencyList, true ); |
— | — | @@ -213,7 +260,7 @@ |
214 | 261 | // If the user provided a value for this parameter, validate and handle it. |
215 | 262 | if ( array_key_exists( $paramName, $this->mParameters ) ) { |
216 | 263 | |
217 | | - $paramValue = $this->mParameters[$paramName]['value']; |
| 264 | + $paramValue = $this->mParameters[$paramName]['original-value']; |
218 | 265 | $this->cleanParameter( $paramName, $paramValue ); |
219 | 266 | |
220 | 267 | if ( $this->validateParameter( $paramName ) ) { |
— | — | @@ -241,32 +288,6 @@ |
242 | 289 | } |
243 | 290 | |
244 | 291 | /** |
245 | | - * Returns the main parameter name for a given parameter or alias, or false |
246 | | - * when it is not recognized as main parameter or alias. |
247 | | - * |
248 | | - * @param string $paramName |
249 | | - * |
250 | | - * @return string |
251 | | - */ |
252 | | - private function getMainParamName( $paramName ) { |
253 | | - $result = false; |
254 | | - |
255 | | - if ( array_key_exists( $paramName, $this->mParameterInfo ) ) { |
256 | | - $result = $paramName; |
257 | | - } |
258 | | - else { |
259 | | - foreach ( $this->mParameterInfo as $name => $data ) { |
260 | | - if ( array_key_exists( 'aliases', $data ) && in_array( $paramName, $data['aliases'] ) ) { |
261 | | - $result = $name; |
262 | | - break; |
263 | | - } |
264 | | - } |
265 | | - } |
266 | | - |
267 | | - return $result; |
268 | | - } |
269 | | - |
270 | | - /** |
271 | 292 | * Ensures the parameter info is valid, and splits lists. |
272 | 293 | * |
273 | 294 | * @param string $name |
— | — | @@ -341,7 +362,7 @@ |
342 | 363 | $hasNoErrors = true; |
343 | 364 | $checkItemCriteria = true; |
344 | 365 | |
345 | | - $value = $this->mParameters[$name]['value']; |
| 366 | + $value = $this->mParameters[$name]['original-value']; |
346 | 367 | |
347 | 368 | if ( array_key_exists( 'list-criteria', $this->mParameterInfo[$name] ) ) { |
348 | 369 | foreach ( $this->mParameterInfo[$name]['list-criteria'] as $criteriaName => $criteriaArgs ) { |
— | — | @@ -435,7 +456,7 @@ |
436 | 457 | // Add a new error when the validation failed, and break the loop if errors for one parameter should not be accumulated. |
437 | 458 | if ( !$isValid ) { |
438 | 459 | $isList = is_array( $value ); |
439 | | - if ( $isList ) $value = $this->mRawParameters[$name]; |
| 460 | + if ( $isList ) $value = $this->mParameters[$name]['original-value']; |
440 | 461 | $this->mErrors[] = array( 'type' => $criteriaName, 'args' => $criteriaArgs, 'name' => $name, 'list' => $isList, 'value' => $value ); |
441 | 462 | $hasNoErrors = false; |
442 | 463 | if ( !self::$accumulateParameterErrors ) break; |