MediaWiki  1.34.0
IntegerDef.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
26 class IntegerDef extends TypeDef {
27 
34  const PARAM_IGNORE_RANGE = 'param-ignore-range';
35 
45  const PARAM_MIN = 'param-min';
46 
56  const PARAM_MAX = 'param-max';
57 
70  const PARAM_MAX2 = 'param-max2';
71 
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', [] );
75  }
76  $ret = intval( $value, 10 );
77 
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', [] );
83  }
84  }
85 
86  $min = $settings[self::PARAM_MIN] ?? null;
87  $max = $settings[self::PARAM_MAX] ?? null;
88  $max2 = $settings[self::PARAM_MAX2] ?? null;
89  $err = null;
90 
91  if ( $min !== null && $ret < $min ) {
92  $err = 'belowminimum';
93  $ret = $min;
94  } elseif ( $max !== null && $ret > $max ) {
95  if ( $max2 !== null && $this->callbacks->useHighLimits( $options ) ) {
96  if ( $ret > $max2 ) {
97  $err = 'abovehighmaximum';
98  $ret = $max2;
99  }
100  } else {
101  $err = 'abovemaximum';
102  $ret = $max;
103  }
104  }
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,
110  ] );
111  if ( empty( $settings[self::PARAM_IGNORE_RANGE] ) ) {
112  throw $ex;
113  }
114  $this->callbacks->recordCondition( $ex, $options );
115  }
116 
117  return $ret;
118  }
119 
120  public function normalizeSettings( array $settings ) {
121  if ( !isset( $settings[self::PARAM_MAX] ) ) {
122  unset( $settings[self::PARAM_MAX2] );
123  }
124 
125  if ( isset( $settings[self::PARAM_MAX2] ) && isset( $settings[self::PARAM_MAX] ) &&
126  $settings[self::PARAM_MAX2] < $settings[self::PARAM_MAX]
127  ) {
128  $settings[self::PARAM_MAX2] = $settings[self::PARAM_MAX];
129  }
130 
131  return parent::normalizeSettings( $settings );
132  }
133 
134  public function describeSettings( $name, array $settings, array $options ) {
135  $info = parent::describeSettings( $name, $settings, $options );
136 
137  $min = $settings[self::PARAM_MIN] ?? '';
138  $max = $settings[self::PARAM_MAX] ?? '';
139  $max2 = $settings[self::PARAM_MAX2] ?? '';
140  if ( $max === '' || $max2 !== '' && $max2 <= $max ) {
141  $max2 = '';
142  }
143 
144  if ( empty( $options['compact'] ) ) {
145  if ( $min !== '' ) {
146  $info['min'] = $min;
147  }
148  if ( $max !== '' ) {
149  $info['max'] = $max;
150  }
151  if ( $max2 !== '' ) {
152  $info['max2'] = $max2;
153  }
154  } else {
155  $key = '';
156  if ( $min !== '' ) {
157  $key = 'min';
158  }
159  if ( $max2 !== '' ) {
160  $key .= 'max2';
161  } elseif ( $max !== '' ) {
162  $key .= 'max';
163  }
164  if ( $key !== '' ) {
165  $info[$key] = [ 'min' => $min, 'max' => $max, 'max2' => $max2 ];
166  }
167  }
168 
169  return $info;
170  }
171 
172 }
Wikimedia\ParamValidator\TypeDef\IntegerDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition: IntegerDef.php:72
Wikimedia\ParamValidator\ValidationException
Error reporting for ParamValidator.
Definition: ValidationException.php:15
Wikimedia\ParamValidator\TypeDef\IntegerDef\describeSettings
describeSettings( $name, array $settings, array $options)
"Describe" a settings array
Definition: IntegerDef.php:134
Wikimedia\ParamValidator\TypeDef\IntegerDef\normalizeSettings
normalizeSettings(array $settings)
Normalize a settings array.
Definition: IntegerDef.php:120
Wikimedia\ParamValidator\TypeDef\IntegerDef\PARAM_IGNORE_RANGE
const PARAM_IGNORE_RANGE
(bool) Whether to enforce the specified range.
Definition: IntegerDef.php:34
Wikimedia\ParamValidator\TypeDef\IntegerDef\PARAM_MAX2
const PARAM_MAX2
(int) Maximum allowed value (high limits)
Definition: IntegerDef.php:70
Wikimedia\ParamValidator\TypeDef
Base definition for ParamValidator types.
Definition: TypeDef.php:15
Wikimedia\ParamValidator\TypeDef\IntegerDef\PARAM_MAX
const PARAM_MAX
(int) Maximum allowed value (normal limits)
Definition: IntegerDef.php:56
Wikimedia\ParamValidator\TypeDef\IntegerDef
Type definition for integer types.
Definition: IntegerDef.php:26
Wikimedia\ParamValidator\TypeDef
Definition: BooleanDef.php:3
Wikimedia\ParamValidator\TypeDef\IntegerDef\PARAM_MIN
const PARAM_MIN
(int) Minimum allowed value.
Definition: IntegerDef.php:45