MediaWiki REL1_34
EnumDef.php
Go to the documentation of this file.
1<?php
2
4
8
28class 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}
Service for formatting and validating API parameters.
const PARAM_TYPE
(string|array) Type of the parameter.
static explodeMultiValue( $value, $limit)
Split a multi-valued parameter string, like explode()
Type definition for enumeration types.
Definition EnumDef.php:28
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition EnumDef.php:46
getEnumValues( $name, array $settings, array $options)
Get the values for enum-like parameters.
Definition EnumDef.php:72
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.
Definition EnumDef.php:76
const PARAM_DEPRECATED_VALUES
(array) Associative array of deprecated values.
Definition EnumDef.php:44
Base definition for ParamValidator types.
Definition TypeDef.php:15