Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 3 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
ParamValidatorCallbacks | |
0.00% |
0 / 3 |
|
0.00% |
0 / 3 |
12 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hasParam | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * ParamValidatorCallbacks.php |
4 | * |
5 | * This file is part of the Codex design system, the official design system |
6 | * for Wikimedia projects. It provides the `ParamValidatorCallbacks` class, which |
7 | * implements the `IParamValidatorCallbacks` interface. This class serves as a simple wrapper |
8 | * around an associative array, providing standardized access to web request data |
9 | * for Codex components. |
10 | * |
11 | * The `ParamValidatorCallbacks` class enables Codex to interact with request data in a consistent |
12 | * manner without being tightly coupled to a specific web framework. |
13 | * |
14 | * This class is inspired by and borrows concepts from MediaWiki's `ParamValidator`. |
15 | * While it has been adapted to meet the requirements of the Codex system, it maintains |
16 | * a similar approach to parameter validation. Any direct code or conceptual borrowing |
17 | * has been done with due acknowledgment of MediaWiki's contributors. |
18 | * |
19 | * @category ParamValidator |
20 | * @package Codex\ParamValidator |
21 | * @since 0.3.0 |
22 | * @author Doğu Abaris <abaris@null.net> |
23 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
24 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
25 | */ |
26 | |
27 | namespace Wikimedia\Codex\ParamValidator; |
28 | |
29 | use Wikimedia\Codex\Contract\ParamValidator\IParamValidatorCallbacks; |
30 | |
31 | /** |
32 | * ParamValidatorCallbacks provides access to web request data using an array structure. |
33 | * |
34 | * The `ParamValidatorCallbacks` class implements the `IWebRequest` interface, allowing it |
35 | * to provide a simple, array-based mechanism for accessing request parameters. |
36 | * It adapts an associative array of request data, allowing Codex components to |
37 | * retrieve values in a standardized way. |
38 | * |
39 | * This implementation is inspired by MediaWiki's `ParamValidator`, with notable |
40 | * adaptations for Codex-specific needs. The approach and structure of this class |
41 | * owe much to the original design, and credit is extended to MediaWiki's contributors. |
42 | * |
43 | * @category ParamValidator |
44 | * @package Codex\ParamValidator |
45 | * @since 0.3.0 |
46 | * @author Doğu Abaris <abaris@null.net> |
47 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
48 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
49 | * TODO: Once MediaWiki's ParamValidator is split into a standalone library, use it's `Callbacks` interface instead. |
50 | * TODO: Once MediaWiki's ParamValidator is split into a standalone library, this class will no longer be necessary. |
51 | */ |
52 | class ParamValidatorCallbacks implements IParamValidatorCallbacks { |
53 | |
54 | /** |
55 | * The array containing request data from $_GET and $_POST. |
56 | * |
57 | * @var array<string, mixed> |
58 | */ |
59 | private array $params; |
60 | |
61 | /** |
62 | * Constructor for ParamValidatorCallbacks. |
63 | * |
64 | * @param array<string, mixed> $params Associative array of request parameters. |
65 | */ |
66 | public function __construct( array $params ) { |
67 | $this->params = $params; |
68 | } |
69 | |
70 | /** |
71 | * Test if a parameter exists in the request. |
72 | * |
73 | * This method checks whether a given parameter name exists in the request data. |
74 | * |
75 | * @since 0.3.0 |
76 | * @param string $name The name of the parameter to check. |
77 | * @param array $options An associative array of options that may modify the behavior. |
78 | * |
79 | * @return bool True if the parameter exists; false otherwise. |
80 | */ |
81 | public function hasParam( string $name, array $options ): bool { |
82 | return array_key_exists( $name, $this->params ); |
83 | } |
84 | |
85 | /** |
86 | * Fetch a value from the request data. |
87 | * |
88 | * This method retrieves the value of a specific parameter from the request data. |
89 | * If the parameter is not present, the provided default value will be returned. |
90 | * |
91 | * @since 0.3.0 |
92 | * @param string $name The name of the parameter to fetch. |
93 | * @param mixed $default The default value to return if the parameter is not set. |
94 | * @param array $options An associative array of options that may modify the behavior. |
95 | * |
96 | * @return mixed The value of the parameter, or the `$default` value if the parameter is not set. |
97 | */ |
98 | public function getValue( string $name, $default, array $options ) { |
99 | return $this->params[$name] ?? $default; |
100 | } |
101 | } |