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(
81 static fn ( $v ) => new ScalarParam( ParamType::PLAINTEXT, $v ),
82 $values
83 ) )
84 ->numParams( count( $values ) ),
85 $name, $value, $settings, $options
86 );
87 }
88
90 public function checkSettings( string $name, $settings, array $options, array $ret ): array {
91 $ret = parent::checkSettings( $name, $settings, $options, $ret );
92
93 $ret['allowedKeys'][] = self::PARAM_DEPRECATED_VALUES;
94
95 $dv = $settings[self::PARAM_DEPRECATED_VALUES] ?? [];
96 if ( !is_array( $dv ) ) {
97 $ret['issues'][self::PARAM_DEPRECATED_VALUES] = 'PARAM_DEPRECATED_VALUES must be an array, got '
98 . gettype( $dv );
99 } else {
100 $values = array_map( function ( $v ) use ( $name, $settings, $options ) {
101 return $this->stringifyValue( $name, $v, $settings, $options );
102 }, $this->getEnumValues( $name, $settings, $options ) );
103 foreach ( $dv as $k => $v ) {
104 $k = $this->stringifyValue( $name, $k, $settings, $options );
105 if ( !in_array( $k, $values, true ) ) {
106 $ret['issues'][] = "PARAM_DEPRECATED_VALUES contains \"$k\", which is not "
107 . 'one of the enumerated values';
108 } elseif ( $v instanceof MessageValue ) {
109 $ret['messages'][] = $v;
110 } elseif ( $v !== null && $v !== true ) {
111 $type = $v === false ? 'false' : ( is_object( $v ) ? get_class( $v ) : gettype( $v ) );
112 $ret['issues'][] = 'Values in PARAM_DEPRECATED_VALUES must be null, true, or MessageValue, '
113 . "but value for \"$k\" is $type";
114 }
115 }
116 }
117
118 return $ret;
119 }
120
122 public function getEnumValues( $name, array $settings, array $options ) {
123 return array_values( $settings[ParamValidator::PARAM_TYPE] );
124 }
125
127 public function stringifyValue( $name, $value, array $settings, array $options ) {
128 if ( !is_array( $value ) ) {
129 return parent::stringifyValue( $name, $value, $settings, $options );
130 }
131
132 return ParamValidator::implodeMultiValue( $value );
133 }
134
136 public function getParamInfo( $name, array $settings, array $options ) {
137 $info = parent::getParamInfo( $name, $settings, $options );
138
139 $info['type'] = $this->sortEnumValues(
140 $name,
141 $this->getEnumValues( $name, $settings, $options ),
142 $settings,
143 $options
144 );
145
146 if ( !empty( $settings[self::PARAM_DEPRECATED_VALUES] ) ) {
147 $deprecatedValues = array_intersect(
148 array_keys( $settings[self::PARAM_DEPRECATED_VALUES] ),
149 $this->getEnumValues( $name, $settings, $options )
150 );
151 if ( $deprecatedValues ) {
152 $deprecatedValues = $this->sortEnumValues( $name, $deprecatedValues, $settings, $options );
153 $info['deprecatedvalues'] = array_values( $deprecatedValues );
154 }
155 }
156
157 return $info;
158 }
159
161 public function getHelpInfo( $name, array $settings, array $options ) {
162 $info = parent::getHelpInfo( $name, $settings, $options );
163
164 $isMulti = !empty( $settings[ParamValidator::PARAM_ISMULTI] );
165
166 $values = $this->getEnumValuesForHelp( $name, $settings, $options );
167 $count = count( $values );
168
169 $i = array_search( '', $values, true );
170 if ( $i === false ) {
171 $valuesParam = new ListParam( ListType::COMMA, $values );
172 } else {
173 unset( $values[$i] );
174 $valuesParam = MessageValue::new( 'paramvalidator-help-type-enum-can-be-empty' )
175 ->commaListParams( $values )
176 ->numParams( count( $values ) );
177 }
178
179 $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-enum' )
180 ->params( $isMulti ? 2 : 1 )
181 ->params( $valuesParam )
182 ->numParams( $count );
183
184 // Suppress standard ISMULTI message, it should be incorporated into our type message.
185 $info[ParamValidator::PARAM_ISMULTI] = null;
186
187 return $info;
188 }
189
199 protected function sortEnumValues(
200 string $name, array $values, array $settings, array $options
201 ): array {
202 // sort values by deprecation status and name
203 $flags = [];
204 foreach ( $values as $k => $value ) {
205 $flag = 0;
206 if ( isset( $settings[self::PARAM_DEPRECATED_VALUES][$value] ) ) {
207 $flag |= 1;
208 }
209 $flags[$k] = $flag;
210 }
211 array_multisort( $flags, $values, SORT_NATURAL );
212
213 return $values;
214 }
215
224 protected function getEnumValuesForHelp( $name, array $settings, array $options ) {
225 $values = $this->getEnumValues( $name, $settings, $options );
226 $values = $this->sortEnumValues( $name, $values, $settings, $options );
227
228 // @todo Indicate deprecated values in some manner. Probably that needs
229 // MessageValue and/or MessageParam to have a generic ability to wrap
230 // values in HTML without that HTML coming out in the text format too.
231
232 return $values;
233 }
234
235}
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:161
getEnumValues( $name, array $settings, array $options)
Get the values for enum-like parameters.This is primarily intended for documentation and implementati...
Definition EnumDef.php:122
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:90
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:127
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.Keys should be short strings using lowercase...
Definition EnumDef.php:136
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:199
getEnumValuesForHelp( $name, array $settings, array $options)
Return enum values formatted for the help message.
Definition EnumDef.php:224
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