3 namespace Wikimedia\ParamValidator\TypeDef
;
5 use Wikimedia\ParamValidator\TypeDef
;
6 use Wikimedia\ParamValidator\ValidationException
;
9 * Type definition for integer types
11 * A valid representation consists of an optional sign (`+` or `-`) followed by
12 * one or more decimal digits.
14 * The result from validate() is a PHP integer.
16 * * ValidationException codes:
17 * - 'badinteger': The value was invalid or could not be represented as a PHP
20 * Additional codes may be generated when using certain PARAM constants. See
21 * the constants' documentation for details.
25 class IntegerDef
extends TypeDef
{
28 * (bool) Whether to enforce the specified range.
30 * If set and truthy, ValidationExceptions from PARAM_MIN, PARAM_MAX, and
31 * PARAM_MAX2 are non-fatal.
33 const PARAM_IGNORE_RANGE
= 'param-ignore-range';
36 * (int) Minimum allowed value.
38 * ValidationException codes:
39 * - 'belowminimum': The value was below the allowed minimum. Data:
40 * - 'min': Allowed minimum, or empty string if there is none.
41 * - 'max': Allowed (normal) maximum, or empty string if there is none.
42 * - 'max2': Allowed (high limits) maximum, or empty string if there is none.
44 const PARAM_MIN
= 'param-min';
47 * (int) Maximum allowed value (normal limits)
49 * ValidationException codes:
50 * - 'abovemaximum': The value was above the allowed maximum. Data:
51 * - 'min': Allowed minimum, or empty string if there is none.
52 * - 'max': Allowed (normal) maximum, or empty string if there is none.
53 * - 'max2': Allowed (high limits) maximum, or empty string if there is none.
55 const PARAM_MAX
= 'param-max';
58 * (int) Maximum allowed value (high limits)
60 * If not specified, PARAM_MAX will be enforced for all users. Ignored if
61 * PARAM_MAX is not set.
63 * ValidationException codes:
64 * - 'abovehighmaximum': The value was above the allowed maximum. Data:
65 * - 'min': Allowed minimum, or empty string if there is none.
66 * - 'max': Allowed (normal) maximum, or empty string if there is none.
67 * - 'max2': Allowed (high limits) maximum, or empty string if there is none.
69 const PARAM_MAX2
= 'param-max2';
71 public function validate( $name, $value, array $settings, array $options ) {
72 if ( !preg_match( '/^[+-]?\d+$/D', $value ) ) {
73 throw new ValidationException( $name, $value, $settings, 'badinteger', [] );
75 $ret = intval( $value, 10 );
77 // intval() returns min/max on overflow, so check that
78 if ( $ret === PHP_INT_MAX ||
$ret === PHP_INT_MIN
) {
79 $tmp = ( $ret < 0 ?
'-' : '' ) . ltrim( $value, '-0' );
80 if ( $tmp !== (string)$ret ) {
81 throw new ValidationException( $name, $value, $settings, 'badinteger', [] );
85 $min = $settings[self
::PARAM_MIN
] ??
null;
86 $max = $settings[self
::PARAM_MAX
] ??
null;
87 $max2 = $settings[self
::PARAM_MAX2
] ??
null;
90 if ( $min !== null && $ret < $min ) {
91 $err = 'belowminimum';
93 } elseif ( $max !== null && $ret > $max ) {
94 if ( $max2 !== null && $this->callbacks
->useHighLimits( $options ) ) {
96 $err = 'abovehighmaximum';
100 $err = 'abovemaximum';
104 if ( $err !== null ) {
105 $ex = new ValidationException( $name, $value, $settings, $err, [
106 'min' => $min === null ?
'' : $min,
107 'max' => $max === null ?
'' : $max,
108 'max2' => $max2 === null ?
'' : $max2,
110 if ( empty( $settings[self
::PARAM_IGNORE_RANGE
] ) ) {
113 $this->callbacks
->recordCondition( $ex, $options );
119 public function normalizeSettings( array $settings ) {
120 if ( !isset( $settings[self
::PARAM_MAX
] ) ) {
121 unset( $settings[self
::PARAM_MAX2
] );
124 if ( isset( $settings[self
::PARAM_MAX2
] ) && isset( $settings[self
::PARAM_MAX
] ) &&
125 $settings[self
::PARAM_MAX2
] < $settings[self
::PARAM_MAX
]
127 $settings[self
::PARAM_MAX2
] = $settings[self
::PARAM_MAX
];
130 return parent
::normalizeSettings( $settings );
133 public function describeSettings( $name, array $settings, array $options ) {
134 $info = parent
::describeSettings( $name, $settings, $options );
136 $min = $settings[self
::PARAM_MIN
] ??
'';
137 $max = $settings[self
::PARAM_MAX
] ??
'';
138 $max2 = $settings[self
::PARAM_MAX2
] ??
'';
139 if ( $max === '' ||
$max2 !== '' && $max2 <= $max ) {
143 if ( empty( $options['compact'] ) ) {
150 if ( $max2 !== '' ) {
151 $info['max2'] = $max2;
158 if ( $max2 !== '' ) {
160 } elseif ( $max !== '' ) {
164 $info[$key] = [ 'min' => $min, 'max' => $max, 'max2' => $max2 ];