MediaWiki  1.34.0
EnumDef.php
Go to the documentation of this file.
1 <?php
2 
4 
8 
28 class EnumDef extends TypeDef {
29 
44  const PARAM_DEPRECATED_VALUES = 'param-deprecated-values';
45 
46  public function validate( $name, $value, array $settings, array $options ) {
47  $values = $this->getEnumValues( $name, $settings, $options );
48 
49  if ( in_array( $value, $values, true ) ) {
50  // Set a warning if a deprecated parameter value has been passed
51  if ( isset( $settings[self::PARAM_DEPRECATED_VALUES][$value] ) ) {
52  $this->callbacks->recordCondition(
53  new ValidationException( $name, $value, $settings, 'deprecated-value', [
54  'flag' => $settings[self::PARAM_DEPRECATED_VALUES][$value],
55  ] ),
56  $options
57  );
58  }
59 
60  return $value;
61  }
62 
63  if ( !isset( $options['values-list'] ) &&
64  count( ParamValidator::explodeMultiValue( $value, 2 ) ) > 1
65  ) {
66  throw new ValidationException( $name, $value, $settings, 'notmulti', [] );
67  } else {
68  throw new ValidationException( $name, $value, $settings, 'badvalue', [] );
69  }
70  }
71 
72  public function getEnumValues( $name, array $settings, array $options ) {
73  return $settings[ParamValidator::PARAM_TYPE];
74  }
75 
76  public function stringifyValue( $name, $value, array $settings, array $options ) {
77  if ( !is_array( $value ) ) {
78  return parent::stringifyValue( $name, $value, $settings, $options );
79  }
80 
81  foreach ( $value as $v ) {
82  if ( strpos( $v, '|' ) !== false ) {
83  return "\x1f" . implode( "\x1f", $value );
84  }
85  }
86  return implode( '|', $value );
87  }
88 
89 }
Wikimedia\ParamValidator\ValidationException
Error reporting for ParamValidator.
Definition: ValidationException.php:15
Wikimedia\ParamValidator\ParamValidator\explodeMultiValue
static explodeMultiValue( $value, $limit)
Split a multi-valued parameter string, like explode()
Definition: ParamValidator.php:508
Wikimedia\ParamValidator\TypeDef\EnumDef
Type definition for enumeration types.
Definition: EnumDef.php:28
Wikimedia\ParamValidator\TypeDef
Base definition for ParamValidator types.
Definition: TypeDef.php:15
Wikimedia\ParamValidator\TypeDef\EnumDef\stringifyValue
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.
Definition: EnumDef.php:76
Wikimedia\ParamValidator\TypeDef\EnumDef\getEnumValues
getEnumValues( $name, array $settings, array $options)
Get the values for enum-like parameters.
Definition: EnumDef.php:72
Wikimedia\ParamValidator\TypeDef\EnumDef\PARAM_DEPRECATED_VALUES
const PARAM_DEPRECATED_VALUES
(array) Associative array of deprecated values.
Definition: EnumDef.php:44
Wikimedia\ParamValidator\TypeDef
Definition: BooleanDef.php:3
Wikimedia\ParamValidator\ParamValidator\PARAM_TYPE
const PARAM_TYPE
(string|array) Type of the parameter.
Definition: ParamValidator.php:67
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:38
Wikimedia\ParamValidator\TypeDef\EnumDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition: EnumDef.php:46