1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace Utilities\Router; |
||
5 | |||
6 | use Utilities\Common\Validator; |
||
7 | |||
8 | /** |
||
9 | * Request class |
||
10 | * |
||
11 | * @method static string getUri() Returns the request URI. |
||
12 | * @method static string getMethod() Returns the request method. |
||
13 | * @method static array getQueryString() Returns the request query string. |
||
14 | * @method static array getHeaders() Returns the request headers. |
||
15 | * @method static array getParams() Returns the request params. |
||
16 | * |
||
17 | * @link https://github.com/utilities-php/router |
||
18 | * @author Shahrad Elahi (https://github.com/shahradelahi) |
||
19 | * @license https://github.com/utilities-php/router/blob/master/LICENSE (MIT License) |
||
20 | */ |
||
21 | class Request |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | private static array $data = []; |
||
28 | |||
29 | /** |
||
30 | * Request constructor. |
||
31 | * |
||
32 | * @param array $initial |
||
33 | */ |
||
34 | public function __construct(array $initial) |
||
35 | { |
||
36 | $types = [ |
||
37 | 'uri' => "string", |
||
38 | 'method' => "string", |
||
39 | 'query_string' => "array", |
||
40 | 'headers' => "array", |
||
41 | "params" => "array", |
||
42 | ]; |
||
43 | |||
44 | foreach ($types as $key => $value) { |
||
45 | if (!in_array($key, array_keys($initial))) { |
||
46 | throw new \RuntimeException(sprintf( |
||
47 | 'The given data of %s, must contain the following keys: %s', |
||
48 | '\Utilities\Router\Request', |
||
49 | implode(', ', array_keys($types)) |
||
50 | )); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | if (!Validator::validateType($initial, $types)) { |
||
55 | throw new \RuntimeException(sprintf( |
||
56 | "The type of %s must be %s", |
||
57 | '\Utilities\Router\Request', |
||
58 | implode(', ', $types) |
||
59 | )); |
||
60 | } |
||
61 | |||
62 | static::$data = $initial; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Returns the request header line. |
||
67 | * |
||
68 | * @param string $name |
||
69 | * @return mixed |
||
70 | */ |
||
71 | public static function getHeaderLine(string $name): mixed |
||
72 | { |
||
73 | return static::getHeaders()[$name] ?? null; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Returns the request cookie. |
||
78 | * |
||
79 | * @param string $name |
||
80 | * @return mixed |
||
81 | */ |
||
82 | public static function getCookie(string $name): mixed |
||
83 | { |
||
84 | return $_COOKIE[$name] ?? null; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Get the request input data |
||
89 | * |
||
90 | * @return string|null |
||
91 | */ |
||
92 | public static function getInput(): ?string |
||
93 | { |
||
94 | return file_get_contents('php://input'); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param string $name |
||
99 | * @param array $arguments |
||
100 | * @return mixed |
||
101 | */ |
||
102 | public static function __callStatic(string $name, array $arguments): mixed |
||
103 | { |
||
104 | if (str_starts_with($name, 'get')) { |
||
105 | $property = substr($name, 3); |
||
106 | return static::$data[strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', $property), '_'))]; |
||
0 ignored issues
–
show
|
|||
107 | } |
||
108 | |||
109 | if (method_exists(static::class, $name)) { |
||
110 | return static::$name(...$arguments); |
||
111 | } |
||
112 | |||
113 | throw new \BadMethodCallException(sprintf( |
||
114 | 'Call to undefined method %s::%s()', |
||
115 | self::class, |
||
116 | $name |
||
117 | )); |
||
118 | } |
||
119 | |||
120 | } |