| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\ParamValidator\TypeDef; |
| 4 |
|
| 5 |
use Wikimedia\Message\MessageValue; |
| 6 |
use Wikimedia\Message\ParamType; |
| 7 |
use Wikimedia\Message\ScalarParam; |
| 8 |
use Wikimedia\ParamValidator\ParamValidator; |
| 9 |
use Wikimedia\ParamValidator\TypeDef; |
| 10 |
use Wikimedia\ParamValidator\ValidationException; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
abstract class NumericDef extends TypeDef { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public const PARAM_IGNORE_RANGE = 'param-ignore-range'; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public const PARAM_MIN = 'param-min'; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public const PARAM_MAX = 'param-max'; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public const PARAM_MAX2 = 'param-max2'; |
| 49 |
|
| 50 |
|
| 51 |
protected $valueType = 'integer'; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 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 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 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 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 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 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 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 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 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 |
|
| 216 |
|
| 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 |
} |