Index: trunk/extensions/Validator/Validator.class.php |
— | — | @@ -23,8 +23,11 @@ |
24 | 24 | * TODO: provide all original and inferred info about a parameter pair to the validation and formatting functions. |
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 | 28 | * TODO: break on fatal errors, such as missing required parameters that are dependencies |
28 | 29 | * |
| 30 | + * TODO: correct invalid parameters in the main loop, as to have correct dependency handling |
| 31 | + * |
29 | 32 | * FIXME: lists are broken |
30 | 33 | */ |
31 | 34 | final class Validator { |
— | — | @@ -118,8 +121,8 @@ |
119 | 122 | * default |
120 | 123 | * position |
121 | 124 | * original-name |
| 125 | + * formatted-value |
122 | 126 | * |
123 | | - * |
124 | 127 | * @var associative array |
125 | 128 | */ |
126 | 129 | private $mParameters = array(); |
— | — | @@ -139,23 +142,16 @@ |
140 | 143 | private $mErrors = array(); |
141 | 144 | |
142 | 145 | /** |
143 | | - * Sets the parameter criteria, used to valiate the parameters. |
144 | | - * |
| 146 | + * Determines the names and values of all parameters. Also takes care of |
| 147 | + * default parameters and aliases, by determining the main parameter name. |
| 148 | + * |
| 149 | + * @param array $rawParams |
145 | 150 | * @param array $parameterInfo |
146 | 151 | * @param array $defaultParams |
147 | 152 | */ |
148 | | - public function setParameterInfo( array $parameterInfo ) { |
| 153 | + public function parseAndSetParams( array $rawParams, array $parameterInfo, array $defaultParams = array() ) { |
149 | 154 | $this->mParameterInfo = $parameterInfo; |
150 | | - } |
151 | | - |
152 | | - /** |
153 | | - * Determine all parameter names and value, and take care of default (nameless) |
154 | | - * parameters, by turning them into named ones. |
155 | | - * |
156 | | - * @param array $rawParams |
157 | | - * @param array $defaultParams |
158 | | - */ |
159 | | - public function parseAndSetParams( array $rawParams, array $defaultParams = array() ) { |
| 155 | + |
160 | 156 | $parameters = array(); |
161 | 157 | |
162 | 158 | $nr = 0; |
— | — | @@ -206,10 +202,10 @@ |
207 | 203 | } |
208 | 204 | } |
209 | 205 | else { // If the parameter is not found in the list of allowed ones, add an item to the $this->mErrors array. |
210 | | - if ( self::$storeUnknownParameters ) $this->mUnknownParams[$paramName] = $paramData['original-value']; |
| 206 | + if ( self::$storeUnknownParameters ) $this->mUnknownParams[] = $paramName; |
211 | 207 | $this->mErrors[] = array( 'type' => 'unknown', 'name' => $paramName ); |
212 | 208 | } |
213 | | - } |
| 209 | + } |
214 | 210 | } |
215 | 211 | |
216 | 212 | /** |
— | — | @@ -239,9 +235,11 @@ |
240 | 236 | } |
241 | 237 | |
242 | 238 | /** |
243 | | - * @new |
| 239 | + * First determines the order of parameter handling based on the dependency definitons, |
| 240 | + * and then goes through the parameters one by one, first validating and then formatting, |
| 241 | + * storing any encountered errors along the way. |
244 | 242 | * |
245 | | - * TODO: further impelement new meta data structure from this point on |
| 243 | + * The 'value' element is set here, either by the cleaned 'original-value' or default. |
246 | 244 | */ |
247 | 245 | public function validateAndFormatParameters() { |
248 | 246 | $dependencyList = array(); |
— | — | @@ -260,40 +258,40 @@ |
261 | 259 | // If the user provided a value for this parameter, validate and handle it. |
262 | 260 | if ( array_key_exists( $paramName, $this->mParameters ) ) { |
263 | 261 | |
264 | | - $paramValue = $this->mParameters[$paramName]['original-value']; |
265 | | - $this->cleanParameter( $paramName, $paramValue ); |
| 262 | + $this->cleanParameter( $paramName ); |
266 | 263 | |
267 | 264 | if ( $this->validateParameter( $paramName ) ) { |
268 | 265 | // If the validation succeeded, add the parameter to the list of valid ones. |
269 | | - $this->mValidParams[$paramName] = $paramValue; |
270 | | - $this->setOutputTypes( $this->mValidParams[$paramName], $paramInfo ); |
| 266 | + $this->mValidParams[] = $paramName; |
| 267 | + $this->setOutputTypes( $paramName ); |
271 | 268 | } |
272 | 269 | else { |
273 | 270 | // If the validation failed, add the parameter to the list of invalid ones. |
274 | | - $this->mInvalidParams[$paramName] = $paramValue; |
| 271 | + $this->mInvalidParams[] = $paramName; |
275 | 272 | } |
276 | 273 | } |
277 | 274 | else { |
278 | 275 | // If the parameter is required, add a new error of type 'missing'. |
| 276 | + // TODO: break when has dependencies |
279 | 277 | if ( array_key_exists( 'required', $paramInfo ) && $paramInfo['required'] ) { |
280 | 278 | $this->errors[] = array( 'type' => 'missing', 'name' => $paramName ); |
281 | 279 | } |
282 | 280 | else { |
283 | 281 | // Set the default value (or default 'default value' if none is provided), and ensure the type is correct. |
284 | | - $this->mValidParams[$paramName] = array_key_exists( 'default', $paramInfo ) ? $paramInfo['default'] : self::$defaultDefaultValue; |
285 | | - $this->setOutputTypes( $this->mValidParams[$paramName], $paramInfo ); |
| 282 | + $this->mParameters[$paramName]['value'] = array_key_exists( 'default', $paramInfo ) ? $paramInfo['default'] : self::$defaultDefaultValue; |
| 283 | + $this->mValidParams[] = $paramName; |
| 284 | + $this->setOutputTypes( $paramName ); |
286 | 285 | } |
287 | 286 | } |
288 | 287 | } |
289 | 288 | } |
290 | 289 | |
291 | 290 | /** |
292 | | - * Ensures the parameter info is valid, and splits lists. |
| 291 | + * Ensures the parameter info is valid and parses list types. |
293 | 292 | * |
294 | 293 | * @param string $name |
295 | | - * @param $value |
296 | 294 | */ |
297 | | - private function cleanParameter( $name, &$value ) { |
| 295 | + private function cleanParameter( $name ) { |
298 | 296 | // Ensure there is a criteria array. |
299 | 297 | if ( ! array_key_exists( 'criteria', $this->mParameterInfo[$name] ) ) { |
300 | 298 | $this->mParameterInfo[$name]['criteria'] = array(); |
— | — | @@ -330,20 +328,30 @@ |
331 | 329 | } |
332 | 330 | } |
333 | 331 | |
334 | | - if ( count( $this->mParameterInfo[$name]['type'] ) > 1 && $this->mParameterInfo[$name]['type'][1] == 'list' ) { |
335 | | - // Trimming and splitting of list values. |
336 | | - $delimiter = count( $this->mParameterInfo[$name]['type'] ) > 2 ? $this->mParameterInfo[$name]['type'][2] : self::$defaultListDelimeter; |
337 | | - $value = preg_replace( '/((\s)*' . $delimiter . '(\s)*)/', $delimiter, $value ); |
338 | | - $value = explode( $delimiter, $value ); |
| 332 | + // If the original-value element is set, clean it, and store as value. |
| 333 | + if ( array_key_exists( 'original-value', $this->mParameters[$name] ) ) { |
| 334 | + $value = $this->mParameters[$name]['original-value']; |
| 335 | + |
| 336 | + if ( count( $this->mParameterInfo[$name]['type'] ) > 1 && $this->mParameterInfo[$name]['type'][1] == 'list' ) { |
| 337 | + // Trimming and splitting of list values. |
| 338 | + $delimiter = count( $this->mParameterInfo[$name]['type'] ) > 2 ? $this->mParameterInfo[$name]['type'][2] : self::$defaultListDelimeter; |
| 339 | + $value = preg_replace( '/((\s)*' . $delimiter . '(\s)*)/', $delimiter, $value ); |
| 340 | + $value = explode( $delimiter, $value ); |
| 341 | + } |
| 342 | + elseif ( count( $this->mParameterInfo[$name]['type'] ) > 1 && $this->mParameterInfo[$name]['type'][1] == 'array' && is_array( $value ) ) { |
| 343 | + // Trimming of array values. |
| 344 | + for ( $i = count( $value ); $i > 0; $i-- ) $value[$i] = trim( $value[$i] ); |
| 345 | + } |
| 346 | + |
| 347 | + $this->mParameters[$name]['value'] = $value; |
339 | 348 | } |
340 | | - elseif ( count( $this->mParameterInfo[$name]['type'] ) > 1 && $this->mParameterInfo[$name]['type'][1] == 'array' && is_array( $value ) ) { |
341 | | - // Trimming of array values. |
342 | | - for ( $i = count( $value ); $i > 0; $i-- ) $value[$i] = trim ( $value[$i] ); |
343 | | - } |
344 | 349 | } |
345 | 350 | |
346 | 351 | private function addTypeCriteria( $paramName, $criteriaName, $criteriaArgs = array() ) { |
347 | | - $this->mParameterInfo[$paramName]['criteria'] = array_merge( array( $criteriaName => $criteriaArgs ), $this->mParameterInfo[$paramName]['criteria'] ); |
| 352 | + $this->mParameterInfo[$paramName]['criteria'] = array_merge( |
| 353 | + array( $criteriaName => $criteriaArgs ), |
| 354 | + $this->mParameterInfo[$paramName]['criteria'] |
| 355 | + ); |
348 | 356 | } |
349 | 357 | |
350 | 358 | /** |
— | — | @@ -355,21 +363,31 @@ |
356 | 364 | * @param string $name |
357 | 365 | * |
358 | 366 | * @return boolean Indicates whether there the parameter value(s) is/are valid. |
359 | | - * |
360 | | - * TODO: value was byref arg for some reason - this could break stuff |
361 | 367 | */ |
362 | 368 | private function validateParameter( $name ) { |
363 | | - $hasNoErrors = true; |
364 | | - $checkItemCriteria = true; |
| 369 | + $hasNoErrors = $this->doListValidation( $name ); |
365 | 370 | |
366 | | - $value = $this->mParameters[$name]['original-value']; |
| 371 | + if ( $hasNoErrors || self::$accumulateParameterErrors ) { |
| 372 | + $hasNoErrors = $hasNoErrors && $this->doItemValidation( $name ); |
| 373 | + } |
367 | 374 | |
| 375 | + return $hasNoErrors; |
| 376 | + } |
| 377 | + |
| 378 | + /** |
| 379 | + * Validates the list criteria for a parameter, if there are any. |
| 380 | + * |
| 381 | + * @param string $name |
| 382 | + */ |
| 383 | + private function doListValidation( $name ) { |
| 384 | + $hasNoErrors = true; |
| 385 | + |
368 | 386 | if ( array_key_exists( 'list-criteria', $this->mParameterInfo[$name] ) ) { |
369 | 387 | foreach ( $this->mParameterInfo[$name]['list-criteria'] as $criteriaName => $criteriaArgs ) { |
370 | 388 | // Get the validation function. If there is no matching function, throw an exception. |
371 | 389 | if ( array_key_exists( $criteriaName, self::$mListValidationFunctions ) ) { |
372 | 390 | $validationFunction = self::$mListValidationFunctions[$criteriaName]; |
373 | | - $isValid = $this->doCriteriaValidation( $validationFunction, $value, $this->mParameters[$name], $criteriaArgs ); |
| 391 | + $isValid = $this->doCriteriaValidation( $validationFunction, $this->mParameters['value'], $this->mParameters[$name], $criteriaArgs ); |
374 | 392 | |
375 | 393 | // Add a new error when the validation failed, and break the loop if errors for one parameter should not be accumulated. |
376 | 394 | if ( ! $isValid ) { |
— | — | @@ -377,8 +395,7 @@ |
378 | 396 | |
379 | 397 | $this->errors[] = array( 'type' => $criteriaName, 'args' => $criteriaArgs, 'name' => $name, 'list' => true, 'value' => $this->rawParameters[$name] ); |
380 | 398 | |
381 | | - if ( ! self::$accumulateParameterErrors ) { |
382 | | - $checkItemCriteria = false; |
| 399 | + if ( !self::$accumulateParameterErrors ) { |
383 | 400 | break; |
384 | 401 | } |
385 | 402 | } |
— | — | @@ -389,23 +406,22 @@ |
390 | 407 | } |
391 | 408 | } |
392 | 409 | } |
393 | | - |
394 | | - if ( $checkItemCriteria ) $hasNoErrors = $hasNoErrors && $this->doItemValidation( $name, $value ); |
395 | | - |
| 410 | + |
396 | 411 | return $hasNoErrors; |
397 | 412 | } |
398 | 413 | |
399 | 414 | /** |
400 | 415 | * Valides the provided parameter by matching the value against the item criteria for the name. |
401 | 416 | * |
402 | | - * @param $name |
403 | | - * @param $value |
| 417 | + * @param string $name |
404 | 418 | * |
405 | 419 | * @return boolean Indicates whether there the parameter value(s) is/are valid. |
406 | 420 | */ |
407 | | - private function doItemValidation( $name, &$value ) { |
| 421 | + private function doItemValidation( $name ) { |
408 | 422 | $hasNoErrors = true; |
409 | 423 | |
| 424 | + $value = &$this->mParameters[$name]['value']; |
| 425 | + |
410 | 426 | // Go through all item criteria. |
411 | 427 | foreach ( $this->mParameterInfo[$name]['criteria'] as $criteriaName => $criteriaArgs ) { |
412 | 428 | // Get the validation function. If there is no matching function, throw an exception. |
— | — | @@ -452,7 +468,7 @@ |
453 | 469 | // Determine if the value is valid for single valued parameters. |
454 | 470 | $isValid = $this->doCriteriaValidation( $validationFunction, $value, $this->mParameters[$name], $criteriaArgs ); |
455 | 471 | } |
456 | | - |
| 472 | + |
457 | 473 | // Add a new error when the validation failed, and break the loop if errors for one parameter should not be accumulated. |
458 | 474 | if ( !$isValid ) { |
459 | 475 | $isList = is_array( $value ); |
— | — | @@ -467,7 +483,7 @@ |
468 | 484 | throw new Exception( 'There is no validation function for criteria type ' . $criteriaName ); |
469 | 485 | } |
470 | 486 | } |
471 | | - |
| 487 | + |
472 | 488 | return $hasNoErrors; |
473 | 489 | } |
474 | 490 | |
— | — | @@ -483,7 +499,6 @@ |
484 | 500 | */ |
485 | 501 | private function doCriteriaValidation( $validationFunction, $value, array $metaData, array $criteriaArgs ) { |
486 | 502 | // Call the validation function and store the result. |
487 | | - //var_dump($metaData);exit; |
488 | 503 | return call_user_func_array( $validationFunction, array_merge( array_merge( array( $value ), array( $metaData ) ), $criteriaArgs ) ); |
489 | 504 | } |
490 | 505 | |
— | — | @@ -491,29 +506,35 @@ |
492 | 507 | * Changes the invalid parameters to their default values, and changes their state to valid. |
493 | 508 | */ |
494 | 509 | public function correctInvalidParams() { |
495 | | - foreach ( $this->mInvalidParams as $paramName => $paramValue ) { |
496 | | - unset( $this->mInvalidParams[$paramName] ); |
497 | | - $this->mValidParams[$paramName] = array_key_exists( 'default', $this->mParameterInfo[$paramName] ) ? $this->mParameterInfo[$paramName]['default'] : ''; |
498 | | - $this->setOutputTypes( $this->mValidParams[$paramName], $this->mParameterInfo[$paramName] ); |
| 510 | + while ( $paramName = array_shift( $this->mInvalidParams ) ) { |
| 511 | + if ( array_key_exists( 'default', $this->mParameterInfo[$paramName] ) ) { |
| 512 | + $this->mParameters[$paramName]['value'] = $this->mParameterInfo[$paramName]['default']; |
| 513 | + } |
| 514 | + else { |
| 515 | + $this->mParameters[$paramName]['value'] = self::$defaultDefaultValue; |
| 516 | + } |
| 517 | + $this->setOutputTypes( $paramName ); |
| 518 | + $this->mValidParams[] = $paramName; |
499 | 519 | } |
500 | 520 | } |
501 | 521 | |
502 | 522 | /** |
503 | 523 | * Ensures the output type values are arrays, and then calls setOutputType. |
504 | 524 | * |
505 | | - * @param $value |
506 | | - * @param array $info |
| 525 | + * @param string $name |
507 | 526 | */ |
508 | | - private function setOutputTypes( &$value, array $info ) { |
| 527 | + private function setOutputTypes( $name ) { |
| 528 | + $info = $this->mParameterInfo[$name]; |
| 529 | + |
509 | 530 | if ( array_key_exists( 'output-types', $info ) ) { |
510 | 531 | for ( $i = 0, $c = count( $info['output-types'] ); $i < $c; $i++ ) { |
511 | 532 | if ( ! is_array( $info['output-types'][$i] ) ) $info['output-types'][$i] = array( $info['output-types'][$i] ); |
512 | | - $this->setOutputType( $value, $info['output-types'][$i] ); |
| 533 | + $this->setOutputType( $name, $info['output-types'][$i] ); |
513 | 534 | } |
514 | 535 | } |
515 | 536 | elseif ( array_key_exists( 'output-type', $info ) ) { |
516 | 537 | if ( ! is_array( $info['output-type'] ) ) $info['output-type'] = array( $info['output-type'] ); |
517 | | - $this->setOutputType( $value, $info['output-type'] ); |
| 538 | + $this->setOutputType( $name, $info['output-type'] ); |
518 | 539 | } |
519 | 540 | |
520 | 541 | } |
— | — | @@ -521,17 +542,29 @@ |
522 | 543 | /** |
523 | 544 | * Calls the formatting function for the provided output format with the provided value. |
524 | 545 | * |
525 | | - * @param $value |
| 546 | + * @param string $name |
526 | 547 | * @param array $typeInfo |
527 | 548 | */ |
528 | | - private function setOutputType( &$value, array $typeInfo ) { |
| 549 | + private function setOutputType( $name, array $typeInfo ) { |
529 | 550 | // The output type is the first value in the type info array. |
530 | 551 | // The remaining ones will be any extra arguments. |
531 | 552 | $outputType = strtolower( array_shift( $typeInfo ) ); |
532 | 553 | |
| 554 | + if ( !array_key_exists( 'formatted-value', $this->mParameters[$name] ) ) { |
| 555 | + $this->mParameters[$name]['formatted-value'] = $this->mParameters[$name]['value']; |
| 556 | + } |
| 557 | + |
533 | 558 | if ( array_key_exists( $outputType, self::$mOutputFormats ) ) { |
534 | | - // Call the formatting function with as first parameter the value, followed by the extra arguments. |
535 | | - call_user_func_array( self::$mOutputFormats[$outputType], array_merge( array( &$value ), $typeInfo ) ); |
| 559 | + /** |
| 560 | + * Call the formatting function with these parameters: |
| 561 | + * - parameter value: ByRef for easy manipulation. |
| 562 | + * - parameter name: For lookups in the param info array. |
| 563 | + * - parameter array: All data about the parameters gathered so far (this includes dependencies!). |
| 564 | + * - output type info: Type info as provided by the parameter definition. This can be zero or more parameters. |
| 565 | + */ |
| 566 | + $parameters = array( &$this->mParameters[$name]['formatted-value'], $name, $this->mParameters ); |
| 567 | + $parameters = array_merge( $parameters, $typeInfo ); |
| 568 | + call_user_func_array( self::$mOutputFormats[$outputType], $parameters ); |
536 | 569 | } |
537 | 570 | else { |
538 | 571 | throw new Exception( 'There is no formatting function for output format ' . $outputType ); |
— | — | @@ -544,7 +577,14 @@ |
545 | 578 | * @return array |
546 | 579 | */ |
547 | 580 | public function getValidParams() { |
548 | | - return $this->mValidParams; |
| 581 | + $validParams = array(); |
| 582 | + |
| 583 | + foreach( $this->mValidParams as $name ) { |
| 584 | + $key = array_key_exists( 'formatted-value', $this->mParameters[$name] ) ? 'formatted-value' : 'value'; |
| 585 | + $validParams[$name] = $this->mParameters[$name][$key]; |
| 586 | + } |
| 587 | + |
| 588 | + return $validParams; |
549 | 589 | } |
550 | 590 | |
551 | 591 | /** |
— | — | @@ -553,7 +593,13 @@ |
554 | 594 | * @return array |
555 | 595 | */ |
556 | 596 | public static function getUnknownParams() { |
557 | | - return $this->mUnknownParams; |
| 597 | + $unknownParams = array(); |
| 598 | + |
| 599 | + foreach( $this->mUnknownParams as $name ) { |
| 600 | + $unknownParams[$name] = $this->mParameters[$name]; // TODO |
| 601 | + } |
| 602 | + |
| 603 | + return $unknownParams; |
558 | 604 | } |
559 | 605 | |
560 | 606 | /** |
Index: trunk/extensions/Validator/Validator_Manager.php |
— | — | @@ -42,17 +42,16 @@ |
43 | 43 | |
44 | 44 | $validator = new Validator(); |
45 | 45 | |
46 | | - $validator->setParameterInfo( $parameterInfo ); |
47 | | - $validator->parseAndSetParams( $rawParameters, $defaultParams ); |
48 | | - |
| 46 | + $validator->parseAndSetParams( $rawParameters, $parameterInfo, $defaultParams ); |
49 | 47 | $hasNoErrors = $validator->validateAndFormatParameters(); |
| 48 | + |
50 | 49 | $hasFatalError = $hasNoErrors ? false : $this->hasFatalError(); |
51 | 50 | |
52 | 51 | if ( !$hasNoErrors ) { |
53 | 52 | if ( $egValidatorErrorLevel < Validator_ERRORS_STRICT ) $validator->correctInvalidParams(); |
54 | 53 | if ( $egValidatorErrorLevel >= Validator_ERRORS_WARN ) $this->errors = $validator->getErrors(); |
55 | 54 | } |
56 | | - |
| 55 | + |
57 | 56 | return !$hasFatalError ? $validator->getValidParams() : false; |
58 | 57 | } |
59 | 58 | |
Index: trunk/extensions/Validator/Validator_Formats.php |
— | — | @@ -25,8 +25,10 @@ |
26 | 26 | * Ensures the value is an array. |
27 | 27 | * |
28 | 28 | * @param $value |
| 29 | + * @param string name The name of the parameter. |
| 30 | + * @param array $parameters Array containing data about the so far handled parameters. |
29 | 31 | */ |
30 | | - public static function format_array( &$value ) { |
| 32 | + public static function format_array( &$value, $name, array $parameters ) { |
31 | 33 | if ( ! is_array( $value ) ) $value = array( $value ); |
32 | 34 | } |
33 | 35 | |
— | — | @@ -34,13 +36,15 @@ |
35 | 37 | * Ensures the value is an array. |
36 | 38 | * |
37 | 39 | * @param $value |
| 40 | + * @param string name The name of the parameter. |
| 41 | + * @param array $parameters Array containing data about the so far handled parameters. |
38 | 42 | */ |
39 | | - public static function format_filtered_array( &$value ) { |
| 43 | + public static function format_filtered_array( &$value, $name, array $parameters ) { |
40 | 44 | // TODO: It's possible the way the allowed values are passed here is quite inneficient... |
41 | 45 | $params = func_get_args(); |
42 | 46 | array_shift( $params ); // Ommit the value |
43 | 47 | |
44 | | - self::format_array( $value ); |
| 48 | + self::format_array( $value, $name, $parameters ); |
45 | 49 | $filtered = array(); |
46 | 50 | foreach ( $value as $item ) if ( in_array( $item, $params ) ) $filtered[] = $item; |
47 | 51 | |
— | — | @@ -53,11 +57,13 @@ |
54 | 58 | * will also work for single values. |
55 | 59 | * |
56 | 60 | * @param $value |
| 61 | + * @param string name The name of the parameter. |
| 62 | + * @param array $parameters Array containing data about the so far handled parameters. |
57 | 63 | * @param $delimiter |
58 | 64 | * @param $wrapper |
59 | 65 | */ |
60 | | - public static function format_list( &$value, $delimiter = ',', $wrapper = '' ) { |
61 | | - self::format_array( $value ); |
| 66 | + public static function format_list( &$value, $name, array $parameters, $delimiter = ',', $wrapper = '' ) { |
| 67 | + self::format_array( $value, $name, $parameters ); |
62 | 68 | $value = $wrapper . implode( $wrapper . $delimiter . $wrapper, $value ) . $wrapper; |
63 | 69 | } |
64 | 70 | |
— | — | @@ -67,8 +73,10 @@ |
68 | 74 | * TODO: work with a list of true-values. |
69 | 75 | * |
70 | 76 | * @param $value |
| 77 | + * @param string name The name of the parameter. |
| 78 | + * @param array $parameters Array containing data about the so far handled parameters. |
71 | 79 | */ |
72 | | - public static function format_boolean( &$value ) { |
| 80 | + public static function format_boolean( &$value, $name, array $parameters ) { |
73 | 81 | if ( is_array( $value ) ) { |
74 | 82 | $boolArray = array(); |
75 | 83 | foreach ( $value as $item ) $boolArray[] = in_array( $item, array( 'yes', 'on' ) ); |
— | — | @@ -83,9 +91,11 @@ |
84 | 92 | * Changes every value into a boolean, represented by a 'false' or 'true' string. |
85 | 93 | * |
86 | 94 | * @param $value |
| 95 | + * @param string name The name of the parameter. |
| 96 | + * @param array $parameters Array containing data about the so far handled parameters. |
87 | 97 | */ |
88 | | - public static function format_boolean_string( &$value ) { |
89 | | - self::format_boolean( $value ); |
| 98 | + public static function format_boolean_string( &$value, $name, array $parameters ) { |
| 99 | + self::format_boolean( $value, $name, $parameters ); |
90 | 100 | if ( is_array( $value ) ) { |
91 | 101 | $boolArray = array(); |
92 | 102 | foreach ( $value as $item ) $boolArray[] = $item ? 'true' : 'false'; |
— | — | @@ -100,8 +110,10 @@ |
101 | 111 | * Changes lists into strings, by enumerating the items using $wgLang->listToText. |
102 | 112 | * |
103 | 113 | * @param $value |
| 114 | + * @param string name The name of the parameter. |
| 115 | + * @param array $parameters Array containing data about the so far handled parameters. |
104 | 116 | */ |
105 | | - public static function format_string( &$value ) { |
| 117 | + public static function format_string( &$value, $name, array $parameters ) { |
106 | 118 | if ( is_array( $value ) ) { |
107 | 119 | global $wgLang; |
108 | 120 | $value = $wgLang->listToText( $value ); |
— | — | @@ -112,8 +124,10 @@ |
113 | 125 | * Removes duplicate items from lists. |
114 | 126 | * |
115 | 127 | * @param $value |
| 128 | + * @param string name The name of the parameter. |
| 129 | + * @param array $parameters Array containing data about the so far handled parameters. |
116 | 130 | */ |
117 | | - public static function format_unique_items( &$value ) { |
| 131 | + public static function format_unique_items( &$value, $name, array $parameters ) { |
118 | 132 | if ( is_array( $value ) ) $value = array_unique( $value ); |
119 | 133 | } |
120 | 134 | |
Index: trunk/extensions/Validator/TopologicalSort.php |
— | — | @@ -22,26 +22,19 @@ |
23 | 23 | class TopologicalSort { |
24 | 24 | |
25 | 25 | private $nodes = array(); |
26 | | - private $looseNodes = array(); |
| 26 | + private $nodeNames = array(); |
27 | 27 | |
28 | 28 | /** |
29 | 29 | * Dependency pairs are a list of arrays in the form |
30 | 30 | * $name => $val where $key must come before $val in load order. |
31 | 31 | */ |
32 | 32 | function TopologicalSort( $dependencies = array(), $parse = true ) { |
33 | | - $rawDependencies = $dependencies; |
| 33 | + $this->nodeNames = array_keys( $dependencies ); |
34 | 34 | |
35 | 35 | if ( $parse ) { |
36 | 36 | $dependencies = $this->parseDependencyList( $dependencies ); |
37 | 37 | } |
38 | 38 | |
39 | | - // Store items that have no dependencies, and therefore no nodes. |
40 | | - foreach( $rawDependencies as $item => $dependency ) { |
41 | | - if ( !in_array( $item, $dependencies ) && !array_key_exists( $item, $dependencies ) ) { |
42 | | - $this->looseNodes[] = $item; |
43 | | - } |
44 | | - } |
45 | | - |
46 | 39 | // turn pairs into double-linked node tree |
47 | 40 | foreach ( $dependencies as $key => $dpair ) { |
48 | 41 | list ( $module, $dependency ) = each ( $dpair ); |
— | — | @@ -55,16 +48,10 @@ |
56 | 49 | /** |
57 | 50 | * Perform Topological Sort. |
58 | 51 | * |
59 | | - * @param array $nodes optional array of node objects may be passed. |
60 | | - * Default is $this->nodes created in constructor. |
61 | | - * |
62 | 52 | * @return sorted array |
63 | 53 | */ |
64 | | - public function doSort( array $nodes = array() ) { |
65 | | - // use this->nodes if it is populated and no param passed |
66 | | - if ( !count( $nodes ) && count( $this->nodes ) ) { |
67 | | - $nodes = $this->nodes; |
68 | | - } |
| 54 | + public function doSort() { |
| 55 | + $nodes = $this->nodes; |
69 | 56 | |
70 | 57 | // get nodes without parents |
71 | 58 | $root_nodes = array_values( $this->getRootNodes( $nodes ) ); |
— | — | @@ -104,8 +91,16 @@ |
105 | 92 | unset( $nodes[$n->name] ); |
106 | 93 | } |
107 | 94 | |
| 95 | + $looseNodes = array(); |
| 96 | + |
108 | 97 | // Return the result with the loose nodes (items with no dependencies) appended. |
109 | | - return array_merge( $sorted, $this->looseNodes ); |
| 98 | + foreach( $this->nodeNames as $name ) { |
| 99 | + if ( !in_array( $name, $sorted ) ) { |
| 100 | + $looseNodes[] = $name; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return array_merge( $sorted, $looseNodes ); |
110 | 105 | } |
111 | 106 | |
112 | 107 | /** |
Index: trunk/extensions/Validator/Validator.php |
— | — | @@ -24,7 +24,7 @@ |
25 | 25 | die( 'Not an entry point.' ); |
26 | 26 | } |
27 | 27 | |
28 | | -define( 'Validator_VERSION', '0.3 a4' ); |
| 28 | +define( 'Validator_VERSION', '0.3 a6' ); |
29 | 29 | |
30 | 30 | // Constants indicating the strictness of the parameter validation. |
31 | 31 | define( 'Validator_ERRORS_NONE', 0 ); |