Index: trunk/extensions/Validator/RELEASE-NOTES |
— | — | @@ -18,6 +18,10 @@ |
19 | 19 | registered Hooks. First option can be set via ParserHook::FH_NO_HASH to define that the function |
20 | 20 | hook should be callable without leading hash ("{{plural:...}}"-like style). |
21 | 21 | |
| 22 | +* Option for unnamed parameter handling to work without named fallback. This allows to ignore '=' |
| 23 | + within parameter values entirely, these parameters bust be set before any named parameter then. |
| 24 | + See Validator::setFunctionParams() and ParserHook::getParameterInfo() for details. |
| 25 | + |
22 | 26 | * ParserHook Validation messages will now output text in global content language instead of users interface language. |
23 | 27 | |
24 | 28 | === Validator 0.4.12 === |
Index: trunk/extensions/Validator/includes/ParserHook.php |
— | — | @@ -470,7 +470,16 @@ |
471 | 471 | } |
472 | 472 | |
473 | 473 | /** |
474 | | - * Returns the list of default parameters. |
| 474 | + * Returns the list of default parameters. These parameters can be used as |
| 475 | + * unnamed parameters where it is not necessary to use the name and the '=' as |
| 476 | + * long as there is no '=' within the value. |
| 477 | + * It is possible to define that a parameter should not have a named fallback. |
| 478 | + * Therefore the information has to be returnd as sub-array with the parameter |
| 479 | + * name as first and Validator::PARAM_UNNAMED as second value. Parameter using |
| 480 | + * this option must be set first, before any unnamed parameter in the same order |
| 481 | + * as set here. All parameters defined before the last parameter making use of |
| 482 | + * Validator::PARAM_UNNAMED will automatically be populated with this option. |
| 483 | + * |
475 | 484 | * Override in deriving classes to add default parameters. |
476 | 485 | * |
477 | 486 | * @since 0.4 |
Index: trunk/extensions/Validator/includes/Validator.php |
— | — | @@ -10,10 +10,18 @@ |
11 | 11 | * |
12 | 12 | * @licence GNU GPL v3 or later |
13 | 13 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 14 | + * @author Daniel Werner |
14 | 15 | */ |
15 | 16 | class Validator { |
16 | 17 | |
17 | 18 | /** |
| 19 | + * Flag for default parameters used in Validator::setFunctionParams(). |
| 20 | + * |
| 21 | + * @since 0.4.13 |
| 22 | + */ |
| 23 | + const PARAM_UNNAMED = 1; |
| 24 | + |
| 25 | + /** |
18 | 26 | * Array containing the parameters. |
19 | 27 | * |
20 | 28 | * @since 0.4 |
— | — | @@ -96,26 +104,52 @@ |
97 | 105 | * |
98 | 106 | * @param array $rawParams |
99 | 107 | * @param array $parameterInfo |
100 | | - * @param array $defaultParams |
| 108 | + * @param array $defaultParams array of strings or array of arrays to define which parameters can be used unnamed. |
| 109 | + * The second value in array-form is reserved for flags. Currently, Validator::PARAM_UNNAMED determines that |
| 110 | + * the parameter has no name which can be used to set it. Therefore all these parameters must be set before |
| 111 | + * any named parameter. The effecdt is, that '=' within the string won't confuse the parameter anymore like |
| 112 | + * it would happen with default parameters that still have a name as well. |
101 | 113 | * @param boolean $toLower Indicates if the parameter values should be put to lower case. Defaults to true. |
| 114 | + * |
| 115 | + * @todo: $toLower takes no effect yet. |
102 | 116 | */ |
103 | 117 | public function setFunctionParams( array $rawParams, array $parameterInfo, array $defaultParams = array(), $toLower = true ) { |
104 | 118 | $parameters = array(); |
105 | 119 | |
106 | 120 | $nr = 0; |
107 | 121 | $defaultNr = 0; |
| 122 | + $lastUnnamedDefaultNr = -1; |
108 | 123 | |
| 124 | + /* |
| 125 | + * Find last parameter with self::PARAM_UNNAMED set. Tread all parameters in front as |
| 126 | + * the flag were set for them as well to ensure that there can't be any unnamed params |
| 127 | + * after the first named param. Wouldn't be possible to determine which unnamed value |
| 128 | + * belongs to which parameter otherwise. |
| 129 | + */ |
| 130 | + for( $i = count( $defaultParams ) - 1; $i >= 0 ; $i-- ) { |
| 131 | + $dflt = $defaultParams[$i]; |
| 132 | + if( is_array( $dflt ) && !empty( $dflt[1] ) && ( $dflt[1] | self::PARAM_UNNAMED ) ) { |
| 133 | + $lastUnnamedDefaultNr = $i; |
| 134 | + break; |
| 135 | + } |
| 136 | + } |
| 137 | + |
109 | 138 | foreach ( $rawParams as $arg ) { |
110 | 139 | // Only take into account strings. If the value is not a string, |
111 | 140 | // it is not a raw parameter, and can not be parsed correctly in all cases. |
112 | | - if ( is_string( $arg ) ) { |
113 | | - $parts = explode( '=', $arg, 2 ); |
| 141 | + if ( is_string( $arg ) ) { |
| 142 | + $parts = explode( '=', $arg, ( $nr <= $lastUnnamedDefaultNr ? 1 : 2 ) ); |
114 | 143 | |
115 | 144 | // If there is only one part, no parameter name is provided, so try default parameter assignment. |
| 145 | + // Default parameters having self::PARAM_UNNAMED set for having no name alias go here in any case. |
116 | 146 | if ( count( $parts ) == 1 ) { |
117 | 147 | // Default parameter assignment is only possible when there are default parameters! |
118 | 148 | if ( count( $defaultParams ) > 0 ) { |
119 | | - $defaultParam = strtolower( array_shift( $defaultParams ) ); |
| 149 | + $defaultParam = array_shift( $defaultParams ); |
| 150 | + if( is_array( $defaultParam ) ) { |
| 151 | + $defaultParam = $defaultParam[0]; |
| 152 | + } |
| 153 | + $defaultParam = strtolower( $defaultParam ); |
120 | 154 | |
121 | 155 | $parameters[$defaultParam] = array( |
122 | 156 | 'original-value' => trim( $parts[0] ), |
— | — | @@ -161,6 +195,8 @@ |
162 | 196 | * @param array $parameters Parameter name as key, parameter value as value |
163 | 197 | * @param array $parameterInfo List of Parameter objects |
164 | 198 | * @param boolean $toLower Indicates if the parameter values should be put to lower case. Defaults to true. |
| 199 | + * |
| 200 | + * @todo: $toLower takes no effect yet. |
165 | 201 | */ |
166 | 202 | public function setParameters( array $parameters, array $parameterInfo, $toLower = true ) { |
167 | 203 | $this->parameters = $parameterInfo; |