| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\ParamValidator\TypeDef; |
| 4 |
|
| 5 |
use Wikimedia\Message\MessageValue; |
| 6 |
use Wikimedia\ParamValidator\Callbacks; |
| 7 |
use Wikimedia\ParamValidator\ParamValidator; |
| 8 |
use Wikimedia\ParamValidator\TypeDef; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class StringDef extends TypeDef { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public const OPT_ALLOW_EMPTY = 'allowEmptyWhenRequired'; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public const PARAM_MAX_BYTES = 'param-max-bytes'; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public const PARAM_MAX_CHARS = 'param-max-chars'; |
| 52 |
|
| 53 |
|
| 54 |
protected $allowEmptyWhenRequired = false; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function __construct( Callbacks $callbacks, array $options = [] ) { |
| 63 |
parent::__construct( $callbacks ); |
| 64 |
|
| 65 |
$this->allowEmptyWhenRequired = !empty( $options[ self::OPT_ALLOW_EMPTY ] ); |
| 66 |
} |
| 67 |
|
| 68 |
public function validate( $name, $value, array $settings, array $options ) { |
| 69 |
$allowEmptyWhenRequired = $options[ self::OPT_ALLOW_EMPTY ] |
| 70 |
?? $this->allowEmptyWhenRequired; |
| 71 |
|
| 72 |
if ( !$allowEmptyWhenRequired && $value === '' && |
| 73 |
!empty( $settings[ParamValidator::PARAM_REQUIRED] ) |
| 74 |
) { |
| 75 |
$this->failure( 'missingparam', $name, $value, $settings, $options ); |
| 76 |
} |
| 77 |
|
| 78 |
$this->failIfNotString( $name, $value, $settings, $options ); |
| 79 |
|
| 80 |
$len = strlen( $value ); |
| 81 |
if ( isset( $settings[self::PARAM_MAX_BYTES] ) && $len > $settings[self::PARAM_MAX_BYTES] ) { |
| 82 |
$this->failure( |
| 83 |
$this->failureMessage( 'maxbytes', [ |
| 84 |
'maxbytes' => $settings[self::PARAM_MAX_BYTES] ?? null, |
| 85 |
'maxchars' => $settings[self::PARAM_MAX_CHARS] ?? null, |
| 86 |
] )->numParams( $settings[self::PARAM_MAX_BYTES], $len ), |
| 87 |
$name, $value, $settings, $options |
| 88 |
); |
| 89 |
} |
| 90 |
$len = mb_strlen( $value, 'UTF-8' ); |
| 91 |
if ( isset( $settings[self::PARAM_MAX_CHARS] ) && $len > $settings[self::PARAM_MAX_CHARS] ) { |
| 92 |
$this->failure( |
| 93 |
$this->failureMessage( 'maxchars', [ |
| 94 |
'maxbytes' => $settings[self::PARAM_MAX_BYTES] ?? null, |
| 95 |
'maxchars' => $settings[self::PARAM_MAX_CHARS] ?? null, |
| 96 |
] )->numParams( $settings[self::PARAM_MAX_CHARS], $len ), |
| 97 |
$name, $value, $settings, $options |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
return $value; |
| 102 |
} |
| 103 |
|
| 104 |
public function checkSettings( string $name, $settings, array $options, array $ret ): array { |
| 105 |
$ret = parent::checkSettings( $name, $settings, $options, $ret ); |
| 106 |
|
| 107 |
$ret['allowedKeys'] = array_merge( $ret['allowedKeys'], [ |
| 108 |
self::PARAM_MAX_BYTES, self::PARAM_MAX_CHARS, |
| 109 |
] ); |
| 110 |
|
| 111 |
$maxb = $settings[self::PARAM_MAX_BYTES] ?? PHP_INT_MAX; |
| 112 |
if ( !is_int( $maxb ) ) { |
| 113 |
$ret['issues'][self::PARAM_MAX_BYTES] = 'PARAM_MAX_BYTES must be an integer, got ' |
| 114 |
. gettype( $maxb ); |
| 115 |
} elseif ( $maxb < 0 ) { |
| 116 |
$ret['issues'][self::PARAM_MAX_BYTES] = 'PARAM_MAX_BYTES must be greater than or equal to 0'; |
| 117 |
} |
| 118 |
|
| 119 |
$maxc = $settings[self::PARAM_MAX_CHARS] ?? PHP_INT_MAX; |
| 120 |
if ( !is_int( $maxc ) ) { |
| 121 |
$ret['issues'][self::PARAM_MAX_CHARS] = 'PARAM_MAX_CHARS must be an integer, got ' |
| 122 |
. gettype( $maxc ); |
| 123 |
} elseif ( $maxc < 0 ) { |
| 124 |
$ret['issues'][self::PARAM_MAX_CHARS] = 'PARAM_MAX_CHARS must be greater than or equal to 0'; |
| 125 |
} |
| 126 |
|
| 127 |
if ( !$this->allowEmptyWhenRequired && !empty( $settings[ParamValidator::PARAM_REQUIRED] ) ) { |
| 128 |
if ( $maxb === 0 ) { |
| 129 |
$ret['issues'][] = 'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and ' |
| 130 |
. 'PARAM_MAX_BYTES is 0. That\'s impossible to satisfy.'; |
| 131 |
} |
| 132 |
if ( $maxc === 0 ) { |
| 133 |
$ret['issues'][] = 'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and ' |
| 134 |
. 'PARAM_MAX_CHARS is 0. That\'s impossible to satisfy.'; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return $ret; |
| 139 |
} |
| 140 |
|
| 141 |
public function getParamInfo( $name, array $settings, array $options ) { |
| 142 |
$info = parent::getParamInfo( $name, $settings, $options ); |
| 143 |
|
| 144 |
$info['maxbytes'] = $settings[self::PARAM_MAX_BYTES] ?? null; |
| 145 |
$info['maxchars'] = $settings[self::PARAM_MAX_CHARS] ?? null; |
| 146 |
|
| 147 |
return $info; |
| 148 |
} |
| 149 |
|
| 150 |
public function getHelpInfo( $name, array $settings, array $options ) { |
| 151 |
$info = parent::getHelpInfo( $name, $settings, $options ); |
| 152 |
|
| 153 |
if ( isset( $settings[self::PARAM_MAX_BYTES] ) ) { |
| 154 |
$info[self::PARAM_MAX_BYTES] = MessageValue::new( 'paramvalidator-help-type-string-maxbytes' ) |
| 155 |
->numParams( $settings[self::PARAM_MAX_BYTES] ); |
| 156 |
} |
| 157 |
if ( isset( $settings[self::PARAM_MAX_CHARS] ) ) { |
| 158 |
$info[self::PARAM_MAX_CHARS] = MessageValue::new( 'paramvalidator-help-type-string-maxchars' ) |
| 159 |
->numParams( $settings[self::PARAM_MAX_CHARS] ); |
| 160 |
} |
| 161 |
|
| 162 |
return $info; |
| 163 |
} |
| 164 |
|
| 165 |
} |