MediaWiki  master
Wikimedia\ParamValidator\TypeDef\EnumDef Class Reference

Type definition for enumeration types. More...

Inheritance diagram for Wikimedia\ParamValidator\TypeDef\EnumDef:
Collaboration diagram for Wikimedia\ParamValidator\TypeDef\EnumDef:

Public Member Functions

 checkSettings (string $name, $settings, array $options, array $ret)
 Validate a parameter settings array. More...
 
 getEnumValues ( $name, array $settings, array $options)
 Get the values for enum-like parameters. More...
 
 getHelpInfo ( $name, array $settings, array $options)
 Describe parameter settings in human-readable format. More...
 
 getParamInfo ( $name, array $settings, array $options)
 Describe parameter settings in a machine-readable format. More...
 
 stringifyValue ( $name, $value, array $settings, array $options)
 Convert a value to a string representation. More...
 
 validate ( $name, $value, array $settings, array $options)
 Validate the value. More...
 
- Public Member Functions inherited from Wikimedia\ParamValidator\TypeDef
 __construct (Callbacks $callbacks)
 
 getValue ( $name, array $settings, array $options)
 Get the value from the request. More...
 
 normalizeSettings (array $settings)
 Normalize a settings array. More...
 

Public Attributes

const PARAM_DEPRECATED_VALUES = 'param-deprecated-values'
 (array) Associative array of deprecated values. More...
 

Protected Member Functions

 getEnumValuesForHelp ( $name, array $settings, array $options)
 Return enum values formatted for the help message. More...
 
 sortEnumValues (string $name, array $values, array $settings, array $options)
 Sort enum values for help/param info output. More...
 
- Protected Member Functions inherited from Wikimedia\ParamValidator\TypeDef
 failure ( $failure, $name, $value, array $settings, array $options, $fatal=true)
 Record a failure message. More...
 
 failureMessage ( $code, array $data=null, $suffix=null)
 Create a DataMessageValue representing a failure. More...
 

Additional Inherited Members

- Protected Attributes inherited from Wikimedia\ParamValidator\TypeDef
Callbacks $callbacks
 

Detailed Description

Type definition for enumeration types.

This class expects that PARAM_TYPE is an array of allowed values. Subclasses may override getEnumValues() to determine the allowed values differently.

The result from validate() is one of the defined values.

Failure codes:

  • 'badvalue': The value is not a recognized value. No data.

Additional codes may be generated when using certain PARAM constants. See the constants' documentation for details.

Since
1.34 @unstable

Definition at line 32 of file EnumDef.php.

Member Function Documentation

◆ checkSettings()

Wikimedia\ParamValidator\TypeDef\EnumDef::checkSettings ( string  $name,
  $settings,
array  $options,
array  $ret 
)

Validate a parameter settings array.

This is intended for validation of parameter settings during unit or integration testing, and should implement strict checks.

The rest of the code should generally be more permissive.

See also
ParamValidator::checkSettings()
Parameters
string$nameParameter name
array | mixed$settingsDefault value or an array of settings using PARAM_* constants.
array$optionsOptions array, passed through to the TypeDef and Callbacks.
array$ret
  • 'issues': (string[]) Errors detected in $settings, as English text. If the settings are valid, this will be the empty array. Keys on input are ParamValidator constants, allowing the typedef to easily override core validation; this need not be preserved when returned.
  • 'allowedKeys': (string[]) ParamValidator keys that are allowed in $settings.
  • 'messages': (MessageValue[]) Messages to be checked for existence.
Returns
array $ret, with any relevant changes.

Reimplemented from Wikimedia\ParamValidator\TypeDef.

Reimplemented in Wikimedia\ParamValidator\ParamValidator\TypeDef\NamespaceDef, and MediaWiki\Api\Validator\SubmoduleDef.

Definition at line 87 of file EnumDef.php.

References $type, Wikimedia\ParamValidator\TypeDef\EnumDef\getEnumValues(), Wikimedia\ParamValidator\TypeDef\EnumDef\PARAM_DEPRECATED_VALUES, and Wikimedia\ParamValidator\TypeDef\EnumDef\stringifyValue().

◆ getEnumValues()

Wikimedia\ParamValidator\TypeDef\EnumDef::getEnumValues (   $name,
array  $settings,
array  $options 
)

Get the values for enum-like parameters.

This is primarily intended for documentation and implementation of PARAM_ALL; it is the responsibility of the TypeDef to ensure that validate() accepts the values returned here.

Parameters
string$nameParameter name being validated.
array$settingsParameter settings array.
array$optionsOptions array.
Returns
array|null All possible enumerated values, or null if this is not an enumeration.

Reimplemented from Wikimedia\ParamValidator\TypeDef.

Reimplemented in MediaWiki\Api\Validator\SubmoduleDef, Wikimedia\ParamValidator\ParamValidator\TypeDef\TagsDef, and Wikimedia\ParamValidator\ParamValidator\TypeDef\NamespaceDef.

Definition at line 118 of file EnumDef.php.

References Wikimedia\ParamValidator\ParamValidator\PARAM_TYPE.

Referenced by Wikimedia\ParamValidator\TypeDef\EnumDef\checkSettings(), Wikimedia\ParamValidator\TypeDef\EnumDef\getEnumValuesForHelp(), Wikimedia\ParamValidator\TypeDef\EnumDef\getParamInfo(), and Wikimedia\ParamValidator\TypeDef\EnumDef\validate().

◆ getEnumValuesForHelp()

Wikimedia\ParamValidator\TypeDef\EnumDef::getEnumValuesForHelp (   $name,
array  $settings,
array  $options 
)
protected

Return enum values formatted for the help message.

Parameters
string$nameParameter name being described.
array$settingsParameter settings array.
array$optionsOptions array.
Returns
(MessageParam|string)[]

Reimplemented in MediaWiki\Api\Validator\SubmoduleDef.

Definition at line 217 of file EnumDef.php.

References Wikimedia\ParamValidator\TypeDef\EnumDef\getEnumValues(), and Wikimedia\ParamValidator\TypeDef\EnumDef\sortEnumValues().

Referenced by Wikimedia\ParamValidator\TypeDef\EnumDef\getHelpInfo().

◆ getHelpInfo()

Wikimedia\ParamValidator\TypeDef\EnumDef::getHelpInfo (   $name,
array  $settings,
array  $options 
)

Describe parameter settings in human-readable format.

Keys in the returned array should generally correspond to PARAM constants.

If relevant, a MessageValue describing the type itself should be returned with key ParamValidator::PARAM_TYPE.

The default messages for other ParamValidator-defined PARAM constants may be suppressed by returning null as the value for those constants, or replaced by returning a replacement MessageValue. Normally, however, the default messages should not be changed.

MessageValues describing any other constraints applied via PARAM constants specific to this class should also be returned.

Parameters
string$nameParameter name being described.
array$settingsParameter settings array.
array$optionsOptions array.
Returns
(MessageValue|null)[]

Reimplemented from Wikimedia\ParamValidator\TypeDef.

Definition at line 154 of file EnumDef.php.

References Wikimedia\ParamValidator\TypeDef\EnumDef\getEnumValuesForHelp(), Wikimedia\Message\MessageValue\new(), Wikimedia\ParamValidator\ParamValidator\PARAM_ISMULTI, and Wikimedia\ParamValidator\ParamValidator\PARAM_TYPE.

◆ getParamInfo()

Wikimedia\ParamValidator\TypeDef\EnumDef::getParamInfo (   $name,
array  $settings,
array  $options 
)

Describe parameter settings in a machine-readable format.

Keys should be short strings using lowercase ASCII letters. Values should generally be values that could be encoded in JSON or the like.

This is intended to handle PARAM constants specific to this class. It generally shouldn't handle constants defined on ParamValidator itself.

Parameters
string$nameParameter name.
array$settingsParameter settings array.
array$optionsOptions array.
Returns
array

Reimplemented from Wikimedia\ParamValidator\TypeDef.

Reimplemented in Wikimedia\ParamValidator\ParamValidator\TypeDef\NamespaceDef, and MediaWiki\Api\Validator\SubmoduleDef.

Definition at line 130 of file EnumDef.php.

References Wikimedia\ParamValidator\TypeDef\EnumDef\getEnumValues(), and Wikimedia\ParamValidator\TypeDef\EnumDef\sortEnumValues().

◆ sortEnumValues()

Wikimedia\ParamValidator\TypeDef\EnumDef::sortEnumValues ( string  $name,
array  $values,
array  $settings,
array  $options 
)
protected

Sort enum values for help/param info output.

Parameters
string$nameParameter name being described.
string[]$valuesValues being sorted
array$settingsParameter settings array.
array$optionsOptions array.
Returns
string[]

Reimplemented in MediaWiki\Api\Validator\SubmoduleDef.

Definition at line 192 of file EnumDef.php.

Referenced by Wikimedia\ParamValidator\TypeDef\EnumDef\getEnumValuesForHelp(), and Wikimedia\ParamValidator\TypeDef\EnumDef\getParamInfo().

◆ stringifyValue()

Wikimedia\ParamValidator\TypeDef\EnumDef::stringifyValue (   $name,
  $value,
array  $settings,
array  $options 
)

Convert a value to a string representation.

This is intended as the inverse of getValue() and validate(): this should accept anything returned by those methods or expected to be used as PARAM_DEFAULT, and if the string from this method is passed in as client input or PARAM_DEFAULT it should give equivalent output from validate().

Parameters
string$nameParameter name being converted.
mixed$valueParameter value being converted. Do not pass null.
array$settingsParameter settings array.
array$optionsOptions array.
Returns
string|null Return null if there is no representation of $value reasonably satisfying the description given.

Reimplemented from Wikimedia\ParamValidator\TypeDef.

Definition at line 122 of file EnumDef.php.

References Wikimedia\ParamValidator\ParamValidator\implodeMultiValue().

Referenced by Wikimedia\ParamValidator\TypeDef\EnumDef\checkSettings().

◆ validate()

Wikimedia\ParamValidator\TypeDef\EnumDef::validate (   $name,
  $value,
array  $settings,
array  $options 
)

Validate the value.

When ParamValidator is processing a multi-valued parameter, this will be called once for each of the supplied values. Which may mean zero calls.

When getValue() returned null, this will not be called.

Parameters
string$nameParameter name being validated.
mixed$valueValue to validate, from getValue().
array$settingsParameter settings array.
array$optionsOptions array. Note the following values that may be set by ParamValidator:
  • is-default: (bool) If present and true, the value was taken from PARAM_DEFAULT rather that being supplied by the client.
  • values-list: (string[]) If defined, values of a multi-valued parameter are being processed (and this array holds the full set of values).
Returns
mixed Validated value
Exceptions
ValidationExceptionif the value is invalid

Reimplemented from Wikimedia\ParamValidator\TypeDef.

Reimplemented in Wikimedia\ParamValidator\ParamValidator\TypeDef\NamespaceDef, and Wikimedia\ParamValidator\ParamValidator\TypeDef\TagsDef.

Definition at line 51 of file EnumDef.php.

References Wikimedia\ParamValidator\TypeDef\failure(), Wikimedia\ParamValidator\TypeDef\failureMessage(), Wikimedia\Message\DataMessageValue\getData(), Wikimedia\ParamValidator\TypeDef\EnumDef\getEnumValues(), Wikimedia\Message\DataMessageValue\new(), Wikimedia\ParamValidator\TypeDef\EnumDef\PARAM_DEPRECATED_VALUES, and Wikimedia\Message\ParamType\PLAINTEXT.

Member Data Documentation

◆ PARAM_DEPRECATED_VALUES

const Wikimedia\ParamValidator\TypeDef\EnumDef::PARAM_DEPRECATED_VALUES = 'param-deprecated-values'

(array) Associative array of deprecated values.

Keys are the deprecated parameter values. Value is one of the following:

  • null: Parameter isn't actually deprecated.
  • true: Parameter is deprecated.
  • MessageValue: Parameter is deprecated, and this message (converted to a DataMessageValue) is used in place of the default for passing to $this->failure().

Note that this does not add any values to the enumeration, it only documents existing values as being deprecated.

Failure codes: (non-fatal)

  • 'deprecated-value': A deprecated value was encountered. No data.

Definition at line 49 of file EnumDef.php.

Referenced by Wikimedia\ParamValidator\TypeDef\EnumDef\checkSettings(), MediaWiki\Api\Validator\ApiParamValidator\mapDeprecatedSettingsMessages(), and Wikimedia\ParamValidator\TypeDef\EnumDef\validate().


The documentation for this class was generated from the following file: