| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
öm |
| 6 |
o |
| 7 |
ki |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
m |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
namespace MediaWiki\Html; |
| 30 |
|
| 31 |
use ArrayAccess; |
| 32 |
use InvalidArgumentException; |
| 33 |
use MediaWiki\Request\WebRequest; |
| 34 |
use UnexpectedValueException; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class FormOptions implements ArrayAccess { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public const AUTO = -1; |
| 49 |
|
| 50 |
public const STRING = 0; |
| 51 |
|
| 52 |
public const INT = 1; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public const FLOAT = 4; |
| 57 |
|
| 58 |
public const BOOL = 2; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public const INTNULL = 3; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public const ARR = 5; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
protected $options = []; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function add( $name, $default, $type = self::AUTO ) { |
| 92 |
$option = []; |
| 93 |
$option['default'] = $default; |
| 94 |
$option['value'] = null; |
| 95 |
$option['consumed'] = false; |
| 96 |
|
| 97 |
if ( $type !== self::AUTO ) { |
| 98 |
$option['type'] = $type; |
| 99 |
} else { |
| 100 |
$option['type'] = self::guessType( $default ); |
| 101 |
} |
| 102 |
|
| 103 |
$this->options[$name] = $option; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function delete( $name ) { |
| 112 |
$this->validateName( $name, true ); |
| 113 |
unset( $this->options[$name] ); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public static function guessType( $data ) { |
| 126 |
if ( is_bool( $data ) ) { |
| 127 |
return self::BOOL; |
| 128 |
} elseif ( is_int( $data ) ) { |
| 129 |
return self::INT; |
| 130 |
} elseif ( is_float( $data ) ) { |
| 131 |
return self::FLOAT; |
| 132 |
} elseif ( is_string( $data ) ) { |
| 133 |
return self::STRING; |
| 134 |
} elseif ( is_array( $data ) ) { |
| 135 |
return self::ARR; |
| 136 |
} else { |
| 137 |
throw new InvalidArgumentException( 'Unsupported datatype' ); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function validateName( $name, $strict = false ) { |
| 151 |
if ( !isset( $this->options[$name] ) ) { |
| 152 |
if ( $strict ) { |
| 153 |
throw new InvalidArgumentException( "Invalid option $name" ); |
| 154 |
} else { |
| 155 |
return false; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public function setValue( $name, $value, $force = false ) { |
| 171 |
$this->validateName( $name, true ); |
| 172 |
|
| 173 |
if ( !$force && $value === $this->options[$name]['default'] ) { |
| 174 |
|
| 175 |
$this->options[$name]['value'] = null; |
| 176 |
} else { |
| 177 |
$this->options[$name]['value'] = $value; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function getValue( $name ) { |
| 190 |
$this->validateName( $name, true ); |
| 191 |
|
| 192 |
return $this->getValueReal( $this->options[$name] ); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
protected function getValueReal( $option ) { |
| 202 |
if ( $option['value'] !== null ) { |
| 203 |
return $option['value']; |
| 204 |
} else { |
| 205 |
return $option['default']; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public function reset( $name ) { |
| 215 |
$this->validateName( $name, true ); |
| 216 |
$this->options[$name]['value'] = null; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public function consumeValue( $name ) { |
| 228 |
$this->validateName( $name, true ); |
| 229 |
$this->options[$name]['consumed'] = true; |
| 230 |
|
| 231 |
return $this->getValueReal( $this->options[$name] ); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public function consumeValues( $names ) { |
| 243 |
$out = []; |
| 244 |
|
| 245 |
foreach ( $names as $name ) { |
| 246 |
$this->validateName( $name, true ); |
| 247 |
$this->options[$name]['consumed'] = true; |
| 248 |
$out[] = $this->getValueReal( $this->options[$name] ); |
| 249 |
} |
| 250 |
|
| 251 |
return $out; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public function validateIntBounds( $name, $min, $max ) { |
| 261 |
$this->validateBounds( $name, $min, $max ); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public function validateBounds( $name, $min, $max ) { |
| 275 |
$this->validateName( $name, true ); |
| 276 |
$type = $this->options[$name]['type']; |
| 277 |
|
| 278 |
if ( $type !== self::INT && $type !== self::INTNULL && $type !== self::FLOAT ) { |
| 279 |
throw new InvalidArgumentException( "Type of option $name is not numeric" ); |
| 280 |
} |
| 281 |
|
| 282 |
$value = $this->getValueReal( $this->options[$name] ); |
| 283 |
if ( $type !== self::INTNULL || $value !== null ) { |
| 284 |
$this->setValue( $name, max( $min, min( $max, $value ) ) ); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public function getUnconsumedValues( $all = false ) { |
| 295 |
$values = []; |
| 296 |
|
| 297 |
foreach ( $this->options as $name => $data ) { |
| 298 |
if ( !$data['consumed'] ) { |
| 299 |
if ( $all || $data['value'] !== null ) { |
| 300 |
$values[$name] = $this->getValueReal( $data ); |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
return $values; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public function getChangedValues() { |
| 313 |
$values = []; |
| 314 |
|
| 315 |
foreach ( $this->options as $name => $data ) { |
| 316 |
if ( $data['value'] !== null ) { |
| 317 |
$values[$name] = $data['value']; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
return $values; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public function getAllValues() { |
| 329 |
$values = []; |
| 330 |
|
| 331 |
foreach ( $this->options as $name => $data ) { |
| 332 |
$values[$name] = $this->getValueReal( $data ); |
| 333 |
} |
| 334 |
|
| 335 |
return $values; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
public function fetchValuesFromRequest( WebRequest $r, $optionKeys = null ) { |
| 350 |
if ( !$optionKeys ) { |
| 351 |
$optionKeys = array_keys( $this->options ); |
| 352 |
} |
| 353 |
|
| 354 |
foreach ( $optionKeys as $name ) { |
| 355 |
$default = $this->options[$name]['default']; |
| 356 |
$type = $this->options[$name]['type']; |
| 357 |
|
| 358 |
switch ( $type ) { |
| 359 |
case self::BOOL: |
| 360 |
$value = $r->getBool( $name, $default ); |
| 361 |
break; |
| 362 |
case self::INT: |
| 363 |
$value = $r->getInt( $name, $default ); |
| 364 |
break; |
| 365 |
case self::FLOAT: |
| 366 |
$value = $r->getFloat( $name, $default ); |
| 367 |
break; |
| 368 |
case self::STRING: |
| 369 |
$value = $r->getText( $name, $default ); |
| 370 |
break; |
| 371 |
case self::INTNULL: |
| 372 |
$value = $r->getIntOrNull( $name ); |
| 373 |
break; |
| 374 |
case self::ARR: |
| 375 |
$value = $r->getArray( $name ); |
| 376 |
|
| 377 |
if ( $value !== null ) { |
| 378 |
|
| 379 |
$value = array_filter( $value, 'is_scalar' ); |
| 380 |
} |
| 381 |
break; |
| 382 |
default: |
| 383 |
throw new UnexpectedValueException( "Unsupported datatype $type" ); |
| 384 |
} |
| 385 |
|
| 386 |
if ( $value !== null ) { |
| 387 |
$this->options[$name]['value'] = $value === $default ? null : $value; |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public function offsetExists( $name ): bool { |
| 405 |
return isset( $this->options[$name] ); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
#[\ReturnTypeWillChange] |
| 414 |
public function offsetGet( $name ) { |
| 415 |
return $this->getValue( $name ); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
public function offsetSet( $name, $value ): void { |
| 424 |
$this->setValue( $name, $value ); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
public function offsetUnset( $name ): void { |
| 432 |
$this->delete( $name ); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
class_alias( FormOptions::class, 'FormOptions' ); |