about summary refs log tree commit diff stats
path: root/classes/Request.php
blob: d3377405db23aeb164f5be467081ddc2c0ecd383 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

class Request
{
	/** @var array<string, mixed> */
	private array $serverParams;
	/** @var array<string, mixed> */
	private array $queryParams;
	/** @var array<string, mixed> */
	private array $bodyParams;
	/** @var array<string, mixed> */
	private array $cookies;

	public function __construct()
	{
		$this->serverParams = $_SERVER;
		$this->queryParams  = $_GET;
		$this->bodyParams   = $_POST;
		$this->cookies      = $_COOKIE;
	}

	/** @return array<string, mixed> */
	public function getServerParams(): array
	{
		return $this->serverParams;
	}

	/** @return array<string, mixed> */
	public function getQueryParams(): array
	{
		return $this->queryParams;
	}

	/** @return array<string, mixed> */
	public function getParsedBody(): array
	{
		return array_merge($this->queryParams, $this->bodyParams);
	}

	/** @return array<string, mixed> */
	public function getCookieParams(): array
	{
		return $this->cookies;
	}

	public function getAttribute(string $name, mixed $default = null): mixed
	{
		return $this->getParsedBody()[$name] ?? $default;
	}

	public function withAttribute(string $name, mixed $value): self
	{
		$clone = clone $this;
		$clone->bodyParams[$name] = $value;
		return $clone;
	}
}