ParamValidator: Flag as unstable for 1.34
[lhc/web/wiklou.git] / includes / libs / ParamValidator / TypeDef / LimitDef.php
1 <?php
2
3 namespace Wikimedia\ParamValidator\TypeDef;
4
5 /**
6 * Type definition for "limit" types
7 *
8 * A limit type is an integer type that also accepts the magic value "max".
9 * IntegerDef::PARAM_MIN defaults to 0 for this type.
10 *
11 * @see IntegerDef
12 * @since 1.34
13 * @unstable
14 */
15 class LimitDef extends IntegerDef {
16
17 /**
18 * @inheritDoc
19 *
20 * Additional `$options` accepted:
21 * - 'parse-limit': (bool) Default true, set false to return 'max' rather
22 * than determining the effective value.
23 */
24 public function validate( $name, $value, array $settings, array $options ) {
25 if ( $value === 'max' ) {
26 if ( !isset( $options['parse-limit'] ) || $options['parse-limit'] ) {
27 $value = $this->callbacks->useHighLimits( $options )
28 ? $settings[self::PARAM_MAX2] ?? $settings[self::PARAM_MAX] ?? PHP_INT_MAX
29 : $settings[self::PARAM_MAX] ?? PHP_INT_MAX;
30 }
31 return $value;
32 }
33
34 return parent::validate( $name, $value, $settings, $options );
35 }
36
37 public function normalizeSettings( array $settings ) {
38 $settings += [
39 self::PARAM_MIN => 0,
40 ];
41
42 return parent::normalizeSettings( $settings );
43 }
44
45 }