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