Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / libs / ParamValidator / TypeDef.php
blob7a7b4e24a991c399294faa25e2536f52b3b0dd8a
1 <?php
3 namespace Wikimedia\ParamValidator;
5 use Wikimedia\Message\DataMessageValue;
6 use Wikimedia\Message\MessageValue;
8 /**
9 * Base definition for ParamValidator types.
11 * Most methods in this class accept an "options array". This is just the `$options`
12 * passed to ParamValidator::getValue(), ParamValidator::validateValue(), and the like
13 * and is intended for communication of non-global state to the Callbacks.
15 * @since 1.34
16 * @unstable for use in extensions. Intended to become stable to extend, at
17 * least for use in MediaWiki, which already defines some subclasses.
19 abstract class TypeDef {
21 /**
22 * @unstable Temporarily log warnings to detect misbehaving clients (T305973)
24 public const OPT_LOG_BAD_TYPES = 'log-bad-types';
26 /**
27 * Option that instructs TypeDefs to enforce the native type of parameter
28 * values, instead of allowing string values as input. This is intended for
29 * use with values coming from a JSON request body, and may accommodate for
30 * differences between the type system of PHP and JSON.
32 public const OPT_ENFORCE_JSON_TYPES = 'enforce-json-types';
34 /** @var Callbacks */
35 protected $callbacks;
37 /**
38 * @stable to call
40 * @param Callbacks $callbacks
42 public function __construct( Callbacks $callbacks ) {
43 $this->callbacks = $callbacks;
46 /**
47 * Whether the value may be an array.
48 * Note that this is different from multi-value.
49 * This should only return true if each value can be an array.
50 * @since 1.41
51 * @stable to override
52 * @return bool
54 public function supportsArrays() {
55 return false;
58 /**
59 * Fails if $value is not a string.
61 * @param string $name Parameter name being validated.
62 * @param mixed $value Value being validated.
63 * @param array $settings Parameter settings array.
64 * @param array $options Options array.
66 * @return void
68 protected function failIfNotString(
69 string $name,
70 $value,
71 array $settings,
72 array $options
73 ): void {
74 if ( !is_string( $value ) ) {
75 $this->fatal(
76 $this->failureMessage( 'needstring' )
77 ->params( gettype( $value ) ),
78 $name, $value, $settings, $options
83 /**
84 * Throw a ValidationException.
85 * This is a wrapper for failure() which explicitly declares that it
86 * never returns, which is useful to static analysis tools like Phan.
88 * Note that parameters for `$name` and `$value` are always added as `$1`
89 * and `$2`.
91 * @param DataMessageValue|string $failure Failure code or message.
92 * @param string $name Parameter name being validated.
93 * @param mixed $value Value being validated.
94 * @param array $settings Parameter settings array.
95 * @param array $options Options array.
96 * @return never
97 * @throws ValidationException always
99 protected function fatal(
100 $failure, $name, $value, array $settings, array $options
102 $this->failure( $failure, $name, $value, $settings, $options );
106 * Record a failure message
108 * Depending on `$fatal`, this will either throw a ValidationException or
109 * call $this->callbacks->recordCondition().
111 * Note that parameters for `$name` and `$value` are always added as `$1`
112 * and `$2`.
114 * @param DataMessageValue|string $failure Failure code or message.
115 * @param string $name Parameter name being validated.
116 * @param mixed $value Value being validated.
117 * @param array $settings Parameter settings array.
118 * @param array $options Options array.
119 * @param bool $fatal Whether the failure is fatal
121 protected function failure(
122 $failure, $name, $value, array $settings, array $options, $fatal = true
124 if ( !is_string( $value ) ) {
125 $value = (string)$this->stringifyValue( $name, $value, $settings, $options );
128 if ( is_string( $failure ) ) {
129 $mv = $this->failureMessage( $failure )
130 ->plaintextParams( $name, $value );
131 } else {
132 $mv = DataMessageValue::new( $failure->getKey(), [], $failure->getCode(), $failure->getData() )
133 ->plaintextParams( $name, $value )
134 ->params( ...$failure->getParams() );
137 if ( $fatal ) {
138 throw new ValidationException( $mv, $name, $value, $settings );
140 $this->callbacks->recordCondition( $mv, $name, $value, $settings, $options );
144 * Create a DataMessageValue representing a failure
146 * The message key will be "paramvalidator-$code" or "paramvalidator-$code-$suffix".
148 * Use DataMessageValue's param mutators to add additional MessageParams.
149 * Note that `failure()` will prepend parameters for `$name` and `$value`.
151 * @param string $code Failure code.
152 * @param array|null $data Failure data.
153 * @param string|null $suffix Suffix to append when producing the message key
154 * @return DataMessageValue
156 protected function failureMessage( $code, ?array $data = null, $suffix = null ): DataMessageValue {
157 return DataMessageValue::new(
158 "paramvalidator-$code" . ( $suffix !== null ? "-$suffix" : '' ),
159 [], $code, $data
164 * Get the value from the request
165 * @stable to override
167 * @note Only override this if you need to use something other than
168 * $this->callbacks->getValue() to fetch the value. Reformatting from a
169 * string should typically be done by self::validate().
170 * @note Handling of ParamValidator::PARAM_DEFAULT should be left to ParamValidator,
171 * as should PARAM_REQUIRED and the like.
173 * @param string $name Parameter name being fetched.
174 * @param array $settings Parameter settings array.
175 * @param array $options Options array.
176 * @return null|mixed Return null if the value wasn't present, otherwise a
177 * value to be passed to self::validate().
179 public function getValue( $name, array $settings, array $options ) {
180 return $this->callbacks->getValue( $name, null, $options );
184 * Validate the value
186 * When ParamValidator is processing a multi-valued parameter, this will be
187 * called once for each of the supplied values. Which may mean zero calls.
189 * When getValue() returned null, this will not be called.
191 * @param string $name Parameter name being validated.
192 * @param mixed $value Value to validate, from getValue().
193 * @param array $settings Parameter settings array.
194 * @param array $options Options array. Note the following values that may be set
195 * by ParamValidator:
196 * - is-default: (bool) If present and true, the value was taken from PARAM_DEFAULT rather
197 * that being supplied by the client.
198 * - values-list: (string[]) If defined, values of a multi-valued parameter are being processed
199 * (and this array holds the full set of values).
200 * @return mixed Validated value
201 * @throws ValidationException if the value is invalid
203 abstract public function validate( $name, $value, array $settings, array $options );
206 * Normalize a settings array
207 * @stable to override
208 * @param array $settings
209 * @return array
211 public function normalizeSettings( array $settings ) {
212 return $settings;
216 * Validate a parameter settings array
218 * This is intended for validation of parameter settings during unit or
219 * integration testing, and should implement strict checks.
221 * The rest of the code should generally be more permissive.
223 * @see ParamValidator::checkSettings()
224 * @stable to override
226 * @param string $name Parameter name
227 * @param array|mixed $settings Default value or an array of settings
228 * using PARAM_* constants.
229 * @param array $options Options array, passed through to the TypeDef and Callbacks.
230 * @param array $ret
231 * - 'issues': (string[]) Errors detected in $settings, as English text. If the settings
232 * are valid, this will be the empty array. Keys on input are ParamValidator constants,
233 * allowing the typedef to easily override core validation; this need not be preserved
234 * when returned.
235 * - 'allowedKeys': (string[]) ParamValidator keys that are allowed in `$settings`.
236 * - 'messages': (MessageValue[]) Messages to be checked for existence.
237 * @return array $ret, with any relevant changes.
239 public function checkSettings( string $name, $settings, array $options, array $ret ): array {
240 return $ret;
244 * Get the values for enum-like parameters
246 * This is primarily intended for documentation and implementation of
247 * PARAM_ALL; it is the responsibility of the TypeDef to ensure that validate()
248 * accepts the values returned here.
249 * @stable to override
251 * @param string $name Parameter name being validated.
252 * @param array $settings Parameter settings array.
253 * @param array $options Options array.
254 * @return array|null All possible enumerated values, or null if this is
255 * not an enumeration.
257 public function getEnumValues( $name, array $settings, array $options ) {
258 return null;
262 * Convert a value to a string representation.
264 * This is intended as the inverse of getValue() and validate(): this
265 * should accept anything returned by those methods or expected to be used
266 * as PARAM_DEFAULT, and if the string from this method is passed in as client
267 * input or PARAM_DEFAULT it should give equivalent output from validate().
269 * @param string $name Parameter name being converted.
270 * @param mixed $value Parameter value being converted. Do not pass null.
271 * @param array $settings Parameter settings array.
272 * @param array $options Options array.
273 * @return string|null Return null if there is no representation of $value
274 * reasonably satisfying the description given.
276 public function stringifyValue( $name, $value, array $settings, array $options ) {
277 if ( is_array( $value ) ) {
278 return '(array)';
281 return (string)$value;
285 * Describe parameter settings in a machine-readable format.
287 * Keys should be short strings using lowercase ASCII letters. Values
288 * should generally be values that could be encoded in JSON or the like.
290 * This is intended to handle PARAM constants specific to this class. It
291 * generally shouldn't handle constants defined on ParamValidator itself.
292 * @stable to override
294 * @param string $name Parameter name.
295 * @param array $settings Parameter settings array.
296 * @param array $options Options array.
297 * @return array
299 public function getParamInfo( $name, array $settings, array $options ) {
300 return [];
304 * Describe parameter settings in human-readable format
306 * Keys in the returned array should generally correspond to PARAM
307 * constants.
309 * If relevant, a MessageValue describing the type itself should be
310 * returned with key ParamValidator::PARAM_TYPE.
312 * The default messages for other ParamValidator-defined PARAM constants
313 * may be suppressed by returning null as the value for those constants, or
314 * replaced by returning a replacement MessageValue. Normally, however,
315 * the default messages should not be changed.
317 * MessageValues describing any other constraints applied via PARAM
318 * constants specific to this class should also be returned.
319 * @stable to override
321 * @param string $name Parameter name being described.
322 * @param array $settings Parameter settings array.
323 * @param array $options Options array.
324 * @return (MessageValue|null)[]
326 public function getHelpInfo( $name, array $settings, array $options ) {
327 return [];