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
67 public const PARAM_INTERNAL_VALUES = 'param-internal-values';
68
70 public function validate( $name, $value, array $settings, array $options ) {
71 $values = $this->getEnumValues( $name, $settings, $options );
72
73 if ( in_array( $value, $values, true ) ) {
74 // Set a warning if a deprecated parameter value has been passed
75 if ( empty( $options['is-default'] ) &&
76 isset( $settings[self::PARAM_DEPRECATED_VALUES][$value] )
77 ) {
78 $msg = $settings[self::PARAM_DEPRECATED_VALUES][$value];
79 if ( $msg instanceof MessageValue ) {
80 $message = DataMessageValue::new(
81 $msg->getKey(),
82 $msg->getParams(),
83 'deprecated-value',
84 $msg instanceof DataMessageValue ? $msg->getData() : null
85 );
86 } else {
87 $message = $this->failureMessage( 'deprecated-value' );
88 }
89 $this->failure( $message, $name, $value, $settings, $options, false );
90 }
91
92 return $value;
93 }
94
95 $isMulti = isset( $options['values-list'] );
96 $this->failure(
97 $this->failureMessage( 'badvalue', [], $isMulti ? 'enummulti' : 'enumnotmulti' )
98 ->textListParams( array_map(
99 static fn ( $v ) => new ScalarParam( ParamType::PLAINTEXT, $v ),
100 $values
101 ) )
102 ->numParams( count( $values ) ),
103 $name, $value, $settings, $options
104 );
105 }
106
107 private const DISCOURAGED_VALUE_INFO = [
108 [
110 'name' => 'PARAM_DEPRECATED_VALUES',
111 'info' => 'deprecatedvalues',
112 'canBeMessage' => true,
113 'allowedValues' => [ null, true ],
114 ],
115 [
117 'name' => 'PARAM_INTERNAL_VALUES',
118 'info' => 'internalvalues',
119 'canBeMessage' => false,
120 'allowedValues' => [ false, true ],
121 ],
122 ];
123
125 public function checkSettings( string $name, $settings, array $options, array $ret ): array {
126 $ret = parent::checkSettings( $name, $settings, $options, $ret );
127
128 foreach ( self::DISCOURAGED_VALUE_INFO as $vinfo ) {
129 $ret['allowedKeys'][] = $vinfo['param'];
130
131 $dv = $settings[$vinfo['param']] ?? [];
132 if ( !is_array( $dv ) ) {
133 $ret['issues'][$vinfo['param']] = "{$vinfo['name']} must be an array, got "
134 . gettype( $dv );
135 } else {
136 $values = array_map( function ( $v ) use ( $name, $settings, $options ) {
137 return $this->stringifyValue( $name, $v, $settings, $options );
138 }, $this->getEnumValues( $name, $settings, $options ) );
139 foreach ( $dv as $k => $v ) {
140 $k = $this->stringifyValue( $name, $k, $settings, $options );
141 if ( !in_array( $k, $values, true ) ) {
142 $ret['issues'][] = "{$vinfo['name']} contains \"{$k}\", which is not "
143 . 'one of the enumerated values';
144 } elseif ( $vinfo['canBeMessage'] && $v instanceof MessageValue ) {
145 $ret['messages'][] = $v;
146 } elseif ( !in_array( $v, $vinfo['allowedValues'], true ) ) {
147 $type = get_debug_type( $v );
148 $ret['issues'][] = "Values in {$vinfo['name']} has bad type for \"{$k}\": {$type}";
149 }
150 }
151 }
152 }
153
154 return $ret;
155 }
156
158 public function getEnumValues( $name, array $settings, array $options ) {
159 return array_values( $settings[ParamValidator::PARAM_TYPE] );
160 }
161
163 public function stringifyValue( $name, $value, array $settings, array $options ) {
164 if ( !is_array( $value ) ) {
165 return parent::stringifyValue( $name, $value, $settings, $options );
166 }
167
168 return ParamValidator::implodeMultiValue( $value );
169 }
170
172 public function getParamInfo( $name, array $settings, array $options ) {
173 $info = parent::getParamInfo( $name, $settings, $options );
174
175 $info['type'] = $this->sortEnumValues(
176 $name,
177 $this->getEnumValues( $name, $settings, $options ),
178 $settings,
179 $options
180 );
181
182 foreach ( self::DISCOURAGED_VALUE_INFO as $vinfo ) {
183 if ( !empty( $settings[$vinfo['param']] ) ) {
184 $badValues = array_intersect(
185 array_keys( $settings[$vinfo['param']] ),
186 $this->getEnumValues( $name, $settings, $options )
187 );
188 if ( $badValues ) {
189 $badValues = $this->sortEnumValues( $name, $badValues, $settings, $options );
190 $info[$vinfo['info']] = array_values( $badValues );
191 }
192 }
193 }
194
195 return $info;
196 }
197
199 public function getHelpInfo( $name, array $settings, array $options ) {
200 $info = parent::getHelpInfo( $name, $settings, $options );
201
202 $isMulti = !empty( $settings[ParamValidator::PARAM_ISMULTI] );
203
204 $values = $this->getEnumValuesForHelp( $name, $settings, $options );
205 $count = count( $values );
206
207 $i = array_search( '', $values, true );
208 if ( $i === false ) {
209 $valuesParam = new ListParam( ListType::COMMA, $values );
210 } else {
211 unset( $values[$i] );
212 $valuesParam = MessageValue::new( 'paramvalidator-help-type-enum-can-be-empty' )
213 ->commaListParams( $values )
214 ->numParams( count( $values ) );
215 }
216
217 $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-enum' )
218 ->params( $isMulti ? 2 : 1 )
219 ->params( $valuesParam )
220 ->numParams( $count );
221
222 // Suppress standard ISMULTI message, it should be incorporated into our type message.
223 $info[ParamValidator::PARAM_ISMULTI] = null;
224
225 return $info;
226 }
227
237 protected function sortEnumValues(
238 string $name, array $values, array $settings, array $options
239 ): array {
240 // sort values by deprecation status and name
241 $flags = [];
242 foreach ( $values as $k => $value ) {
243 $flag = 0;
244 if ( isset( $settings[self::PARAM_DEPRECATED_VALUES][$value] ) ) {
245 $flag |= 1;
246 }
247 $flags[$k] = $flag;
248 }
249 array_multisort( $flags, $values, SORT_NATURAL );
250
251 return $values;
252 }
253
262 protected function getEnumValuesForHelp( $name, array $settings, array $options ) {
263 $values = $this->getEnumValues( $name, $settings, $options );
264 $values = $this->sortEnumValues( $name, $values, $settings, $options );
265
266 // @todo Indicate deprecated values in some manner. Probably that needs
267 // MessageValue and/or MessageParam to have a generic ability to wrap
268 // values in HTML without that HTML coming out in the text format too.
269
270 return $values;
271 }
272
273}
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:70
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.Keys in the returned array should generally corr...
Definition EnumDef.php:199
getEnumValues( $name, array $settings, array $options)
Get the values for enum-like parameters.This is primarily intended for documentation and implementati...
Definition EnumDef.php:158
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:125
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:163
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.Keys should be short strings using lowercase...
Definition EnumDef.php:172
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:237
getEnumValuesForHelp( $name, array $settings, array $options)
Return enum values formatted for the help message.
Definition EnumDef.php:262
const PARAM_INTERNAL_VALUES
(array) Associative array of internal values.
Definition EnumDef.php:67
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