MediaWiki  master
LimitDef.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
18 class LimitDef extends IntegerDef {
19 
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  // Cannot be multi-valued
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 }
Wikimedia\ParamValidator\TypeDef\LimitDef\normalizeSettings
normalizeSettings(array $settings)
Normalize a settings array Stable to override.array Stable to override Stable to override
Definition: LimitDef.php:40
Wikimedia\ParamValidator\TypeDef\LimitDef\checkSettings
checkSettings(string $name, $settings, array $options, array $ret)
Validate a parameter settings array.This is intended for validation of parameter settings during unit...
Definition: LimitDef.php:51
Wikimedia\ParamValidator\TypeDef\NumericDef\PARAM_MAX
const PARAM_MAX
(int|float) Maximum allowed value (normal limits)
Definition: NumericDef.php:43
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
Wikimedia\ParamValidator\TypeDef\LimitDef\getHelpInfo
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.Keys in the returned array should generally corr...
Definition: LimitDef.php:71
Wikimedia\ParamValidator\ParamValidator\PARAM_ISMULTI
const PARAM_ISMULTI
(bool) Indicate that the parameter is multi-valued.
Definition: ParamValidator.php:112
Wikimedia\ParamValidator\TypeDef\LimitDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.When ParamValidator is processing a multi-valued parameter, this will be called on...
Definition: LimitDef.php:27
Wikimedia\ParamValidator\TypeDef\LimitDef
Type definition for "limit" types.
Definition: LimitDef.php:18
Wikimedia\ParamValidator\TypeDef\IntegerDef
Type definition for integer types.
Definition: IntegerDef.php:23
Wikimedia\ParamValidator\TypeDef\NumericDef\PARAM_MAX2
const PARAM_MAX2
(int|float) Maximum allowed value (high limits)
Definition: NumericDef.php:48
Wikimedia\ParamValidator\TypeDef
Definition: BooleanDef.php:3
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:42
Wikimedia\ParamValidator\ParamValidator\PARAM_TYPE
const PARAM_TYPE
(string|array) Type of the parameter.
Definition: ParamValidator.php:76
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42