| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\ParamValidator; |
| 4 |
|
| 5 |
use DomainException; |
| 6 |
use InvalidArgumentException; |
| 7 |
use Wikimedia\Assert\Assert; |
| 8 |
use Wikimedia\Message\DataMessageValue; |
| 9 |
use Wikimedia\Message\MessageValue; |
| 10 |
use Wikimedia\Message\ParamType; |
| 11 |
use Wikimedia\Message\ScalarParam; |
| 12 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class ParamValidator { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public const PARAM_DEFAULT = 'param-default'; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public const PARAM_TYPE = 'param-type'; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public const PARAM_REQUIRED = 'param-required'; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public const PARAM_ISMULTI = 'param-ismulti'; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public const PARAM_ISMULTI_LIMIT1 = 'param-ismulti-limit1'; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public const PARAM_ISMULTI_LIMIT2 = 'param-ismulti-limit2'; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public const PARAM_ALL = 'param-all'; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public const PARAM_ALLOW_DUPLICATES = 'param-allow-duplicates'; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public const PARAM_SENSITIVE = 'param-sensitive'; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public const PARAM_DEPRECATED = 'param-deprecated'; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public const PARAM_IGNORE_UNRECOGNIZED_VALUES = 'param-ignore-unrecognized-values'; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public const OPT_ENFORCE_JSON_TYPES = TypeDef::OPT_ENFORCE_JSON_TYPES; |
| 177 |
|
| 178 |
|
| 179 |
public const ALL_DEFAULT_STRING = '*'; |
| 180 |
|
| 181 |
|
| 182 |
public const STANDARD_TYPES = [ |
| 183 |
'boolean' => [ 'class' => TypeDef\BooleanDef::class ], |
| 184 |
'checkbox' => [ 'class' => TypeDef\PresenceBooleanDef::class ], |
| 185 |
'integer' => [ 'class' => TypeDef\IntegerDef::class ], |
| 186 |
'limit' => [ 'class' => TypeDef\LimitDef::class ], |
| 187 |
'float' => [ 'class' => TypeDef\FloatDef::class ], |
| 188 |
'double' => [ 'class' => TypeDef\FloatDef::class ], |
| 189 |
'string' => [ 'class' => TypeDef\StringDef::class ], |
| 190 |
'password' => [ 'class' => TypeDef\PasswordDef::class ], |
| 191 |
'NULL' => [ |
| 192 |
'class' => TypeDef\StringDef::class, |
| 193 |
'args' => [ [ |
| 194 |
TypeDef\StringDef::OPT_ALLOW_EMPTY => true, |
| 195 |
] ], |
| 196 |
], |
| 197 |
'timestamp' => [ 'class' => TypeDef\TimestampDef::class ], |
| 198 |
'upload' => [ 'class' => TypeDef\UploadDef::class ], |
| 199 |
'enum' => [ 'class' => TypeDef\EnumDef::class ], |
| 200 |
'expiry' => [ 'class' => TypeDef\ExpiryDef::class ], |
| 201 |
]; |
| 202 |
|
| 203 |
|
| 204 |
private $callbacks; |
| 205 |
|
| 206 |
|
| 207 |
private $objectFactory; |
| 208 |
|
| 209 |
|
| 210 |
private $typeDefs = []; |
| 211 |
|
| 212 |
|
| 213 |
private $ismultiLimit1; |
| 214 |
|
| 215 |
|
| 216 |
private $ismultiLimit2; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public function __construct( |
| 228 |
Callbacks $callbacks, |
| 229 |
ObjectFactory $objectFactory, |
| 230 |
array $options = [] |
| 231 |
) { |
| 232 |
$this->callbacks = $callbacks; |
| 233 |
$this->objectFactory = $objectFactory; |
| 234 |
|
| 235 |
$this->addTypeDefs( $options['typeDefs'] ?? self::STANDARD_TYPES ); |
| 236 |
$this->ismultiLimit1 = $options['ismultiLimits'][0] ?? 50; |
| 237 |
$this->ismultiLimit2 = $options['ismultiLimits'][1] ?? 500; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public function knownTypes() { |
| 245 |
return array_keys( $this->typeDefs ); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function addTypeDefs( array $typeDefs ) { |
| 255 |
foreach ( $typeDefs as $name => $def ) { |
| 256 |
$this->addTypeDef( $name, $def ); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public function addTypeDef( $name, $typeDef ) { |
| 274 |
Assert::parameterType( |
| 275 |
[ TypeDef::class, 'array' ], |
| 276 |
$typeDef, |
| 277 |
'$typeDef' |
| 278 |
); |
| 279 |
|
| 280 |
if ( isset( $this->typeDefs[$name] ) ) { |
| 281 |
throw new InvalidArgumentException( "Type '$name' is already registered" ); |
| 282 |
} |
| 283 |
$this->typeDefs[$name] = $typeDef; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public function overrideTypeDef( $name, $typeDef ) { |
| 293 |
Assert::parameterType( |
| 294 |
[ TypeDef::class, 'array', 'null' ], |
| 295 |
$typeDef, |
| 296 |
'$typeDef' |
| 297 |
); |
| 298 |
|
| 299 |
if ( $typeDef === null ) { |
| 300 |
unset( $this->typeDefs[$name] ); |
| 301 |
} else { |
| 302 |
$this->typeDefs[$name] = $typeDef; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
public function hasTypeDef( $name ) { |
| 312 |
return isset( $this->typeDefs[$name] ); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
public function getTypeDef( $type ) { |
| 321 |
if ( is_array( $type ) ) { |
| 322 |
$type = 'enum'; |
| 323 |
} |
| 324 |
|
| 325 |
if ( !isset( $this->typeDefs[$type] ) ) { |
| 326 |
return null; |
| 327 |
} |
| 328 |
|
| 329 |
$def = $this->typeDefs[$type]; |
| 330 |
if ( !$def instanceof TypeDef ) { |
| 331 |
$def = $this->objectFactory->createObject( $def, [ |
| 332 |
'extraArgs' => [ $this->callbacks ], |
| 333 |
'assertClass' => TypeDef::class, |
| 334 |
] ); |
| 335 |
$this->typeDefs[$type] = $def; |
| 336 |
} |
| 337 |
|
| 338 |
return $def; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
private function normalizeSettingsInternal( $settings ) { |
| 347 |
|
| 348 |
if ( !is_array( $settings ) ) { |
| 349 |
$settings = [ |
| 350 |
self::PARAM_DEFAULT => $settings, |
| 351 |
]; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
if ( !isset( $settings[self::PARAM_TYPE] ) ) { |
| 356 |
$settings[self::PARAM_TYPE] = gettype( $settings[self::PARAM_DEFAULT] ?? null ); |
| 357 |
} |
| 358 |
|
| 359 |
return $settings; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public function normalizeSettings( $settings ) { |
| 369 |
$settings = $this->normalizeSettingsInternal( $settings ); |
| 370 |
|
| 371 |
$typeDef = $this->getTypeDef( $settings[self::PARAM_TYPE] ); |
| 372 |
if ( $typeDef ) { |
| 373 |
$settings = $typeDef->normalizeSettings( $settings ); |
| 374 |
} |
| 375 |
|
| 376 |
return $settings; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
public function checkSettings( string $name, $settings, array $options ): array { |
| 398 |
$settings = $this->normalizeSettingsInternal( $settings ); |
| 399 |
$issues = []; |
| 400 |
$allowedKeys = [ |
| 401 |
self::PARAM_TYPE, self::PARAM_DEFAULT, self::PARAM_REQUIRED, self::PARAM_ISMULTI, |
| 402 |
self::PARAM_SENSITIVE, self::PARAM_DEPRECATED, self::PARAM_IGNORE_UNRECOGNIZED_VALUES, |
| 403 |
]; |
| 404 |
$messages = []; |
| 405 |
|
| 406 |
$type = $settings[self::PARAM_TYPE]; |
| 407 |
$typeDef = null; |
| 408 |
if ( !is_string( $type ) && !is_array( $type ) ) { |
| 409 |
$issues[self::PARAM_TYPE] = 'PARAM_TYPE must be a string or array, got ' . gettype( $type ); |
| 410 |
} else { |
| 411 |
$typeDef = $this->getTypeDef( $settings[self::PARAM_TYPE] ); |
| 412 |
if ( !$typeDef ) { |
| 413 |
if ( is_array( $type ) ) { |
| 414 |
$type = 'enum'; |
| 415 |
} |
| 416 |
$issues[self::PARAM_TYPE] = "Unknown/unregistered PARAM_TYPE \"$type\""; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
if ( isset( $settings[self::PARAM_DEFAULT] ) ) { |
| 421 |
try { |
| 422 |
$this->validateValue( |
| 423 |
$name, $settings[self::PARAM_DEFAULT], $settings, [ 'is-default' => true ] + $options |
| 424 |
); |
| 425 |
} catch ( ValidationException $ex ) { |
| 426 |
$issues[self::PARAM_DEFAULT] = 'Value for PARAM_DEFAULT does not validate (code ' |
| 427 |
. $ex->getFailureMessage()->getCode() . ')'; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
if ( !is_bool( $settings[self::PARAM_REQUIRED] ?? false ) ) { |
| 432 |
$issues[self::PARAM_REQUIRED] = 'PARAM_REQUIRED must be boolean, got ' |
| 433 |
. gettype( $settings[self::PARAM_REQUIRED] ); |
| 434 |
} |
| 435 |
|
| 436 |
if ( !is_bool( $settings[self::PARAM_ISMULTI] ?? false ) ) { |
| 437 |
$issues[self::PARAM_ISMULTI] = 'PARAM_ISMULTI must be boolean, got ' |
| 438 |
. gettype( $settings[self::PARAM_ISMULTI] ); |
| 439 |
} |
| 440 |
|
| 441 |
if ( !empty( $settings[self::PARAM_ISMULTI] ) ) { |
| 442 |
$allowedKeys = array_merge( $allowedKeys, [ |
| 443 |
self::PARAM_ISMULTI_LIMIT1, self::PARAM_ISMULTI_LIMIT2, |
| 444 |
self::PARAM_ALL, self::PARAM_ALLOW_DUPLICATES |
| 445 |
] ); |
| 446 |
|
| 447 |
$limit1 = $settings[self::PARAM_ISMULTI_LIMIT1] ?? $this->ismultiLimit1; |
| 448 |
$limit2 = $settings[self::PARAM_ISMULTI_LIMIT2] ?? $this->ismultiLimit2; |
| 449 |
if ( !is_int( $limit1 ) ) { |
| 450 |
$issues[self::PARAM_ISMULTI_LIMIT1] = 'PARAM_ISMULTI_LIMIT1 must be an integer, got ' |
| 451 |
. gettype( $settings[self::PARAM_ISMULTI_LIMIT1] ); |
| 452 |
} elseif ( $limit1 <= 0 ) { |
| 453 |
$issues[self::PARAM_ISMULTI_LIMIT1] = |
| 454 |
"PARAM_ISMULTI_LIMIT1 must be greater than 0, got $limit1"; |
| 455 |
} |
| 456 |
if ( !is_int( $limit2 ) ) { |
| 457 |
$issues[self::PARAM_ISMULTI_LIMIT2] = 'PARAM_ISMULTI_LIMIT2 must be an integer, got ' |
| 458 |
. gettype( $settings[self::PARAM_ISMULTI_LIMIT2] ); |
| 459 |
} elseif ( $limit2 < $limit1 ) { |
| 460 |
$issues[self::PARAM_ISMULTI_LIMIT2] = |
| 461 |
'PARAM_ISMULTI_LIMIT2 must be greater than or equal to PARAM_ISMULTI_LIMIT1, but ' |
| 462 |
. "$limit2 < $limit1"; |
| 463 |
} |
| 464 |
|
| 465 |
$all = $settings[self::PARAM_ALL] ?? false; |
| 466 |
if ( !is_string( $all ) && !is_bool( $all ) ) { |
| 467 |
$issues[self::PARAM_ALL] = 'PARAM_ALL must be a string or boolean, got ' . gettype( $all ); |
| 468 |
} elseif ( $all !== false && $typeDef ) { |
| 469 |
if ( $all === true ) { |
| 470 |
$all = self::ALL_DEFAULT_STRING; |
| 471 |
} |
| 472 |
$values = $typeDef->getEnumValues( $name, $settings, $options ); |
| 473 |
if ( !is_array( $values ) ) { |
| 474 |
$issues[self::PARAM_ALL] = 'PARAM_ALL cannot be used with non-enumerated types'; |
| 475 |
} elseif ( in_array( $all, $values, true ) ) { |
| 476 |
$issues[self::PARAM_ALL] = 'Value for PARAM_ALL conflicts with an enumerated value'; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
if ( !is_bool( $settings[self::PARAM_ALLOW_DUPLICATES] ?? false ) ) { |
| 481 |
$issues[self::PARAM_ALLOW_DUPLICATES] = 'PARAM_ALLOW_DUPLICATES must be boolean, got ' |
| 482 |
. gettype( $settings[self::PARAM_ALLOW_DUPLICATES] ); |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
if ( !is_bool( $settings[self::PARAM_SENSITIVE] ?? false ) ) { |
| 487 |
$issues[self::PARAM_SENSITIVE] = 'PARAM_SENSITIVE must be boolean, got ' |
| 488 |
. gettype( $settings[self::PARAM_SENSITIVE] ); |
| 489 |
} |
| 490 |
|
| 491 |
if ( !is_bool( $settings[self::PARAM_DEPRECATED] ?? false ) ) { |
| 492 |
$issues[self::PARAM_DEPRECATED] = 'PARAM_DEPRECATED must be boolean, got ' |
| 493 |
. gettype( $settings[self::PARAM_DEPRECATED] ); |
| 494 |
} |
| 495 |
|
| 496 |
if ( !is_bool( $settings[self::PARAM_IGNORE_UNRECOGNIZED_VALUES] ?? false ) ) { |
| 497 |
$issues[self::PARAM_IGNORE_UNRECOGNIZED_VALUES] = 'PARAM_IGNORE_UNRECOGNIZED_VALUES must be ' |
| 498 |
. 'boolean, got ' . gettype( $settings[self::PARAM_IGNORE_UNRECOGNIZED_VALUES] ); |
| 499 |
} |
| 500 |
|
| 501 |
$ret = [ 'issues' => $issues, 'allowedKeys' => $allowedKeys, 'messages' => $messages ]; |
| 502 |
if ( $typeDef ) { |
| 503 |
$ret = $typeDef->checkSettings( $name, $settings, $options, $ret ); |
| 504 |
} |
| 505 |
|
| 506 |
return $ret; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
public function getValue( $name, $settings, array $options = [] ) { |
| 521 |
$settings = $this->normalizeSettings( $settings ); |
| 522 |
|
| 523 |
$typeDef = $this->getTypeDef( $settings[self::PARAM_TYPE] ); |
| 524 |
if ( !$typeDef ) { |
| 525 |
throw new DomainException( |
| 526 |
"Param $name's type is unknown - {$settings[self::PARAM_TYPE]}" |
| 527 |
); |
| 528 |
} |
| 529 |
|
| 530 |
$value = $typeDef->getValue( $name, $settings, $options ); |
| 531 |
|
| 532 |
if ( $value !== null ) { |
| 533 |
if ( !empty( $settings[self::PARAM_SENSITIVE] ) ) { |
| 534 |
$strValue = $typeDef->stringifyValue( $name, $value, $settings, $options ); |
| 535 |
$this->callbacks->recordCondition( |
| 536 |
DataMessageValue::new( 'paramvalidator-param-sensitive', [], 'param-sensitive' ) |
| 537 |
->plaintextParams( $name, $strValue ), |
| 538 |
$name, $value, $settings, $options |
| 539 |
); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
if ( !empty( $settings[self::PARAM_DEPRECATED] ) ) { |
| 544 |
$strValue = $typeDef->stringifyValue( $name, $value, $settings, $options ); |
| 545 |
$this->callbacks->recordCondition( |
| 546 |
DataMessageValue::new( 'paramvalidator-param-deprecated', [], 'param-deprecated' ) |
| 547 |
->plaintextParams( $name, $strValue ), |
| 548 |
$name, $value, $settings, $options |
| 549 |
); |
| 550 |
} |
| 551 |
} elseif ( isset( $settings[self::PARAM_DEFAULT] ) ) { |
| 552 |
$value = $settings[self::PARAM_DEFAULT]; |
| 553 |
$options['is-default'] = true; |
| 554 |
} |
| 555 |
|
| 556 |
return $this->validateValue( $name, $value, $settings, $options ); |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
public function validateValue( $name, $value, $settings, array $options = [] ) { |
| 573 |
$settings = $this->normalizeSettings( $settings ); |
| 574 |
|
| 575 |
$typeDef = $this->getTypeDef( $settings[self::PARAM_TYPE] ); |
| 576 |
if ( !$typeDef ) { |
| 577 |
throw new DomainException( |
| 578 |
"Param $name's type is unknown - {$settings[self::PARAM_TYPE]}" |
| 579 |
); |
| 580 |
} |
| 581 |
|
| 582 |
if ( $value === null ) { |
| 583 |
if ( !empty( $settings[self::PARAM_REQUIRED] ) ) { |
| 584 |
throw new ValidationException( |
| 585 |
DataMessageValue::new( 'paramvalidator-missingparam', [], 'missingparam' ) |
| 586 |
->plaintextParams( $name ), |
| 587 |
$name, $value, $settings |
| 588 |
); |
| 589 |
} |
| 590 |
return null; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
if ( empty( $settings[self::PARAM_ISMULTI] ) ) { |
| 595 |
if ( is_string( $value ) && substr( $value, 0, 1 ) === "\x1f" ) { |
| 596 |
throw new ValidationException( |
| 597 |
DataMessageValue::new( 'paramvalidator-notmulti', [], 'badvalue' ) |
| 598 |
->plaintextParams( $name, $value ), |
| 599 |
$name, $value, $settings |
| 600 |
); |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
if ( is_array( $value ) && !$typeDef->supportsArrays() ) { |
| 607 |
throw new ValidationException( |
| 608 |
DataMessageValue::new( 'paramvalidator-notmulti', [], 'badvalue' ) |
| 609 |
->plaintextParams( $name, gettype( $value ) ), |
| 610 |
$name, $value, $settings |
| 611 |
); |
| 612 |
} |
| 613 |
|
| 614 |
return $typeDef->validate( $name, $value, $settings, $options ); |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
$limit1 = $settings[self::PARAM_ISMULTI_LIMIT1] ?? $this->ismultiLimit1; |
| 619 |
$limit2 = max( $limit1, $settings[self::PARAM_ISMULTI_LIMIT2] ?? $this->ismultiLimit2 ); |
| 620 |
|
| 621 |
if ( is_array( $value ) ) { |
| 622 |
$valuesList = $value; |
| 623 |
} elseif ( $options[ self::OPT_ENFORCE_JSON_TYPES ] ?? false ) { |
| 624 |
throw new ValidationException( |
| 625 |
DataMessageValue::new( |
| 626 |
'paramvalidator-multivalue-must-be-array', |
| 627 |
[], |
| 628 |
'multivalue-must-be-array' |
| 629 |
)->plaintextParams( $name ), |
| 630 |
$name, $value, $settings |
| 631 |
); |
| 632 |
} else { |
| 633 |
$valuesList = self::explodeMultiValue( $value, $limit2 + 1 ); |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
$enumValues = $typeDef->getEnumValues( $name, $settings, $options ); |
| 638 |
if ( is_array( $enumValues ) && isset( $settings[self::PARAM_ALL] ) && |
| 639 |
count( $valuesList ) === 1 |
| 640 |
) { |
| 641 |
$allValue = is_string( $settings[self::PARAM_ALL] ) |
| 642 |
? $settings[self::PARAM_ALL] |
| 643 |
: self::ALL_DEFAULT_STRING; |
| 644 |
if ( $valuesList[0] === $allValue ) { |
| 645 |
return $enumValues; |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
$sizeLimit = ( |
| 651 |
$limit2 > $limit1 && count( $valuesList ) > $limit1 && |
| 652 |
$this->callbacks->useHighLimits( $options ) |
| 653 |
) ? $limit2 : $limit1; |
| 654 |
if ( count( $valuesList ) > $sizeLimit ) { |
| 655 |
throw new ValidationException( |
| 656 |
DataMessageValue::new( 'paramvalidator-toomanyvalues', [], 'toomanyvalues', [ |
| 657 |
'parameter' => $name, |
| 658 |
'limit' => $sizeLimit, |
| 659 |
'lowlimit' => $limit1, |
| 660 |
'highlimit' => $limit2, |
| 661 |
] )->plaintextParams( $name )->numParams( $sizeLimit ), |
| 662 |
$name, $valuesList, $settings |
| 663 |
); |
| 664 |
} |
| 665 |
|
| 666 |
$options['values-list'] = $valuesList; |
| 667 |
$validValues = []; |
| 668 |
$invalidValues = []; |
| 669 |
foreach ( $valuesList as $v ) { |
| 670 |
try { |
| 671 |
$validValues[] = $typeDef->validate( $name, $v, $settings, $options ); |
| 672 |
} catch ( ValidationException $ex ) { |
| 673 |
if ( $ex->getFailureMessage()->getCode() !== 'badvalue' || |
| 674 |
empty( $settings[self::PARAM_IGNORE_UNRECOGNIZED_VALUES] ) |
| 675 |
) { |
| 676 |
throw $ex; |
| 677 |
} |
| 678 |
$invalidValues[] = $v; |
| 679 |
} |
| 680 |
} |
| 681 |
if ( $invalidValues ) { |
| 682 |
if ( is_array( $value ) ) { |
| 683 |
$value = self::implodeMultiValue( $value ); |
| 684 |
} |
| 685 |
$this->callbacks->recordCondition( |
| 686 |
DataMessageValue::new( 'paramvalidator-unrecognizedvalues', [], 'unrecognizedvalues', [ |
| 687 |
'values' => $invalidValues, |
| 688 |
] ) |
| 689 |
->plaintextParams( $name, $value ) |
| 690 |
->commaListParams( array_map( static function ( $v ) { |
| 691 |
return new ScalarParam( ParamType::PLAINTEXT, $v ); |
| 692 |
}, $invalidValues ) ) |
| 693 |
->numParams( count( $invalidValues ) ), |
| 694 |
$name, $value, $settings, $options |
| 695 |
); |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
if ( empty( $settings[self::PARAM_ALLOW_DUPLICATES] ) ) { |
| 700 |
$validValues = array_values( array_unique( $validValues ) ); |
| 701 |
} |
| 702 |
|
| 703 |
return $validValues; |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
public function getParamInfo( $name, $settings, array $options ) { |
| 716 |
$settings = $this->normalizeSettings( $settings ); |
| 717 |
$typeDef = $this->getTypeDef( $settings[self::PARAM_TYPE] ); |
| 718 |
$info = []; |
| 719 |
|
| 720 |
$info['type'] = $settings[self::PARAM_TYPE]; |
| 721 |
$info['required'] = !empty( $settings[self::PARAM_REQUIRED] ); |
| 722 |
if ( !empty( $settings[self::PARAM_DEPRECATED] ) ) { |
| 723 |
$info['deprecated'] = true; |
| 724 |
} |
| 725 |
if ( !empty( $settings[self::PARAM_SENSITIVE] ) ) { |
| 726 |
$info['sensitive'] = true; |
| 727 |
} |
| 728 |
if ( isset( $settings[self::PARAM_DEFAULT] ) ) { |
| 729 |
$info['default'] = $settings[self::PARAM_DEFAULT]; |
| 730 |
} |
| 731 |
$info['multi'] = !empty( $settings[self::PARAM_ISMULTI] ); |
| 732 |
if ( $info['multi'] ) { |
| 733 |
$info['lowlimit'] = $settings[self::PARAM_ISMULTI_LIMIT1] ?? $this->ismultiLimit1; |
| 734 |
$info['highlimit'] = max( |
| 735 |
$info['lowlimit'], $settings[self::PARAM_ISMULTI_LIMIT2] ?? $this->ismultiLimit2 |
| 736 |
); |
| 737 |
$info['limit'] = |
| 738 |
$info['highlimit'] > $info['lowlimit'] && $this->callbacks->useHighLimits( $options ) |
| 739 |
? $info['highlimit'] |
| 740 |
: $info['lowlimit']; |
| 741 |
|
| 742 |
if ( !empty( $settings[self::PARAM_ALLOW_DUPLICATES] ) ) { |
| 743 |
$info['allowsduplicates'] = true; |
| 744 |
} |
| 745 |
|
| 746 |
$allSpecifier = $settings[self::PARAM_ALL] ?? false; |
| 747 |
if ( $allSpecifier !== false ) { |
| 748 |
if ( !is_string( $allSpecifier ) ) { |
| 749 |
$allSpecifier = self::ALL_DEFAULT_STRING; |
| 750 |
} |
| 751 |
$info['allspecifier'] = $allSpecifier; |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
if ( $typeDef ) { |
| 756 |
$info = array_merge( $info, $typeDef->getParamInfo( $name, $settings, $options ) ); |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
return array_filter( $info, static function ( $v ) { |
| 761 |
return $v !== null; |
| 762 |
} ); |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
public function getHelpInfo( $name, $settings, array $options ) { |
| 775 |
$settings = $this->normalizeSettings( $settings ); |
| 776 |
$typeDef = $this->getTypeDef( $settings[self::PARAM_TYPE] ); |
| 777 |
|
| 778 |
|
| 779 |
$info = [ |
| 780 |
self::PARAM_DEPRECATED => null, |
| 781 |
self::PARAM_REQUIRED => null, |
| 782 |
self::PARAM_SENSITIVE => null, |
| 783 |
self::PARAM_TYPE => null, |
| 784 |
self::PARAM_ISMULTI => null, |
| 785 |
self::PARAM_ISMULTI_LIMIT1 => null, |
| 786 |
self::PARAM_ALL => null, |
| 787 |
self::PARAM_DEFAULT => null, |
| 788 |
]; |
| 789 |
|
| 790 |
if ( !empty( $settings[self::PARAM_DEPRECATED] ) ) { |
| 791 |
$info[self::PARAM_DEPRECATED] = MessageValue::new( 'paramvalidator-help-deprecated' ); |
| 792 |
} |
| 793 |
|
| 794 |
if ( !empty( $settings[self::PARAM_REQUIRED] ) ) { |
| 795 |
$info[self::PARAM_REQUIRED] = MessageValue::new( 'paramvalidator-help-required' ); |
| 796 |
} |
| 797 |
|
| 798 |
if ( !empty( $settings[self::PARAM_ISMULTI] ) ) { |
| 799 |
$info[self::PARAM_ISMULTI] = MessageValue::new( 'paramvalidator-help-multi-separate' ); |
| 800 |
|
| 801 |
$lowcount = $settings[self::PARAM_ISMULTI_LIMIT1] ?? $this->ismultiLimit1; |
| 802 |
$highcount = max( $lowcount, $settings[self::PARAM_ISMULTI_LIMIT2] ?? $this->ismultiLimit2 ); |
| 803 |
$values = $typeDef ? $typeDef->getEnumValues( $name, $settings, $options ) : null; |
| 804 |
if ( |
| 805 |
|
| 806 |
$values === null || count( $values ) > $lowcount || |
| 807 |
!empty( $settings[self::PARAM_ALLOW_DUPLICATES] ) |
| 808 |
) { |
| 809 |
if ( $highcount > $lowcount ) { |
| 810 |
$info[self::PARAM_ISMULTI_LIMIT1] = MessageValue::new( 'paramvalidator-help-multi-max' ) |
| 811 |
->numParams( $lowcount, $highcount ); |
| 812 |
} else { |
| 813 |
$info[self::PARAM_ISMULTI_LIMIT1] = MessageValue::new( 'paramvalidator-help-multi-max-simple' ) |
| 814 |
->numParams( $lowcount ); |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
$allSpecifier = $settings[self::PARAM_ALL] ?? false; |
| 819 |
if ( $allSpecifier !== false ) { |
| 820 |
if ( !is_string( $allSpecifier ) ) { |
| 821 |
$allSpecifier = self::ALL_DEFAULT_STRING; |
| 822 |
} |
| 823 |
$info[self::PARAM_ALL] = MessageValue::new( 'paramvalidator-help-multi-all' ) |
| 824 |
->plaintextParams( $allSpecifier ); |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
if ( isset( $settings[self::PARAM_DEFAULT] ) && $typeDef ) { |
| 829 |
$value = $typeDef->stringifyValue( $name, $settings[self::PARAM_DEFAULT], $settings, $options ); |
| 830 |
if ( $value === '' ) { |
| 831 |
$info[self::PARAM_DEFAULT] = MessageValue::new( 'paramvalidator-help-default-empty' ); |
| 832 |
} elseif ( $value !== null ) { |
| 833 |
$info[self::PARAM_DEFAULT] = MessageValue::new( 'paramvalidator-help-default' ) |
| 834 |
->plaintextParams( $value ); |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
if ( $typeDef ) { |
| 839 |
$info = array_merge( $info, $typeDef->getHelpInfo( $name, $settings, $options ) ); |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
$default = $info[self::PARAM_DEFAULT]; |
| 844 |
unset( $info[self::PARAM_DEFAULT] ); |
| 845 |
$info[self::PARAM_DEFAULT] = $default; |
| 846 |
|
| 847 |
|
| 848 |
return array_filter( $info ); |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
public static function explodeMultiValue( $value, $limit ) { |
| 862 |
if ( $value === '' || $value === "\x1f" ) { |
| 863 |
return []; |
| 864 |
} |
| 865 |
|
| 866 |
if ( substr( $value, 0, 1 ) === "\x1f" ) { |
| 867 |
$sep = "\x1f"; |
| 868 |
$value = substr( $value, 1 ); |
| 869 |
} else { |
| 870 |
$sep = '|'; |
| 871 |
} |
| 872 |
|
| 873 |
return explode( $sep, $value, $limit ); |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
public static function implodeMultiValue( array $value ) { |
| 883 |
if ( $value === [ '' ] ) { |
| 884 |
|
| 885 |
|
| 886 |
return '|'; |
| 887 |
} |
| 888 |
|
| 889 |
foreach ( $value as $v ) { |
| 890 |
if ( strpos( $v, '|' ) !== false ) { |
| 891 |
return "\x1f" . implode( "\x1f", $value ); |
| 892 |
} |
| 893 |
} |
| 894 |
return implode( '|', $value ); |
| 895 |
} |
| 896 |
|
| 897 |
} |