MediaWiki master
EnumDef.php
Go to the documentation of this file.
1<?php
2
4
14
32class EnumDef extends TypeDef {
33
49 public const PARAM_DEPRECATED_VALUES = 'param-deprecated-values';
50
52 public function validate( $name, $value, array $settings, array $options ) {
53 $values = $this->getEnumValues( $name, $settings, $options );
54
55 if ( in_array( $value, $values, true ) ) {
56 // Set a warning if a deprecated parameter value has been passed
57 if ( empty( $options['is-default'] ) &&
58 isset( $settings[self::PARAM_DEPRECATED_VALUES][$value] )
59 ) {
60 $msg = $settings[self::PARAM_DEPRECATED_VALUES][$value];
61 if ( $msg instanceof MessageValue ) {
62 $message = DataMessageValue::new(
63 $msg->getKey(),
64 $msg->getParams(),
65 'deprecated-value',
66 $msg instanceof DataMessageValue ? $msg->getData() : null
67 );
68 } else {
69 $message = $this->failureMessage( 'deprecated-value' );
70 }
71 $this->failure( $message, $name, $value, $settings, $options, false );
72 }
73
74 return $value;
75 }
76
77 $isMulti = isset( $options['values-list'] );
78 $this->failure(
79 $this->failureMessage( 'badvalue', [], $isMulti ? 'enummulti' : 'enumnotmulti' )
80 ->textListParams( array_map( static function ( $v ) {
81 return new ScalarParam( ParamType::PLAINTEXT, $v );
82 }, $values ) )
83 ->numParams( count( $values ) ),
84 $name, $value, $settings, $options
85 );
86 }
87
89 public function checkSettings( string $name, $settings, array $options, array $ret ): array {
90 $ret = parent::checkSettings( $name, $settings, $options, $ret );
91
92 $ret['allowedKeys'][] = self::PARAM_DEPRECATED_VALUES;
93
94 $dv = $settings[self::PARAM_DEPRECATED_VALUES] ?? [];
95 if ( !is_array( $dv ) ) {
96 $ret['issues'][self::PARAM_DEPRECATED_VALUES] = 'PARAM_DEPRECATED_VALUES must be an array, got '
97 . gettype( $dv );
98 } else {
99 $values = array_map( function ( $v ) use ( $name, $settings, $options ) {
100 return $this->stringifyValue( $name, $v, $settings, $options );
101 }, $this->getEnumValues( $name, $settings, $options ) );
102 foreach ( $dv as $k => $v ) {
103 $k = $this->stringifyValue( $name, $k, $settings, $options );
104 if ( !in_array( $k, $values, true ) ) {
105 $ret['issues'][] = "PARAM_DEPRECATED_VALUES contains \"$k\", which is not "
106 . 'one of the enumerated values';
107 } elseif ( $v instanceof MessageValue ) {
108 $ret['messages'][] = $v;
109 } elseif ( $v !== null && $v !== true ) {
110 $type = $v === false ? 'false' : ( is_object( $v ) ? get_class( $v ) : gettype( $v ) );
111 $ret['issues'][] = 'Values in PARAM_DEPRECATED_VALUES must be null, true, or MessageValue, '
112 . "but value for \"$k\" is $type";
113 }
114 }
115 }
116
117 return $ret;
118 }
119
121 public function getEnumValues( $name, array $settings, array $options ) {
122 return array_values( $settings[ParamValidator::PARAM_TYPE] );
123 }
124
126 public function stringifyValue( $name, $value, array $settings, array $options ) {
127 if ( !is_array( $value ) ) {
128 return parent::stringifyValue( $name, $value, $settings, $options );
129 }
130
131 return ParamValidator::implodeMultiValue( $value );
132 }
133
135 public function getParamInfo( $name, array $settings, array $options ) {
136 $info = parent::getParamInfo( $name, $settings, $options );
137
138 $info['type'] = $this->sortEnumValues(
139 $name,
140 $this->getEnumValues( $name, $settings, $options ),
141 $settings,
142 $options
143 );
144
145 if ( !empty( $settings[self::PARAM_DEPRECATED_VALUES] ) ) {
146 $deprecatedValues = array_intersect(
147 array_keys( $settings[self::PARAM_DEPRECATED_VALUES] ),
148 $this->getEnumValues( $name, $settings, $options )
149 );
150 if ( $deprecatedValues ) {
151 $deprecatedValues = $this->sortEnumValues( $name, $deprecatedValues, $settings, $options );
152 $info['deprecatedvalues'] = array_values( $deprecatedValues );
153 }
154 }
155
156 return $info;
157 }
158
160 public function getHelpInfo( $name, array $settings, array $options ) {
161 $info = parent::getHelpInfo( $name, $settings, $options );
162
163 $isMulti = !empty( $settings[ParamValidator::PARAM_ISMULTI] );
164
165 $values = $this->getEnumValuesForHelp( $name, $settings, $options );
166 $count = count( $values );
167
168 $i = array_search( '', $values, true );
169 if ( $i === false ) {
170 $valuesParam = new ListParam( ListType::COMMA, $values );
171 } else {
172 unset( $values[$i] );
173 $valuesParam = MessageValue::new( 'paramvalidator-help-type-enum-can-be-empty' )
174 ->commaListParams( $values )
175 ->numParams( count( $values ) );
176 }
177
178 $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-enum' )
179 ->params( $isMulti ? 2 : 1 )
180 ->params( $valuesParam )
181 ->numParams( $count );
182
183 // Suppress standard ISMULTI message, it should be incorporated into our type message.
184 $info[ParamValidator::PARAM_ISMULTI] = null;
185
186 return $info;
187 }
188
198 protected function sortEnumValues(
199 string $name, array $values, array $settings, array $options
200 ): array {
201 // sort values by deprecation status and name
202 $flags = [];
203 foreach ( $values as $k => $value ) {
204 $flag = 0;
205 if ( isset( $settings[self::PARAM_DEPRECATED_VALUES][$value] ) ) {
206 $flag |= 1;
207 }
208 $flags[$k] = $flag;
209 }
210 array_multisort( $flags, $values, SORT_NATURAL );
211
212 return $values;
213 }
214
223 protected function getEnumValuesForHelp( $name, array $settings, array $options ) {
224 $values = $this->getEnumValues( $name, $settings, $options );
225 $values = $this->sortEnumValues( $name, $values, $settings, $options );
226
227 // @todo Indicate deprecated values in some manner. Probably that needs
228 // MessageValue and/or MessageParam to have a generic ability to wrap
229 // values in HTML without that HTML coming out in the text format too.
230
231 return $values;
232 }
233
234}
Value object representing a message for i18n with alternative machine-readable data.
getData()
Get the message's structured data.
Value object representing a message parameter that consists of a list of values.
Definition ListParam.php:15
Value object representing a message parameter with one of the types from {.
Value object representing a message for i18n.
Value object representing a message parameter holding a single value.
Service for formatting and validating API parameters.
const PARAM_ISMULTI
(bool) Indicate that the parameter is multi-valued.
static implodeMultiValue(array $value)
Implode an array as a multi-valued parameter string, like implode()
const PARAM_TYPE
(string|array) Type of the parameter.
Type definition for enumeration types.
Definition EnumDef.php:32
validate( $name, $value, array $settings, array $options)
Validate the value.When ParamValidator is processing a multi-valued parameter, this will be called on...
Definition EnumDef.php:52
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.Keys in the returned array should generally corr...
Definition EnumDef.php:160
getEnumValues( $name, array $settings, array $options)
Get the values for enum-like parameters.This is primarily intended for documentation and implementati...
Definition EnumDef.php:121
checkSettings(string $name, $settings, array $options, array $ret)
Validate a parameter settings array.This is intended for validation of parameter settings during unit...
Definition EnumDef.php:89
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.This is intended as the inverse of getValue() and validate...
Definition EnumDef.php:126
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.Keys should be short strings using lowercase...
Definition EnumDef.php:135
const PARAM_DEPRECATED_VALUES
(array) Associative array of deprecated values.
Definition EnumDef.php:49
sortEnumValues(string $name, array $values, array $settings, array $options)
Sort enum values for help/param info output.
Definition EnumDef.php:198
getEnumValuesForHelp( $name, array $settings, array $options)
Return enum values formatted for the help message.
Definition EnumDef.php:223
Base definition for ParamValidator types.
Definition TypeDef.php:19
failureMessage( $code, ?array $data=null, $suffix=null)
Create a DataMessageValue representing a failure.
Definition TypeDef.php:156
getEnumValues( $name, array $settings, array $options)
Get the values for enum-like parameters.
Definition TypeDef.php:257
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition TypeDef.php:121
ParamType
The constants used to specify parameter types.
Definition ParamType.php:11
ListType
The constants used to specify list types.
Definition ListType.php:9