MediaWiki master
StringDef.php
Go to the documentation of this file.
1<?php
2
4
9
24class StringDef extends TypeDef {
25
29 public const OPT_ALLOW_EMPTY = 'allowEmptyWhenRequired';
30
39 public const PARAM_MAX_BYTES = 'param-max-bytes';
40
51 public const PARAM_MAX_CHARS = 'param-max-chars';
52
54 protected $allowEmptyWhenRequired = false;
55
62 public function __construct( Callbacks $callbacks, array $options = [] ) {
63 parent::__construct( $callbacks );
64
65 $this->allowEmptyWhenRequired = !empty( $options[ self::OPT_ALLOW_EMPTY ] );
66 }
67
69 public function validate( $name, $value, array $settings, array $options ) {
72
73 if ( !$allowEmptyWhenRequired && $value === '' &&
74 !empty( $settings[ParamValidator::PARAM_REQUIRED] )
75 ) {
76 $this->failure( 'missingparam', $name, $value, $settings, $options );
77 }
78
79 $this->failIfNotString( $name, $value, $settings, $options );
80
81 $len = strlen( $value );
82 if ( isset( $settings[self::PARAM_MAX_BYTES] ) && $len > $settings[self::PARAM_MAX_BYTES] ) {
83 $this->failure(
84 $this->failureMessage( 'maxbytes', [
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
89 );
90 }
91 $len = mb_strlen( $value, 'UTF-8' );
92 if ( isset( $settings[self::PARAM_MAX_CHARS] ) && $len > $settings[self::PARAM_MAX_CHARS] ) {
93 $this->failure(
94 $this->failureMessage( 'maxchars', [
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
99 );
100 }
101
102 return $value;
103 }
104
106 public function checkSettings( string $name, $settings, array $options, array $ret ): array {
107 $ret = parent::checkSettings( $name, $settings, $options, $ret );
108
109 $ret['allowedKeys'][] = self::PARAM_MAX_BYTES;
110 $ret['allowedKeys'][] = self::PARAM_MAX_CHARS;
111
112 $maxb = $settings[self::PARAM_MAX_BYTES] ?? PHP_INT_MAX;
113 if ( !is_int( $maxb ) ) {
114 $ret['issues'][self::PARAM_MAX_BYTES] = 'PARAM_MAX_BYTES must be an integer, got '
115 . gettype( $maxb );
116 } elseif ( $maxb < 0 ) {
117 $ret['issues'][self::PARAM_MAX_BYTES] = 'PARAM_MAX_BYTES must be greater than or equal to 0';
118 }
119
120 $maxc = $settings[self::PARAM_MAX_CHARS] ?? PHP_INT_MAX;
121 if ( !is_int( $maxc ) ) {
122 $ret['issues'][self::PARAM_MAX_CHARS] = 'PARAM_MAX_CHARS must be an integer, got '
123 . gettype( $maxc );
124 } elseif ( $maxc < 0 ) {
125 $ret['issues'][self::PARAM_MAX_CHARS] = 'PARAM_MAX_CHARS must be greater than or equal to 0';
126 }
127
128 if ( !$this->allowEmptyWhenRequired && !empty( $settings[ParamValidator::PARAM_REQUIRED] ) ) {
129 if ( $maxb === 0 ) {
130 $ret['issues'][] = 'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and '
131 . 'PARAM_MAX_BYTES is 0. That\'s impossible to satisfy.';
132 }
133 if ( $maxc === 0 ) {
134 $ret['issues'][] = 'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and '
135 . 'PARAM_MAX_CHARS is 0. That\'s impossible to satisfy.';
136 }
137 }
138
139 return $ret;
140 }
141
143 public function getParamInfo( $name, array $settings, array $options ) {
144 $info = parent::getParamInfo( $name, $settings, $options );
145
146 $info['maxbytes'] = $settings[self::PARAM_MAX_BYTES] ?? null;
147 $info['maxchars'] = $settings[self::PARAM_MAX_CHARS] ?? null;
148
149 return $info;
150 }
151
153 public function getHelpInfo( $name, array $settings, array $options ) {
154 $info = parent::getHelpInfo( $name, $settings, $options );
155
156 if ( 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] );
159 }
160 if ( 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] );
163 }
164
165 return $info;
166 }
167
168}
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.
Definition StringDef.php:24
__construct(Callbacks $callbacks, array $options=[])
Definition StringDef.php:62
validate( $name, $value, array $settings, array $options)
Validate the value.When ParamValidator is processing a multi-valued parameter, this will be called on...
Definition StringDef.php:69
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.
Definition StringDef.php:39
const OPT_ALLOW_EMPTY
When this option is set, the empty string is considered a proper value.
Definition StringDef.php:29
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).
Definition StringDef.php:51
Base definition for ParamValidator types.
Definition TypeDef.php:19
failIfNotString(string $name, $value, array $settings, array $options)
Fails if $value is not a string.
Definition TypeDef.php:68
failureMessage( $code, ?array $data=null, $suffix=null)
Create a DataMessageValue representing a failure.
Definition TypeDef.php:156
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition TypeDef.php:121
Interface defining callbacks needed by ParamValidator.
Definition Callbacks.php:21