param_verify
This module does nothing on its own, it's for developers only. It provides the glue from the param_verify package (on packagist) to Drupal by supplying a service to the factory class. It does nothing else.
If loaded using composer it will fetch the adaddinsane/param_verify package, and its dependencies.
How it works
Adapted from the README file of adaddinsane/param_verify.
This package can verify (type check) a set of array values, which are most likely parameters in a call, to ensure they are of the right type, and in some cases within range. It covers most PHP types.
Sometimes you cannot specify in a list of arguments in a function/method perhaps because it's too long, or because they might vary too much between derived class types. Or possibly the data is coming from an external source, such as an API call, and you want to make sure all the required values exist, and all supplied values are the right type.
This class is configured with an array which provides a list of which values are required and their types. Optional values can also be included in which case they are only checked if they are present (no error is generated if they are missing).
When a verification is performed a list of errors will be returned, if it's empty then everything was fine.
Structure
---------
The settings array looks like this:
[
'name' => [
'required' => true,
'type' => 'string',
'data' => '',
'range' => []
]
]
The 'name' is the key value in the array being tested (the name of the property).
Only the 'type' key is mandatory, and must be one of the acceptable values; the 'required' key is assumed false if missing; some types require an additional data string, for a 'regex' type it's the regular expression, for the 'class' type it's the fully qualified class name, and so on.
The 'range' key applies to string, int and float, and allows length and range restrictions to be inserted.
Types
The complete list of types:
- any matches anything
- null matches only a null value (for completeness, you never know)
- class matches a specific class or interface (FQN in 'data')
- object matches any object
- array matches any array, arrays can contain their own sub-definitions.
- int matches an integer value (and can have a range)
- bool only matches true or false
- int_bool matches true, false, 0 or 1
- float matches a float value (and can have a range)
- string matches any string (and can have a range)
- string_list matches a string against a fixed list of strings (in 'data' separated by '|'). This is like an old style "enum". This type is also used to verify the types of the settings/configuration items.
- regex tests a string with a regular expression (in 'data')
- callable only matches a callable
- resource only matches a resource
- url matches a string which is also a valid URL
- email matches a string which is also a valid email address
- enum** only matches a PHP8.1 enum (FQN in 'data')
For further information see the package README file.
Requirements
Drupal 8 or 9 at present. PHP 7.4 or better, PHP 8.1 allows the use of the "enum" verifier.
Installation
Install with composer: composer require drupal/param_verify:^1.4
Support
Do you like to read Science Fiction, Fantasy, Steampunk or Crime Fiction? You might find something you like here most books also available through Kindle Unlimited.