Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / libs / ParamValidator / TypeDef / EnumDef.php
1 <?php
2
3 namespace Wikimedia\ParamValidator\TypeDef;
4
5 use Wikimedia\ParamValidator\ParamValidator;
6 use Wikimedia\ParamValidator\TypeDef;
7 use Wikimedia\ParamValidator\ValidationException;
8
9 /**
10 * Type definition for enumeration types.
11 *
12 * This class expects that PARAM_TYPE is an array of allowed values. Subclasses
13 * may override getEnumValues() to determine the allowed values differently.
14 *
15 * The result from validate() is one of the defined values.
16 *
17 * ValidationException codes:
18 * - 'badvalue': The value is not a recognized value. No data.
19 * - 'notmulti': PARAM_ISMULTI is not set and the unrecognized value seems to
20 * be an attempt at using multiple values. No data.
21 *
22 * Additional codes may be generated when using certain PARAM constants. See
23 * the constants' documentation for details.
24 *
25 * @since 1.34
26 */
27 class EnumDef extends TypeDef {
28
29 /**
30 * (array) Associative array of deprecated values.
31 *
32 * Keys are the deprecated parameter values, values are included in
33 * the ValidationException. If value is null, the parameter is considered
34 * not actually deprecated.
35 *
36 * Note that this does not add any values to the enumeration, it only
37 * documents existing values as being deprecated.
38 *
39 * ValidationException codes: (non-fatal)
40 * - 'deprecated-value': A deprecated value was encountered. Data:
41 * - 'flag': The value from the associative array.
42 */
43 const PARAM_DEPRECATED_VALUES = 'param-deprecated-values';
44
45 public function validate( $name, $value, array $settings, array $options ) {
46 $values = $this->getEnumValues( $name, $settings, $options );
47
48 if ( in_array( $value, $values, true ) ) {
49 // Set a warning if a deprecated parameter value has been passed
50 if ( isset( $settings[self::PARAM_DEPRECATED_VALUES][$value] ) ) {
51 $this->callbacks->recordCondition(
52 new ValidationException( $name, $value, $settings, 'deprecated-value', [
53 'flag' => $settings[self::PARAM_DEPRECATED_VALUES][$value],
54 ] ),
55 $options
56 );
57 }
58
59 return $value;
60 }
61
62 if ( !isset( $options['values-list'] ) &&
63 count( ParamValidator::explodeMultiValue( $value, 2 ) ) > 1
64 ) {
65 throw new ValidationException( $name, $value, $settings, 'notmulti', [] );
66 } else {
67 throw new ValidationException( $name, $value, $settings, 'badvalue', [] );
68 }
69 }
70
71 public function getEnumValues( $name, array $settings, array $options ) {
72 return $settings[ParamValidator::PARAM_TYPE];
73 }
74
75 public function stringifyValue( $name, $value, array $settings, array $options ) {
76 if ( !is_array( $value ) ) {
77 return parent::stringifyValue( $name, $value, $settings, $options );
78 }
79
80 foreach ( $value as $v ) {
81 if ( strpos( $v, '|' ) !== false ) {
82 return "\x1f" . implode( "\x1f", $value );
83 }
84 }
85 return implode( '|', $value );
86 }
87
88 }