MediaWiki  master
NumericDef.php
Go to the documentation of this file.
1 <?php
2 
4 
11 
25 abstract class NumericDef extends TypeDef {
26 
32  public const PARAM_IGNORE_RANGE = 'param-ignore-range';
33 
37  public const PARAM_MIN = 'param-min';
38 
42  public const PARAM_MAX = 'param-max';
43 
47  public const PARAM_MAX2 = 'param-max2';
48 
50  protected $valueType = 'integer';
51 
63  protected function checkRange( $value, $name, $origValue, array $settings, array $options ) {
64  $min = $settings[self::PARAM_MIN] ?? null;
65  $max1 = $settings[self::PARAM_MAX] ?? null;
66  $max2 = $settings[self::PARAM_MAX2] ?? $max1;
67  $err = false;
68 
69  if ( $min !== null && $value < $min ) {
70  $err = true;
71  $value = $min;
72  } elseif ( $max1 !== null && $value > $max1 ) {
73  if ( $max2 > $max1 && $this->callbacks->useHighLimits( $options ) ) {
74  if ( $value > $max2 ) {
75  $err = true;
76  $value = $max2;
77  }
78  } else {
79  $err = true;
80  $value = $max1;
81  }
82  }
83  if ( $err ) {
84  $what = '';
85  if ( $min !== null ) {
86  $what .= 'min';
87  }
88  if ( $max1 !== null ) {
89  $what .= 'max';
90  }
91  $max = $max2 !== null && $max2 > $max1 && $this->callbacks->useHighLimits( $options )
92  ? $max2 : $max1;
93  $this->failure(
94  $this->failureMessage( 'outofrange', [
95  'min' => $min,
96  'curmax' => $max,
97  'max' => $max1,
98  'highmax' => $max2,
99  ], $what )->numParams( $min ?? '', $max ?? '' ),
100  $name, $origValue, $settings, $options,
101  empty( $settings[self::PARAM_IGNORE_RANGE] )
102  );
103  }
104 
105  return $value;
106  }
107 
108  public function normalizeSettings( array $settings ) {
109  if ( !isset( $settings[self::PARAM_MAX] ) ) {
110  unset( $settings[self::PARAM_MAX2] );
111  }
112 
113  if ( isset( $settings[self::PARAM_MAX2] ) && isset( $settings[self::PARAM_MAX] ) &&
114  $settings[self::PARAM_MAX2] < $settings[self::PARAM_MAX]
115  ) {
116  $settings[self::PARAM_MAX2] = $settings[self::PARAM_MAX];
117  }
118 
119  return parent::normalizeSettings( $settings );
120  }
121 
122  public function checkSettings( string $name, $settings, array $options, array $ret ) : array {
123  $ret = parent::checkSettings( $name, $settings, $options, $ret );
124 
125  $ret['allowedKeys'] = array_merge( $ret['allowedKeys'], [
126  self::PARAM_IGNORE_RANGE, self::PARAM_MIN, self::PARAM_MAX, self::PARAM_MAX2,
127  ] );
128 
129  if ( !is_bool( $settings[self::PARAM_IGNORE_RANGE] ?? false ) ) {
130  $ret['issues'][self::PARAM_IGNORE_RANGE] = 'PARAM_IGNORE_RANGE must be boolean, got '
131  . gettype( $settings[self::PARAM_IGNORE_RANGE] );
132  }
133 
134  $min = $settings[self::PARAM_MIN] ?? null;
135  $max = $settings[self::PARAM_MAX] ?? null;
136  $max2 = $settings[self::PARAM_MAX2] ?? null;
137  if ( $min !== null && gettype( $min ) !== $this->valueType ) {
138  $ret['issues'][self::PARAM_MIN] = "PARAM_MIN must be $this->valueType, got " . gettype( $min );
139  }
140  if ( $max !== null && gettype( $max ) !== $this->valueType ) {
141  $ret['issues'][self::PARAM_MAX] = "PARAM_MAX must be $this->valueType, got " . gettype( $max );
142  }
143  if ( $max2 !== null && gettype( $max2 ) !== $this->valueType ) {
144  $ret['issues'][self::PARAM_MAX2] = "PARAM_MAX2 must be $this->valueType, got "
145  . gettype( $max2 );
146  }
147 
148  if ( $min !== null && $max !== null && $min > $max ) {
149  $ret['issues'][] = "PARAM_MIN must be less than or equal to PARAM_MAX, but $min > $max";
150  }
151  if ( $max2 !== null ) {
152  if ( $max === null ) {
153  $ret['issues'][] = 'PARAM_MAX2 cannot be used without PARAM_MAX';
154  } elseif ( $max2 < $max ) {
155  $ret['issues'][] = "PARAM_MAX2 must be greater than or equal to PARAM_MAX, but $max2 < $max";
156  }
157  }
158 
159  return $ret;
160  }
161 
162  public function getParamInfo( $name, array $settings, array $options ) {
163  $info = parent::getParamInfo( $name, $settings, $options );
164 
165  $info['min'] = $settings[self::PARAM_MIN] ?? null;
166  $info['max'] = $settings[self::PARAM_MAX] ?? null;
167  $info['highmax'] = $settings[self::PARAM_MAX2] ?? $info['max'];
168  if ( $info['max'] === null || $info['highmax'] <= $info['max'] ) {
169  unset( $info['highmax'] );
170  }
171 
172  return $info;
173  }
174 
175  public function getHelpInfo( $name, array $settings, array $options ) {
176  $info = parent::getHelpInfo( $name, $settings, $options );
177 
178  $min = '−∞';
179  $max = '∞';
180  $msg = '';
181  if ( isset( $settings[self::PARAM_MIN] ) ) {
182  $msg .= 'min';
183  $min = new ScalarParam( ParamType::NUM, $settings[self::PARAM_MIN] );
184  }
185  if ( isset( $settings[self::PARAM_MAX] ) ) {
186  $msg .= 'max';
187  $max = $settings[self::PARAM_MAX];
188  if ( isset( $settings[self::PARAM_MAX2] ) && $settings[self::PARAM_MAX2] > $max &&
189  $this->callbacks->useHighLimits( $options )
190  ) {
191  $max = $settings[self::PARAM_MAX2];
192  }
193  $max = new ScalarParam( ParamType::NUM, $max );
194  }
195  if ( $msg !== '' ) {
196  $isMulti = !empty( $settings[ParamValidator::PARAM_ISMULTI] );
197 
198  // Messages: paramvalidator-help-type-number-min, paramvalidator-help-type-number-max,
199  // paramvalidator-help-type-number-minmax
200  $info[self::PARAM_MIN] = MessageValue::new( "paramvalidator-help-type-number-$msg" )
201  ->params( $isMulti ? 2 : 1, $min, $max );
202  }
203 
204  return $info;
205  }
206 
207 }
Wikimedia\ParamValidator\TypeDef\NumericDef\checkSettings
checkSettings(string $name, $settings, array $options, array $ret)
Validate a parameter settings array.
Definition: NumericDef.php:122
Wikimedia\ParamValidator\ValidationException
Error reporting for ParamValidator.
Definition: ValidationException.php:15
Wikimedia\ParamValidator\TypeDef\failureMessage
failureMessage( $code, array $data=null, $suffix=null)
Create a DataMessageValue representing a failure.
Definition: TypeDef.php:78
Wikimedia\ParamValidator\TypeDef\failure
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition: TypeDef.php:43
Wikimedia\Message\ScalarParam
Value object representing a message parameter holding a single value.
Definition: ScalarParam.php:10
Wikimedia\ParamValidator\TypeDef\NumericDef\PARAM_MAX
const PARAM_MAX
(int|float) Maximum allowed value (normal limits)
Definition: NumericDef.php:42
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:14
Wikimedia\ParamValidator\TypeDef
Base definition for ParamValidator types.
Definition: TypeDef.php:18
Wikimedia\ParamValidator\TypeDef\NumericDef
Type definition base class for numeric types.
Definition: NumericDef.php:25
Wikimedia\ParamValidator\TypeDef\NumericDef\normalizeSettings
normalizeSettings(array $settings)
Normalize a settings array.
Definition: NumericDef.php:108
Wikimedia\ParamValidator\ParamValidator\PARAM_ISMULTI
const PARAM_ISMULTI
(bool) Indicate that the parameter is multi-valued.
Definition: ParamValidator.php:112
Wikimedia\ParamValidator\TypeDef\NumericDef\PARAM_MIN
const PARAM_MIN
(int|float) Minimum allowed value.
Definition: NumericDef.php:37
Wikimedia\ParamValidator\TypeDef\NumericDef\getHelpInfo
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.
Definition: NumericDef.php:175
Wikimedia\ParamValidator\TypeDef\NumericDef\PARAM_MAX2
const PARAM_MAX2
(int|float) Maximum allowed value (high limits)
Definition: NumericDef.php:47
Wikimedia\ParamValidator\TypeDef\NumericDef\$valueType
string $valueType
PHP type (as from gettype()) of values this NumericDef handles.
Definition: NumericDef.php:50
Wikimedia\ParamValidator\TypeDef
Definition: BooleanDef.php:3
Wikimedia\Message\ParamType\NUM
const NUM
A number, to be formatted using local digits and separators.
Definition: ParamType.php:16
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:38
Wikimedia\ParamValidator\TypeDef\NumericDef\PARAM_IGNORE_RANGE
const PARAM_IGNORE_RANGE
(bool) Whether to enforce the specified range.
Definition: NumericDef.php:32
Wikimedia\ParamValidator\TypeDef\NumericDef\getParamInfo
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.
Definition: NumericDef.php:162
Wikimedia\ParamValidator\TypeDef\NumericDef\checkRange
checkRange( $value, $name, $origValue, array $settings, array $options)
Check the range of a value.
Definition: NumericDef.php:63
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
Wikimedia\Message\ParamType
The constants used to specify parameter types.
Definition: ParamType.php:11