Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
50.00% |
1 / 2 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
WebRequestAdapter | |
50.00% |
1 / 2 |
|
50.00% |
1 / 2 |
2.50 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getVal | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * WebRequestAdapter.php |
4 | * |
5 | * This file is part of the Codex design system, the official design system |
6 | * for Wikimedia projects. It provides the `WebRequestAdapter` class, which |
7 | * implements the `IWebRequest` to adapt the behavior of an underlying |
8 | * request object to conform to the Codex interface. This adapter ensures that |
9 | * request parameters are accessed in a standardized way across the Codex system. |
10 | * |
11 | * The `WebRequestAdapter` class is designed to decouple the request handling |
12 | * from specific implementations, allowing for greater flexibility and |
13 | * consistency within the Codex system. By implementing the `IWebRequest`, |
14 | * this class makes it possible to interact with various request objects in a |
15 | * uniform manner. |
16 | * |
17 | * @category Adapter |
18 | * @package Codex\Adapter |
19 | * @since 0.1.0 |
20 | * @author Doğu Abaris <abaris@null.net> |
21 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
22 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
23 | */ |
24 | |
25 | namespace Wikimedia\Codex\Adapter; |
26 | |
27 | use Wikimedia\Codex\Contract\IWebRequest; |
28 | |
29 | /** |
30 | * WebRequestAdapter adapts a request object to the IWebRequest. |
31 | * |
32 | * The `WebRequestAdapter` class wraps around a request object and provides |
33 | * methods to access request parameters in a way that conforms to the |
34 | * `IWebRequest`. This allows the Codex system to interact with the request |
35 | * object without being tightly coupled to a specific implementation. |
36 | * |
37 | * @category Adapter |
38 | * @package Codex\Adapter |
39 | * @since 0.1.0 |
40 | * @author Doğu Abaris <abaris@null.net> |
41 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
42 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
43 | */ |
44 | class WebRequestAdapter implements IWebRequest { |
45 | |
46 | /** |
47 | * The underlying request object. |
48 | * |
49 | * @var object The request object being adapted. |
50 | */ |
51 | // phpcs:ignore - Intentional use of object as the type |
52 | protected object $request; |
53 | |
54 | /** |
55 | * Constructor for WebRequestAdapter. |
56 | * |
57 | * This constructor initializes the adapter with the given request object. |
58 | * The request object must provide a `getVal` method that retrieves request |
59 | * parameters. |
60 | * |
61 | * @since 0.1.0 |
62 | * @param object $request The request object to adapt. |
63 | */ |
64 | // phpcs:ignore - Intentional use of object as the type |
65 | public function __construct( object $request ) { |
66 | $this->request = $request; |
67 | } |
68 | |
69 | /** |
70 | * Fetch a value from the request. |
71 | * |
72 | * This method retrieves the value of a specific parameter from the underlying |
73 | * request object. If the parameter is not present, the provided default value |
74 | * will be returned. |
75 | * |
76 | * @since 0.1.0 |
77 | * @param string $name The name of the parameter to fetch. |
78 | * @param mixed $default The default value to return if the parameter is not |
79 | * set in the request. Defaults to null. |
80 | * |
81 | * @return mixed The value of the parameter, or the `$default` value if the |
82 | * parameter is not set. |
83 | */ |
84 | public function getVal( string $name, $default = null ) { |
85 | return $this->request->getVal( $name, $default ); |
86 | } |
87 | } |