MediaWiki REL1_40
|
Service for formatting and validating API parameters. More...
Public Member Functions | |
__construct (Callbacks $callbacks, ObjectFactory $objectFactory, array $options=[]) | |
addTypeDef ( $name, $typeDef) | |
Register a type handler. | |
addTypeDefs (array $typeDefs) | |
Register multiple type handlers. | |
checkSettings (string $name, $settings, array $options) | |
Validate a parameter settings array. | |
getHelpInfo ( $name, $settings, array $options) | |
Describe parameter settings in human-readable format. | |
getParamInfo ( $name, $settings, array $options) | |
Describe parameter settings in a machine-readable format. | |
getTypeDef ( $type) | |
Get the TypeDef for a type. | |
getValue ( $name, $settings, array $options=[]) | |
Fetch and validate a parameter value using a settings array. | |
hasTypeDef ( $name) | |
Test if a type is registered. | |
knownTypes () | |
List known type names. | |
normalizeSettings ( $settings) | |
Normalize a parameter settings array. | |
overrideTypeDef ( $name, $typeDef) | |
Register a type handler, overriding any existing handler. | |
validateValue ( $name, $value, $settings, array $options=[]) | |
Validate a parameter value using a settings array. | |
Static Public Member Functions | |
static | explodeMultiValue ( $value, $limit) |
Split a multi-valued parameter string, like explode() | |
static | implodeMultiValue (array $value) |
Implode an array as a multi-valued parameter string, like implode() | |
Public Attributes | |
const | ALL_DEFAULT_STRING = '*' |
Magic "all values" value when PARAM_ALL is true. | |
Constants for parameter settings arrays | |
These constants are keys in the settings array that define how the parameters coming in from the request are to be interpreted. If a constant is associated with a failure code, the failure code and data are described. ValidationExceptions are typically thrown, but those indicated as "non-fatal" are instead passed to Callbacks::recordCondition(). Additional constants may be defined by TypeDef subclasses, or by other libraries for controlling things like auto-generated parameter documentation. For purposes of namespacing the constants, the values of all constants defined by this library begin with 'param-'. | |
const | PARAM_DEFAULT = 'param-default' |
(mixed) Default value of the parameter. | |
const | PARAM_TYPE = 'param-type' |
(string|array) Type of the parameter. | |
const | PARAM_REQUIRED = 'param-required' |
(bool) Indicate that the parameter is required. | |
const | PARAM_ISMULTI = 'param-ismulti' |
(bool) Indicate that the parameter is multi-valued. | |
const | PARAM_ISMULTI_LIMIT1 = 'param-ismulti-limit1' |
(int) Maximum number of multi-valued parameter values allowed | |
const | PARAM_ISMULTI_LIMIT2 = 'param-ismulti-limit2' |
(int) Maximum number of multi-valued parameter values allowed for users allowed high limits. | |
const | PARAM_ALL = 'param-all' |
(bool|string) Whether a magic "all values" value exists for multi-valued enumerated types, and if so what that value is. | |
const | PARAM_ALLOW_DUPLICATES = 'param-allow-duplicates' |
(bool) Allow the same value to be set more than once when PARAM_ISMULTI is true? | |
const | PARAM_SENSITIVE = 'param-sensitive' |
(bool) Indicate that the parameter's value should not be logged. | |
const | PARAM_DEPRECATED = 'param-deprecated' |
(bool) Indicate that a deprecated parameter was used. | |
const | PARAM_IGNORE_UNRECOGNIZED_VALUES = 'param-ignore-unrecognized-values' |
(bool) Whether to downgrade "badvalue" errors to non-fatal when validating multi-valued parameters. | |
Static Public Attributes | |
static | $STANDARD_TYPES |
A list of standard type names and types that may be passed as $typeDefs to __construct(). | |
Service for formatting and validating API parameters.
A settings array is simply an array with keys being the relevant PARAM_* constants from this class, TypeDef, and its subclasses.
As a general overview of the architecture here:
Definition at line 42 of file ParamValidator.php.
Wikimedia\ParamValidator\ParamValidator::__construct | ( | Callbacks | $callbacks, |
ObjectFactory | $objectFactory, | ||
array | $options = [] ) |
Callbacks | $callbacks | |
ObjectFactory | $objectFactory | To turn specs into TypeDef objects |
array | $options | Associative array of additional settings
|
Definition at line 222 of file ParamValidator.php.
References Wikimedia\ParamValidator\ParamValidator\addTypeDefs().
Wikimedia\ParamValidator\ParamValidator::addTypeDef | ( | $name, | |
$typeDef ) |
Register a type handler.
To allow code to omit PARAM_TYPE in settings arrays to derive the type from PARAM_DEFAULT, it is strongly recommended that the following types be registered: "boolean", "integer", "double", "string", "NULL", and "enum".
When using ObjectFactory specs, the following extra arguments are passed:
string | $name | Type name |
TypeDef | array | $typeDef | Type handler or ObjectFactory spec to create one. |
Definition at line 268 of file ParamValidator.php.
Referenced by Wikimedia\ParamValidator\ParamValidator\addTypeDefs().
Wikimedia\ParamValidator\ParamValidator::addTypeDefs | ( | array | $typeDefs | ) |
Register multiple type handlers.
array | $typeDefs | Associative array mapping $name to $typeDef . |
Definition at line 249 of file ParamValidator.php.
References Wikimedia\ParamValidator\ParamValidator\addTypeDef().
Referenced by Wikimedia\ParamValidator\ParamValidator\__construct().
Wikimedia\ParamValidator\ParamValidator::checkSettings | ( | string | $name, |
$settings, | |||
array | $options ) |
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.
string | $name | Parameter name |
array | mixed | $settings | Default value or an array of settings using PARAM_* constants. |
array | $options | Options array, passed through to the TypeDef and Callbacks. |
$settings
.Definition at line 392 of file ParamValidator.php.
References $type, and Wikimedia\ParamValidator\ParamValidator\PARAM_TYPE.
|
static |
Split a multi-valued parameter string, like explode()
Note that, unlike explode(), this will return an empty array when given an empty string.
string | $value | |
int | $limit |
Definition at line 829 of file ParamValidator.php.
Wikimedia\ParamValidator\ParamValidator::getHelpInfo | ( | $name, | |
$settings, | |||
array | $options ) |
Describe parameter settings in human-readable format.
string | $name | Parameter name being described. |
array | mixed | $settings | Default value or an array of settings using PARAM_* constants. |
array | $options | Options array. |
Definition at line 742 of file ParamValidator.php.
Wikimedia\ParamValidator\ParamValidator::getParamInfo | ( | $name, | |
$settings, | |||
array | $options ) |
Describe parameter settings in a machine-readable format.
string | $name | Parameter name. |
array | mixed | $settings | Default value or an array of settings using PARAM_* constants. |
array | $options | Options array. |
Definition at line 683 of file ParamValidator.php.
Wikimedia\ParamValidator\ParamValidator::getTypeDef | ( | $type | ) |
Get the TypeDef for a type.
string | array | $type | Any array is considered equivalent to the string "enum". |
Definition at line 315 of file ParamValidator.php.
References $type.
Referenced by Wikimedia\ParamValidator\ParamValidator\normalizeSettings().
Wikimedia\ParamValidator\ParamValidator::getValue | ( | $name, | |
$settings, | |||
array | $options = [] ) |
Fetch and validate a parameter value using a settings array.
string | $name | Parameter name |
array | mixed | $settings | Default value or an array of settings using PARAM_* constants. |
array | $options | Options array, passed through to the TypeDef and Callbacks.
|
ValidationException | if the value is invalid |
Definition at line 515 of file ParamValidator.php.
Wikimedia\ParamValidator\ParamValidator::hasTypeDef | ( | $name | ) |
Test if a type is registered.
string | $name | Type name |
Definition at line 306 of file ParamValidator.php.
|
static |
Implode an array as a multi-valued parameter string, like implode()
array | $value |
Definition at line 850 of file ParamValidator.php.
Referenced by Wikimedia\ParamValidator\TypeDef\EnumDef\stringifyValue(), and Wikimedia\ParamValidator\TypeDef\IntegerDef\stringifyValue().
Wikimedia\ParamValidator\ParamValidator::knownTypes | ( | ) |
Wikimedia\ParamValidator\ParamValidator::normalizeSettings | ( | $settings | ) |
Normalize a parameter settings array.
array | mixed | $settings | Default value or an array of settings using PARAM_* constants. |
Definition at line 363 of file ParamValidator.php.
References Wikimedia\ParamValidator\ParamValidator\getTypeDef().
Wikimedia\ParamValidator\ParamValidator::overrideTypeDef | ( | $name, | |
$typeDef ) |
Register a type handler, overriding any existing handler.
string | $name | Type name |
TypeDef | array | null | $typeDef | As for addTypeDef, or null to unregister a type. |
Definition at line 287 of file ParamValidator.php.
Wikimedia\ParamValidator\ParamValidator::validateValue | ( | $name, | |
$value, | |||
$settings, | |||
array | $options = [] ) |
Validate a parameter value using a settings array.
string | $name | Parameter name |
null | mixed | $value | Parameter value |
array | mixed | $settings | Default value or an array of settings using PARAM_* constants. |
array | $options | Options array, passed through to the TypeDef and Callbacks.
|
ValidationException | if the value is invalid |
Definition at line 567 of file ParamValidator.php.
References Wikimedia\ParamValidator\ValidationException\getFailureMessage(), and Wikimedia\Message\ParamType\PLAINTEXT.
|
static |
A list of standard type names and types that may be passed as $typeDefs
to __construct().
Definition at line 177 of file ParamValidator.php.
const Wikimedia\ParamValidator\ParamValidator::ALL_DEFAULT_STRING = '*' |
Magic "all values" value when PARAM_ALL is true.
Definition at line 174 of file ParamValidator.php.
const Wikimedia\ParamValidator\ParamValidator::PARAM_ALL = 'param-all' |
(bool|string) Whether a magic "all values" value exists for multi-valued enumerated types, and if so what that value is.
When PARAM_TYPE has a defined set of values and PARAM_ISMULTI is true, this allows for an asterisk ('*') to be passed in place of a pipe-separated list of every possible value. If a string is set, it will be used in place of the asterisk.
Definition at line 137 of file ParamValidator.php.
const Wikimedia\ParamValidator\ParamValidator::PARAM_ALLOW_DUPLICATES = 'param-allow-duplicates' |
(bool) Allow the same value to be set more than once when PARAM_ISMULTI is true?
If not truthy, the set of values will be passed through array_values( array_unique() )
. The default is falsey.
Definition at line 145 of file ParamValidator.php.
const Wikimedia\ParamValidator\ParamValidator::PARAM_DEFAULT = 'param-default' |
(mixed) Default value of the parameter.
If omitted, null is the default.
TypeDef::validate() will be informed when the default value was used by the presence of 'is-default' in $options.
Definition at line 68 of file ParamValidator.php.
Referenced by Wikimedia\ParamValidator\TypeDef\UploadDef\checkSettings(), Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef\getHelpInfo(), and Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef\normalizeSettings().
const Wikimedia\ParamValidator\ParamValidator::PARAM_DEPRECATED = 'param-deprecated' |
(bool) Indicate that a deprecated parameter was used.
Failure codes: (non-fatal)
Definition at line 161 of file ParamValidator.php.
const Wikimedia\ParamValidator\ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES = 'param-ignore-unrecognized-values' |
(bool) Whether to downgrade "badvalue" errors to non-fatal when validating multi-valued parameters.
Definition at line 168 of file ParamValidator.php.
const Wikimedia\ParamValidator\ParamValidator::PARAM_ISMULTI = 'param-ismulti' |
(bool) Indicate that the parameter is multi-valued.
A multi-valued parameter may be submitted in one of several formats. All of the following result in a value of ‘[ 'a’, 'b', 'c' ]`.
Each of the multiple values is passed individually to the TypeDef. $options will contain a 'values-list' key holding the entire list.
By default duplicates are removed from the resulting parameter list. Use PARAM_ALLOW_DUPLICATES to override that behavior.
Failure codes:
Definition at line 112 of file ParamValidator.php.
Referenced by Wikimedia\ParamValidator\TypeDef\LimitDef\checkSettings(), Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef\checkSettings(), Wikimedia\ParamValidator\TypeDef\BooleanDef\getHelpInfo(), Wikimedia\ParamValidator\TypeDef\EnumDef\getHelpInfo(), Wikimedia\ParamValidator\TypeDef\ExpiryDef\getHelpInfo(), Wikimedia\ParamValidator\TypeDef\FloatDef\getHelpInfo(), Wikimedia\ParamValidator\TypeDef\IntegerDef\getHelpInfo(), Wikimedia\ParamValidator\TypeDef\NumericDef\getHelpInfo(), Wikimedia\ParamValidator\TypeDef\TimestampDef\getHelpInfo(), Wikimedia\ParamValidator\TypeDef\LimitDef\normalizeSettings(), and Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef\normalizeSettings().
const Wikimedia\ParamValidator\ParamValidator::PARAM_ISMULTI_LIMIT1 = 'param-ismulti-limit1' |
(int) Maximum number of multi-valued parameter values allowed
Definition at line 119 of file ParamValidator.php.
const Wikimedia\ParamValidator\ParamValidator::PARAM_ISMULTI_LIMIT2 = 'param-ismulti-limit2' |
(int) Maximum number of multi-valued parameter values allowed for users allowed high limits.
Definition at line 127 of file ParamValidator.php.
const Wikimedia\ParamValidator\ParamValidator::PARAM_REQUIRED = 'param-required' |
(bool) Indicate that the parameter is required.
Failure codes:
Definition at line 84 of file ParamValidator.php.
Referenced by Wikimedia\ParamValidator\TypeDef\StringDef\validate().
const Wikimedia\ParamValidator\ParamValidator::PARAM_SENSITIVE = 'param-sensitive' |
(bool) Indicate that the parameter's value should not be logged.
Failure codes: (non-fatal)
Definition at line 153 of file ParamValidator.php.
Referenced by Wikimedia\ParamValidator\TypeDef\PasswordDef\checkSettings(), and Wikimedia\ParamValidator\TypeDef\PasswordDef\normalizeSettings().
const Wikimedia\ParamValidator\ParamValidator::PARAM_TYPE = 'param-type' |
(string|array) Type of the parameter.
Must be a registered type or an array of enumerated values (in which case the "enum" type must be registered). If omitted, the default is the PHP type of the default value (see PARAM_DEFAULT).
Definition at line 76 of file ParamValidator.php.
Referenced by Wikimedia\ParamValidator\ParamValidator\checkSettings(), Wikimedia\ParamValidator\TypeDef\EnumDef\getEnumValues(), Wikimedia\ParamValidator\TypeDef\BooleanDef\getHelpInfo(), Wikimedia\ParamValidator\TypeDef\EnumDef\getHelpInfo(), Wikimedia\ParamValidator\TypeDef\ExpiryDef\getHelpInfo(), Wikimedia\ParamValidator\TypeDef\FloatDef\getHelpInfo(), Wikimedia\ParamValidator\TypeDef\IntegerDef\getHelpInfo(), Wikimedia\ParamValidator\TypeDef\LimitDef\getHelpInfo(), Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef\getHelpInfo(), Wikimedia\ParamValidator\TypeDef\TimestampDef\getHelpInfo(), and Wikimedia\ParamValidator\TypeDef\UploadDef\getHelpInfo().