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.
26 class IntegerDef
extends TypeDef
{
29 * (bool) Whether to enforce the specified range.
31 * If set and truthy, ValidationExceptions from PARAM_MIN, PARAM_MAX, and
32 * PARAM_MAX2 are non-fatal.
34 const PARAM_IGNORE_RANGE
= 'param-ignore-range';
37 * (int) Minimum allowed value.
39 * ValidationException codes:
40 * - 'belowminimum': The value was below the allowed minimum. Data:
41 * - 'min': Allowed minimum, or empty string if there is none.
42 * - 'max': Allowed (normal) maximum, or empty string if there is none.
43 * - 'max2': Allowed (high limits) maximum, or empty string if there is none.
45 const PARAM_MIN
= 'param-min';
48 * (int) Maximum allowed value (normal limits)
50 * ValidationException codes:
51 * - 'abovemaximum': The value was above the allowed maximum. Data:
52 * - 'min': Allowed minimum, or empty string if there is none.
53 * - 'max': Allowed (normal) maximum, or empty string if there is none.
54 * - 'max2': Allowed (high limits) maximum, or empty string if there is none.
56 const PARAM_MAX
= 'param-max';
59 * (int) Maximum allowed value (high limits)
61 * If not specified, PARAM_MAX will be enforced for all users. Ignored if
62 * PARAM_MAX is not set.
64 * ValidationException codes:
65 * - 'abovehighmaximum': The value was above the allowed maximum. Data:
66 * - 'min': Allowed minimum, or empty string if there is none.
67 * - 'max': Allowed (normal) maximum, or empty string if there is none.
68 * - 'max2': Allowed (high limits) maximum, or empty string if there is none.
70 const PARAM_MAX2
= 'param-max2';
72 public function validate( $name, $value, array $settings, array $options ) {
73 if ( !preg_match( '/^[+-]?\d+$/D', $value ) ) {
74 throw new ValidationException( $name, $value, $settings, 'badinteger', [] );
76 $ret = intval( $value, 10 );
78 // intval() returns min/max on overflow, so check that
79 if ( $ret === PHP_INT_MAX ||
$ret === PHP_INT_MIN
) {
80 $tmp = ( $ret < 0 ?
'-' : '' ) . ltrim( $value, '-0' );
81 if ( $tmp !== (string)$ret ) {
82 throw new ValidationException( $name, $value, $settings, 'badinteger', [] );
86 $min = $settings[self
::PARAM_MIN
] ??
null;
87 $max = $settings[self
::PARAM_MAX
] ??
null;
88 $max2 = $settings[self
::PARAM_MAX2
] ??
null;
91 if ( $min !== null && $ret < $min ) {
92 $err = 'belowminimum';
94 } elseif ( $max !== null && $ret > $max ) {
95 if ( $max2 !== null && $this->callbacks
->useHighLimits( $options ) ) {
97 $err = 'abovehighmaximum';
101 $err = 'abovemaximum';
105 if ( $err !== null ) {
106 $ex = new ValidationException( $name, $value, $settings, $err, [
107 'min' => $min === null ?
'' : $min,
108 'max' => $max === null ?
'' : $max,
109 'max2' => $max2 === null ?
'' : $max2,
111 if ( empty( $settings[self
::PARAM_IGNORE_RANGE
] ) ) {
114 $this->callbacks
->recordCondition( $ex, $options );
120 public function normalizeSettings( array $settings ) {
121 if ( !isset( $settings[self
::PARAM_MAX
] ) ) {
122 unset( $settings[self
::PARAM_MAX2
] );
125 if ( isset( $settings[self
::PARAM_MAX2
] ) && isset( $settings[self
::PARAM_MAX
] ) &&
126 $settings[self
::PARAM_MAX2
] < $settings[self
::PARAM_MAX
]
128 $settings[self
::PARAM_MAX2
] = $settings[self
::PARAM_MAX
];
131 return parent
::normalizeSettings( $settings );
134 public function describeSettings( $name, array $settings, array $options ) {
135 $info = parent
::describeSettings( $name, $settings, $options );
137 $min = $settings[self
::PARAM_MIN
] ??
'';
138 $max = $settings[self
::PARAM_MAX
] ??
'';
139 $max2 = $settings[self
::PARAM_MAX2
] ??
'';
140 if ( $max === '' ||
$max2 !== '' && $max2 <= $max ) {
144 if ( empty( $options['compact'] ) ) {
151 if ( $max2 !== '' ) {
152 $info['max2'] = $max2;
159 if ( $max2 !== '' ) {
161 } elseif ( $max !== '' ) {
165 $info[$key] = [ 'min' => $min, 'max' => $max, 'max2' => $max2 ];