about summary refs log tree commit diff stats
path: root/classes/Request.php
blob: 57fe862b66fb6efbf3c97f6f9dcf0b53289855db (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
<?php

class Request
{
    private array $serverParams;
    private array $queryParams;
    private array $bodyParams;
    private array $cookies;

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

    public function getServerParams(): array
    {
        return $this->serverParams;
    }

    public function getQueryParams(): array
    {
        return $this->queryParams;
    }

    public function getParsedBody(): array
    {
        return array_merge($this->queryParams, $this->bodyParams);
    }

    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;
    }
}