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