3 namespace Wikimedia\ParamValidator\TypeDef
;
5 use Wikimedia\ParamValidator\ParamValidator
;
6 use Wikimedia\ParamValidator\TypeDef
;
7 use Wikimedia\ParamValidator\ValidationException
;
10 * Type definition for enumeration types.
12 * This class expects that PARAM_TYPE is an array of allowed values. Subclasses
13 * may override getEnumValues() to determine the allowed values differently.
15 * The result from validate() is one of the defined values.
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.
22 * Additional codes may be generated when using certain PARAM constants. See
23 * the constants' documentation for details.
28 class EnumDef
extends TypeDef
{
31 * (array) Associative array of deprecated values.
33 * Keys are the deprecated parameter values, values are included in
34 * the ValidationException. If value is null, the parameter is considered
35 * not actually deprecated.
37 * Note that this does not add any values to the enumeration, it only
38 * documents existing values as being deprecated.
40 * ValidationException codes: (non-fatal)
41 * - 'deprecated-value': A deprecated value was encountered. Data:
42 * - 'flag': The value from the associative array.
44 const PARAM_DEPRECATED_VALUES
= 'param-deprecated-values';
46 public function validate( $name, $value, array $settings, array $options ) {
47 $values = $this->getEnumValues( $name, $settings, $options );
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],
63 if ( !isset( $options['values-list'] ) &&
64 count( ParamValidator
::explodeMultiValue( $value, 2 ) ) > 1
66 throw new ValidationException( $name, $value, $settings, 'notmulti', [] );
68 throw new ValidationException( $name, $value, $settings, 'badvalue', [] );
72 public function getEnumValues( $name, array $settings, array $options ) {
73 return $settings[ParamValidator
::PARAM_TYPE
];
76 public function stringifyValue( $name, $value, array $settings, array $options ) {
77 if ( !is_array( $value ) ) {
78 return parent
::stringifyValue( $name, $value, $settings, $options );
81 foreach ( $value as $v ) {
82 if ( strpos( $v, '|' ) !== false ) {
83 return "\x1f" . implode( "\x1f", $value );
86 return implode( '|', $value );