| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\ParamValidator\TypeDef; |
| 4 |
|
| 5 |
use Wikimedia\Message\MessageValue; |
| 6 |
use Wikimedia\ParamValidator\ParamValidator; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class LimitDef extends IntegerDef { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public function validate( $name, $value, array $settings, array $options ) { |
| 28 |
if ( $value === 'max' ) { |
| 29 |
if ( $options['parse-limit'] ?? true ) { |
| 30 |
$value = $this->callbacks->useHighLimits( $options ) |
| 31 |
? $settings[self::PARAM_MAX2] ?? $settings[self::PARAM_MAX] ?? PHP_INT_MAX |
| 32 |
: $settings[self::PARAM_MAX] ?? PHP_INT_MAX; |
| 33 |
} |
| 34 |
return $value; |
| 35 |
} |
| 36 |
|
| 37 |
return parent::validate( $name, $value, $settings, $options ); |
| 38 |
} |
| 39 |
|
| 40 |
public function normalizeSettings( array $settings ) { |
| 41 |
$settings += [ |
| 42 |
self::PARAM_MIN => 0, |
| 43 |
]; |
| 44 |
|
| 45 |
|
| 46 |
$settings[ParamValidator::PARAM_ISMULTI] = false; |
| 47 |
|
| 48 |
return parent::normalizeSettings( $settings ); |
| 49 |
} |
| 50 |
|
| 51 |
public function checkSettings( string $name, $settings, array $options, array $ret ): array { |
| 52 |
$ret = parent::checkSettings( $name, $settings, $options, $ret ); |
| 53 |
|
| 54 |
if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) && |
| 55 |
!isset( $ret['issues'][ParamValidator::PARAM_ISMULTI] ) |
| 56 |
) { |
| 57 |
$ret['issues'][ParamValidator::PARAM_ISMULTI] = |
| 58 |
'PARAM_ISMULTI cannot be used for limit-type parameters'; |
| 59 |
} |
| 60 |
|
| 61 |
if ( ( $settings[self::PARAM_MIN] ?? 0 ) < 0 ) { |
| 62 |
$ret['issues'][] = 'PARAM_MIN must be greater than or equal to 0'; |
| 63 |
} |
| 64 |
if ( !isset( $settings[self::PARAM_MAX] ) ) { |
| 65 |
$ret['issues'][] = 'PARAM_MAX must be set'; |
| 66 |
} |
| 67 |
|
| 68 |
return $ret; |
| 69 |
} |
| 70 |
|
| 71 |
public function getHelpInfo( $name, array $settings, array $options ) { |
| 72 |
$info = parent::getHelpInfo( $name, $settings, $options ); |
| 73 |
|
| 74 |
$info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-limit' ) |
| 75 |
->params( 1 ); |
| 76 |
|
| 77 |
return $info; |
| 78 |
} |
| 79 |
|
| 80 |
} |