File: README.md

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (55 lines) | stat: -rw-r--r-- 1,743 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Wikimedia API Parameter Validator
=================================

This library implements a system for processing and validating parameters to an
API from data like that in PHP's `$_GET`, `$_POST`, and `$_FILES` arrays, based
on a declarative definition of available parameters.

Usage
-----

<pre lang="php">
use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
use Wikimedia\ParamValidator\SimpleCallbacks as ParamValidatorCallbacks;
use Wikimedia\ParamValidator\ValidationException;

// We assume these are available from your environment in some manner.
/** @var Wikimedia\ObjectFactory\ObjectFactory $objectFactory */
$objectFactory = ...;
/** @var Wikimedia\Message\MessageFormatterFactory $messageFormatterFactory */
$messageFormatterFactory = ...;

$validator = new ParamValidator(
	new ParamValidatorCallbacks( $_POST + $_GET, $_FILES ),
	$objectFactory
);

try {
	$intValue = $validator->getValue( 'intParam', [
			ParamValidator::PARAM_TYPE => 'integer',
			ParamValidator::PARAM_DEFAULT => 0,
			IntegerDef::PARAM_MIN => 0,
			IntegerDef::PARAM_MAX => 5,
	] );
} catch ( ValidationException $ex ) {
	$error = $messageFormatterFactory->getTextFormatter( 'en' )->format( $ex->getFailureMessage() );
	echo "Validation error: $error\n";
}
</pre>

Failure reporting
-----------------

This library uses Wikimedia\Message\DataMessageValue objects to report errors
in both human-readable and machine-readable formats. Basic i18n for all errors
generated by the library is included.

For possible failure codes and their parameters, see the documentation of the
relevant `PARAM_*` constants and TypeDef classes.

Running tests
-------------

    composer install --prefer-dist
    composer test