Debian Codegraph @

includes/libs/ParamValidator/TypeDef/LimitDef.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\ParamValidator\TypeDef;
4
5 use Wikimedia\Message\MessageValue;
6 use Wikimedia\ParamValidator\ParamValidator;
7
8 /**
9 * Type definition for "limit" types
10 *
11 * A limit type is an integer type that also accepts the magic value "max".
12 * IntegerDef::PARAM_MIN defaults to 0 for this type.
13 *
14 * @see IntegerDef
15 * @since 1.34
16 * @unstable
17 */
18 class LimitDef extends IntegerDef {
19
20 /**
21 * @inheritDoc
22 *
23 * Additional `$options` accepted:
24 * - 'parse-limit': (bool) Default true, set false to return 'max' rather
25 * than determining the effective value.
26 */
27 public function validate( $name, $value, array $settings, array $options ) {
28 if ( $value === 'max' ) {
29 if ( $options['parse-limit'] ?? true ) {
30 $value = $this->callbacks->useHighLimits( $options )
31 ? $settings[self::PARAM_MAX2] ?? $settings[self::PARAM_MAX] ?? PHP_INT_MAX
32 : $settings[self::PARAM_MAX] ?? PHP_INT_MAX;
33 }
34 return $value;
35 }
36
37 return parent::validate( $name, $value, $settings, $options );
38 }
39
40 public function normalizeSettings( array $settings ) {
41 $settings += [
42 self::PARAM_MIN => 0,
43 ];
44
45 // Cannot be multi-valued
46 $settings[ParamValidator::PARAM_ISMULTI] = false;
47
48 return parent::normalizeSettings( $settings );
49 }
50
51 public function checkSettings( string $name, $settings, array $options, array $ret ): array {
52 $ret = parent::checkSettings( $name, $settings, $options, $ret );
53
54 if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) &&
55 !isset( $ret['issues'][ParamValidator::PARAM_ISMULTI] )
56 ) {
57 $ret['issues'][ParamValidator::PARAM_ISMULTI] =
58 'PARAM_ISMULTI cannot be used for limit-type parameters';
59 }
60
61 if ( ( $settings[self::PARAM_MIN] ?? 0 ) < 0 ) {
62 $ret['issues'][] = 'PARAM_MIN must be greater than or equal to 0';
63 }
64 if ( !isset( $settings[self::PARAM_MAX] ) ) {
65 $ret['issues'][] = 'PARAM_MAX must be set';
66 }
67
68 return $ret;
69 }
70
71 public function getHelpInfo( $name, array $settings, array $options ) {
72 $info = parent::getHelpInfo( $name, $settings, $options );
73
74 $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-limit' )
75 ->params( 1 );
76
77 return $info;
78 }
79
80 }