http
1<?php
2
3declare(strict_types=1);
4
5namespace Upside\Http;
6
7final class Request
8{
9private array $get;
10private array $post;
11private array $cookies;
12private array $files;
13private array $server;
14private array $params;
15
16public function __construct(
17array $get = [],
18array $post = [],
19array $cookies = [],
20array $files = [],
21array $server = [],
22array $parameters = [],
23)
24{
25$this->get = $get;
26$this->post = $post;
27$this->cookies = $cookies;
28$this->files = $files;
29$this->server = $server;
30$this->params = $parameters;
31}
32
33public static function from_globals(): Request
34{
35return new Request($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER);
36}
37
38public function with_params(array $parameters): Request
39{
40$request = clone $this;
41$request->params = $parameters;
42
43return $request;
44}
45
46public function get_params(): array
47{
48return $this->params;
49}
50
51public function get_param(string $key, mixed $default = null): mixed
52{
53return $this->params[$key] ?? $default;
54}
55
56public function get(string $key, mixed $default = null): mixed
57{
58return $this->get[$key] ?? $default;
59}
60
61public function post(string $key, mixed $default = null): mixed
62{
63return $this->post[$key] ?? $default;
64}
65
66public function cookie(string $key, mixed $default = null): mixed
67{
68return $this->cookies[$key] ?? $default;
69}
70
71public function get_path(): string
72{
73return $this->server['PATH_INFO'] ?? '/';
74}
75}
76