Index: trunk/extensions/Validator/includes/ListParameter.php |
— | — | @@ -0,0 +1,62 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Class for list parameters. |
| 6 | + * |
| 7 | + * @since 0.4 |
| 8 | + * |
| 9 | + * @file ListParameter.php |
| 10 | + * @ingroup Validator |
| 11 | + * |
| 12 | + * @author Jeroen De Dauw |
| 13 | + */ |
| 14 | +class ListParameter extends Parameter { |
| 15 | + |
| 16 | + /** |
| 17 | + * The default delimiter for lists, used when the parameter definition does not specify one. |
| 18 | + * |
| 19 | + * @since 0.4 |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + const DEFAULT_DELIMITER = ','; |
| 24 | + |
| 25 | + /** |
| 26 | + * The list delimiter. |
| 27 | + * |
| 28 | + * @since 0.4 |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected $delimiter; |
| 33 | + |
| 34 | + /** |
| 35 | + * Constructor. |
| 36 | + * |
| 37 | + * @since 0.4 |
| 38 | + * |
| 39 | + * @param string $name |
| 40 | + * @param string $delimiter |
| 41 | + * @param mixed $type |
| 42 | + * @param mixed $default Use null for no default (which makes the parameter required) |
| 43 | + * @param array $aliases |
| 44 | + * @param array $criteria |
| 45 | + */ |
| 46 | + public function __construct( $name, $delimiter = ListParameter::DEFAULT_DELIMITER, $type = Parameter::TYPE_STRING, |
| 47 | + $default = null, array $aliases = array(), array $criteria = array() ) { |
| 48 | + parent::construct( $name, $type, $default, $aliases, $criteria ); |
| 49 | + $this->delimiter = $delimiter; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns if the parameter is a list or not. |
| 54 | + * |
| 55 | + * @since 0.4 |
| 56 | + * |
| 57 | + * @return boolean |
| 58 | + */ |
| 59 | + public function isList() { |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | +} |
\ No newline at end of file |
Property changes on: trunk/extensions/Validator/includes/ListParameter.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 64 | + native |
Index: trunk/extensions/Validator/includes/Parameter.php |
— | — | @@ -3,8 +3,6 @@ |
4 | 4 | /** |
5 | 5 | * Parameter definition class. |
6 | 6 | * |
7 | | - * TODO: create deriving ListParameter class and split list logic off to it |
8 | | - * |
9 | 7 | * @since 0.4 |
10 | 8 | * |
11 | 9 | * @file Parameter.php |
— | — | @@ -22,15 +20,6 @@ |
23 | 21 | const TYPE_CHAR = 'char'; |
24 | 22 | |
25 | 23 | /** |
26 | | - * The default delimiter for lists, used when the parameter definition does not specify one. |
27 | | - * |
28 | | - * @since 0.4 |
29 | | - * |
30 | | - * @var string |
31 | | - */ |
32 | | - public static $defaultListDelimeter = ','; |
33 | | - |
34 | | - /** |
35 | 24 | * Indicates if the parameter value should be lowercased. |
36 | 25 | * |
37 | 26 | * @since 0.4 |
— | — | @@ -76,13 +65,11 @@ |
77 | 66 | protected $name; |
78 | 67 | |
79 | 68 | /** |
80 | | - * The type of the parameter. Is either a string of the Parameter::TYPE_ enum, |
81 | | - * or an array with such a string as first element, and optionaly a delimiter |
82 | | - * as second element for list types. |
| 69 | + * The type of the parameter, element of the Parameter::TYPE_ enum. |
83 | 70 | * |
84 | 71 | * @since 0.4 |
85 | 72 | * |
86 | | - * @var mixed |
| 73 | + * @var string |
87 | 74 | */ |
88 | 75 | protected $type; |
89 | 76 | |
— | — | @@ -100,11 +87,41 @@ |
101 | 88 | * |
102 | 89 | * @since 0.4 |
103 | 90 | * |
104 | | - * @var array |
| 91 | + * @var array of ParameterCriterion |
105 | 92 | */ |
106 | 93 | protected $criteria; |
107 | 94 | |
108 | 95 | /** |
| 96 | + * The original parameter name as provided by the user. This can be the |
| 97 | + * main name or an alias. |
| 98 | + * |
| 99 | + * @since 0.4 |
| 100 | + * |
| 101 | + * @var string |
| 102 | + */ |
| 103 | + protected $originalName; |
| 104 | + |
| 105 | + /** |
| 106 | + * The original value as provided by the user. This is mainly retained for |
| 107 | + * usage in error messages when the parameter turns out to be invalid. |
| 108 | + * |
| 109 | + * @since 0.4 |
| 110 | + * |
| 111 | + * @var string |
| 112 | + */ |
| 113 | + protected $originalValue; |
| 114 | + |
| 115 | + /** |
| 116 | + * Keeps track of how many times the parameter has been set by the user. |
| 117 | + * This is used to detect overrides and for figuring out a parameter is missing. |
| 118 | + * |
| 119 | + * @since 0.4 |
| 120 | + * |
| 121 | + * @var integer |
| 122 | + */ |
| 123 | + protected $setCount = 0; |
| 124 | + |
| 125 | + /** |
109 | 126 | * Returns a new instance of Parameter by converting a Validator 3.x-style parameter array definition. |
110 | 127 | * Note: this method is for backward compatibility and should not be used in new code. |
111 | 128 | * |
— | — | @@ -116,15 +133,17 @@ |
117 | 134 | * @return Parameter |
118 | 135 | */ |
119 | 136 | public static function newFromArray( $name, array $definition ) { |
| 137 | + $isList = false; |
| 138 | + $delimiter = false; |
| 139 | + |
120 | 140 | if ( array_key_exists( 'type', $definition ) ) { |
121 | 141 | if ( is_array( $definition['type'] ) ) { |
122 | 142 | if ( count( $definition['type'] ) > 1 ) { |
| 143 | + $isList = true; |
| 144 | + |
123 | 145 | if ( count( $definition['type'] ) > 2 ) { |
124 | | - $type = array( $definition['type'][0], $definition['type'][2] ); |
| 146 | + $delimiter = $definition['type'][2]; |
125 | 147 | } |
126 | | - else { |
127 | | - $type = array( $definition['type'][0] ); |
128 | | - } |
129 | 148 | } |
130 | 149 | else { |
131 | 150 | $type = $definition['type'][0]; |
— | — | @@ -145,13 +164,25 @@ |
146 | 165 | $default = array_key_exists( 'default', $definition ) ? $definition['default'] : ''; |
147 | 166 | } |
148 | 167 | |
149 | | - $parameter = new Parameter( |
150 | | - $name, |
151 | | - $type, |
152 | | - $default, |
153 | | - array_key_exists( 'aliases', $definition ) ? $definition['aliases'] : array(), |
154 | | - array_key_exists( 'criteria', $definition ) ? $definition['criteria'] : array() |
155 | | - ); |
| 168 | + if ( $isList ) { |
| 169 | + $parameter = new ListParameter( |
| 170 | + $name, |
| 171 | + $delimiter, |
| 172 | + $type, |
| 173 | + $default, |
| 174 | + array_key_exists( 'aliases', $definition ) ? $definition['aliases'] : array(), |
| 175 | + array_key_exists( 'criteria', $definition ) ? $definition['criteria'] : array() |
| 176 | + ); |
| 177 | + } |
| 178 | + else { |
| 179 | + $parameter = new Parameter( |
| 180 | + $name, |
| 181 | + $type, |
| 182 | + $default, |
| 183 | + array_key_exists( 'aliases', $definition ) ? $definition['aliases'] : array(), |
| 184 | + array_key_exists( 'criteria', $definition ) ? $definition['criteria'] : array() |
| 185 | + ); |
| 186 | + } |
156 | 187 | |
157 | 188 | if ( array_key_exists( 'output-types', $definition ) ) { |
158 | 189 | $types = array(); |
— | — | @@ -191,7 +222,7 @@ |
192 | 223 | * @since 0.4 |
193 | 224 | * |
194 | 225 | * @param string $name |
195 | | - * @param mixed $type |
| 226 | + * @param string $type |
196 | 227 | * @param mixed $default Use null for no default (which makes the parameter required) |
197 | 228 | * @param array $aliases |
198 | 229 | * @param array $criteria |
— | — | @@ -205,6 +236,47 @@ |
206 | 237 | } |
207 | 238 | |
208 | 239 | /** |
| 240 | + * @since 0.4 |
| 241 | + * |
| 242 | + * @param string $paramName |
| 243 | + * @param string $paramValue |
| 244 | + */ |
| 245 | + public function setUserValue( $paramName, $paramValue ) { |
| 246 | + if ( $this->setCount > 0 && true /* TODO: accept overridng? */ ) { |
| 247 | + // TODO: fatal error |
| 248 | + } |
| 249 | + else { |
| 250 | + $this->originalName = $paramName; |
| 251 | + $this->paramValue = $paramValue; |
| 252 | + |
| 253 | + $this->setCount++; |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * Validates the parameter value against it's criteria. |
| 259 | + * |
| 260 | + * @since 0.4 |
| 261 | + */ |
| 262 | + public function validate() { |
| 263 | + if ( $this->setCount == 0 ) { |
| 264 | + if ( $this->isRequired() ) { |
| 265 | + // TODO: fatal error |
| 266 | + } |
| 267 | + else { |
| 268 | + $this->validateCriteria( $this->default ); |
| 269 | + } |
| 270 | + } |
| 271 | + else { |
| 272 | + $this->validateCriteria( $this->originalValue ); |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + protected function validateCriteria( $value ) { |
| 277 | + |
| 278 | + } |
| 279 | + |
| 280 | + /** |
209 | 281 | * Returns the parameters main name. |
210 | 282 | * |
211 | 283 | * @since 0.4 |
— | — | @@ -234,7 +306,7 @@ |
235 | 307 | * @return boolean |
236 | 308 | */ |
237 | 309 | public function isList() { |
238 | | - return is_array( $this->type ); |
| 310 | + return false; |
239 | 311 | } |
240 | 312 | |
241 | 313 | /** |
— | — | @@ -251,14 +323,14 @@ |
252 | 324 | else { |
253 | 325 | return false; |
254 | 326 | } |
255 | | - } |
| 327 | + } |
256 | 328 | |
257 | 329 | /** |
258 | 330 | * Returns the parameter criteria. |
259 | 331 | * |
260 | 332 | * @since 0.4 |
261 | 333 | * |
262 | | - * @return array |
| 334 | + * @return array of ParameterCriterion |
263 | 335 | */ |
264 | 336 | public function getCriteria() { |
265 | 337 | return array_merge( $this->getCriteriaForType(), $this->criteria ); |
Index: trunk/extensions/Validator/Validator.php |
— | — | @@ -53,6 +53,7 @@ |
54 | 54 | |
55 | 55 | // Autoload the general classes. |
56 | 56 | $incDir = dirname( __FILE__ ) . '/includes/'; |
| 57 | +$wgAutoloadClasses['ListParameter'] = $incDir . 'ListParameter.php'; |
57 | 58 | $wgAutoloadClasses['Parameter'] = $incDir . 'Parameter.php'; |
58 | 59 | $wgAutoloadClasses['ParserHook'] = $incDir . 'ParserHook.php'; |
59 | 60 | $wgAutoloadClasses['Validator'] = $incDir . 'Validator.php'; |