65 $this->allowEmptyWhenRequired = !empty( $options[ self::OPT_ALLOW_EMPTY ] );
69publicfunctionvalidate( $name, $value, array $settings, array $options ) {
76 $this->
failure(
'missingparam', $name, $value, $settings, $options );
81 $len = strlen( $value );
82if ( isset( $settings[self::PARAM_MAX_BYTES] ) && $len > $settings[self::PARAM_MAX_BYTES] ) {
85'maxbytes' => $settings[self::PARAM_MAX_BYTES] ??
null,
86'maxchars' => $settings[self::PARAM_MAX_CHARS] ??
null,
87 ] )->numParams( $settings[self::PARAM_MAX_BYTES], $len ),
88 $name, $value, $settings, $options
91 $len = mb_strlen( $value,
'UTF-8' );
92if ( isset( $settings[self::PARAM_MAX_CHARS] ) && $len > $settings[self::PARAM_MAX_CHARS] ) {
95'maxbytes' => $settings[self::PARAM_MAX_BYTES] ??
null,
96'maxchars' => $settings[self::PARAM_MAX_CHARS] ??
null,
97 ] )->numParams( $settings[self::PARAM_MAX_CHARS], $len ),
98 $name, $value, $settings, $options
106publicfunctioncheckSettings(
string $name, $settings, array $options, array $ret ): array {
107 $ret = parent::
checkSettings( $name, $settings, $options, $ret );
113if ( !is_int( $maxb ) ) {
116 } elseif ( $maxb < 0 ) {
121if ( !is_int( $maxc ) ) {
124 } elseif ( $maxc < 0 ) {
130 $ret[
'issues'][] =
'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and ' 131 .
'PARAM_MAX_BYTES is 0. That\'s impossible to satisfy.';
134 $ret[
'issues'][] =
'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and ' 135 .
'PARAM_MAX_CHARS is 0. That\'s impossible to satisfy.';
144 $info = parent::getParamInfo( $name, $settings, $options );
146 $info[
'maxbytes'] = $settings[self::PARAM_MAX_BYTES] ??
null;
147 $info[
'maxchars'] = $settings[self::PARAM_MAX_CHARS] ??
null;
153publicfunctiongetHelpInfo( $name, array $settings, array $options ) {
154 $info = parent::getHelpInfo( $name, $settings, $options );
156if ( isset( $settings[self::PARAM_MAX_BYTES] ) ) {
157 $info[self::PARAM_MAX_BYTES] = MessageValue::new(
'paramvalidator-help-type-string-maxbytes' )
158 ->numParams( $settings[self::PARAM_MAX_BYTES] );
160if ( isset( $settings[self::PARAM_MAX_CHARS] ) ) {
161 $info[self::PARAM_MAX_CHARS] = MessageValue::new(
'paramvalidator-help-type-string-maxchars' )
162 ->numParams( $settings[self::PARAM_MAX_CHARS] );
Value object representing a message for i18n.
Service for formatting and validating API parameters.
const PARAM_REQUIRED
(bool) Indicate that the parameter is required.
Type definition for string types.
__construct(Callbacks $callbacks, array $options=[])
bool $allowEmptyWhenRequired
validate( $name, $value, array $settings, array $options)
Validate the value.When ParamValidator is processing a multi-valued parameter, this will be called on...
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.Keys should be short strings using lowercase...
checkSettings(string $name, $settings, array $options, array $ret)
Validate a parameter settings array.This is intended for validation of parameter settings during unit...
const PARAM_MAX_BYTES
(integer) Maximum length of a string in bytes.
const OPT_ALLOW_EMPTY
When this option is set, the empty string is considered a proper value.
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.Keys in the returned array should generally corr...
const PARAM_MAX_CHARS
(integer) Maximum length of a string in characters (Unicode codepoints).
Base definition for ParamValidator types.
failIfNotString(string $name, $value, array $settings, array $options)
Fails if $value is not a string.
failureMessage( $code, ?array $data=null, $suffix=null)
Create a DataMessageValue representing a failure.
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Interface defining callbacks needed by ParamValidator.