MediaWiki  master
Wikimedia\ParamValidator\TypeDef Class Reference

Base definition for ParamValidator types. More...

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

Public Member Functions

 __construct (Callbacks $callbacks)
 
 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...
 
 getValue ( $name, array $settings, array $options)
 Get the value from the request. More...
 
 normalizeSettings (array $settings)
 Normalize a settings array. 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...
 

Protected Member Functions

 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...
 

Protected Attributes

Callbacks $callbacks
 

Detailed Description

Base definition for ParamValidator types.

Most methods in this class accept an "options array". This is just the $options passed to ParamValidator::getValue(), ParamValidator::validateValue(), and the like and is intended for communication of non-global state to the Callbacks.

Since
1.34 @unstable

Definition at line 18 of file TypeDef.php.

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\ParamValidator\TypeDef::__construct ( Callbacks  $callbacks)

Member Function Documentation

◆ checkSettings()

Wikimedia\ParamValidator\TypeDef::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 in Wikimedia\ParamValidator\TypeDef\UploadDef, Wikimedia\ParamValidator\TypeDef\NumericDef, Wikimedia\ParamValidator\TypeDef\TimestampDef, Wikimedia\ParamValidator\TypeDef\StringDef, Wikimedia\ParamValidator\TypeDef\EnumDef, Wikimedia\ParamValidator\ParamValidator\TypeDef\UserDef, Wikimedia\ParamValidator\ParamValidator\TypeDef\NamespaceDef, Wikimedia\ParamValidator\TypeDef\LimitDef, Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef, MediaWiki\Api\Validator\SubmoduleDef, and Wikimedia\ParamValidator\TypeDef\PasswordDef.

Definition at line 157 of file TypeDef.php.

◆ failure()

Wikimedia\ParamValidator\TypeDef::failure (   $failure,
  $name,
  $value,
array  $settings,
array  $options,
  $fatal = true 
)
protected

Record a failure message.

Depending on $fatal, this will either throw a ValidationException or call $this->callbacks->recordCondition().

Note that parameters for $name and $value are always added as $1 and $2.

Parameters
DataMessageValue | string$failureFailure code or message.
string$nameParameter name being validated.
mixed$valueValue being validated.
array$settingsParameter settings array.
array$optionsOptions array.
bool$fatalWhether the failure is fatal

Definition at line 43 of file TypeDef.php.

References Wikimedia\ParamValidator\TypeDef\failureMessage(), Wikimedia\Message\DataMessageValue\new(), and Wikimedia\ParamValidator\TypeDef\stringifyValue().

Referenced by Wikimedia\ParamValidator\TypeDef\NumericDef\checkRange(), Wikimedia\ParamValidator\TypeDef\IntegerDef\validate(), Wikimedia\ParamValidator\TypeDef\BooleanDef\validate(), Wikimedia\ParamValidator\TypeDef\FloatDef\validate(), Wikimedia\ParamValidator\TypeDef\ExpiryDef\validate(), Wikimedia\ParamValidator\TypeDef\EnumDef\validate(), Wikimedia\ParamValidator\ParamValidator\TypeDef\UserDef\validate(), Wikimedia\ParamValidator\TypeDef\StringDef\validate(), Wikimedia\ParamValidator\TypeDef\UploadDef\validate(), and Wikimedia\ParamValidator\TypeDef\TimestampDef\validate().

◆ failureMessage()

Wikimedia\ParamValidator\TypeDef::failureMessage (   $code,
array  $data = null,
  $suffix = null 
)
protected

Create a DataMessageValue representing a failure.

The message key will be "paramvalidator-$code" or "paramvalidator-$code-$suffix".

Use DataMessageValue's param mutators to add additional MessageParams. Note that failure() will prepend parameters for $name and $value.

Parameters
string$codeFailure code.
array | null$dataFailure data.
string | null$suffixSuffix to append when producing the message key
Returns
DataMessageValue

Definition at line 78 of file TypeDef.php.

References Wikimedia\Message\DataMessageValue\new().

Referenced by Wikimedia\ParamValidator\TypeDef\NumericDef\checkRange(), Wikimedia\ParamValidator\TypeDef\failure(), Wikimedia\ParamValidator\TypeDef\BooleanDef\validate(), Wikimedia\ParamValidator\TypeDef\EnumDef\validate(), Wikimedia\ParamValidator\TypeDef\StringDef\validate(), Wikimedia\ParamValidator\TypeDef\UploadDef\validate(), and Wikimedia\ParamValidator\TypeDef\TimestampDef\validate().

◆ getEnumValues()

Wikimedia\ParamValidator\TypeDef::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 in Wikimedia\ParamValidator\TypeDef\EnumDef, MediaWiki\Api\Validator\SubmoduleDef, Wikimedia\ParamValidator\ParamValidator\TypeDef\TagsDef, and Wikimedia\ParamValidator\ParamValidator\TypeDef\NamespaceDef.

Definition at line 174 of file TypeDef.php.

◆ getHelpInfo()

Wikimedia\ParamValidator\TypeDef::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 in Wikimedia\ParamValidator\ParamValidator\TypeDef\UserDef, Wikimedia\ParamValidator\TypeDef\NumericDef, Wikimedia\ParamValidator\TypeDef\EnumDef, Wikimedia\ParamValidator\TypeDef\UploadDef, Wikimedia\ParamValidator\TypeDef\StringDef, Wikimedia\ParamValidator\TypeDef\TimestampDef, Wikimedia\ParamValidator\TypeDef\FloatDef, Wikimedia\ParamValidator\TypeDef\LimitDef, Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef, Wikimedia\ParamValidator\TypeDef\ExpiryDef, Wikimedia\ParamValidator\TypeDef\BooleanDef, and Wikimedia\ParamValidator\TypeDef\IntegerDef.

Definition at line 237 of file TypeDef.php.

◆ getParamInfo()

Wikimedia\ParamValidator\TypeDef::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 in Wikimedia\ParamValidator\ParamValidator\TypeDef\UserDef, Wikimedia\ParamValidator\TypeDef\NumericDef, Wikimedia\ParamValidator\TypeDef\EnumDef, Wikimedia\ParamValidator\TypeDef\StringDef, Wikimedia\ParamValidator\ParamValidator\TypeDef\NamespaceDef, MediaWiki\Api\Validator\SubmoduleDef, and Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef.

Definition at line 211 of file TypeDef.php.

◆ getValue()

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

Get the value from the request.

Note
Only override this if you need to use something other than $this->callbacks->getValue() to fetch the value. Reformatting from a string should typically be done by self::validate().
Handling of ParamValidator::PARAM_DEFAULT should be left to ParamValidator, as should PARAM_REQUIRED and the like.
Parameters
string$nameParameter name being fetched.
array$settingsParameter settings array.
array$optionsOptions array.
Returns
null|mixed Return null if the value wasn't present, otherwise a value to be passed to self::validate().

Reimplemented in Wikimedia\ParamValidator\TypeDef\UploadDef, and Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef.

Definition at line 100 of file TypeDef.php.

◆ normalizeSettings()

◆ stringifyValue()

Wikimedia\ParamValidator\TypeDef::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 in Wikimedia\ParamValidator\TypeDef\UploadDef, Wikimedia\ParamValidator\TypeDef\TimestampDef, Wikimedia\ParamValidator\TypeDef\EnumDef, Wikimedia\ParamValidator\TypeDef\FloatDef, and Wikimedia\ParamValidator\TypeDef\BooleanDef.

Definition at line 193 of file TypeDef.php.

Referenced by Wikimedia\ParamValidator\TypeDef\failure().

◆ validate()

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

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 in Wikimedia\ParamValidator\TypeDef\TimestampDef, Wikimedia\ParamValidator\TypeDef\UploadDef, Wikimedia\ParamValidator\TypeDef\StringDef, Wikimedia\ParamValidator\ParamValidator\TypeDef\UserDef, Wikimedia\ParamValidator\TypeDef\EnumDef, Wikimedia\ParamValidator\ParamValidator\TypeDef\NamespaceDef, Wikimedia\ParamValidator\TypeDef\ExpiryDef, Wikimedia\ParamValidator\TypeDef\FloatDef, Wikimedia\ParamValidator\ParamValidator\TypeDef\TagsDef, Wikimedia\ParamValidator\TypeDef\BooleanDef, Wikimedia\ParamValidator\TypeDef\LimitDef, Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef, and Wikimedia\ParamValidator\TypeDef\IntegerDef.

Member Data Documentation

◆ $callbacks


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