aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/app/src/Form/FormState.php
blob: e56e8f07418a3447650aa463d81af5a68416e149 (plain)
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
58
59
60
61
62
<?php

declare(strict_types=1);

namespace Nsfisis\Albatross\Form;

use Psr\Http\Message\ServerRequestInterface;

final class FormState
{
    /**
     * @var array<string, string>
     */
    private array $errors = [];

    /**
     * @param array<string, string> $params
     */
    public function __construct(private readonly array $params = [])
    {
    }

    public static function fromRequest(ServerRequestInterface $request): self
    {
        return new self((array)$request->getParsedBody());
    }

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

    public function get(string $key): ?string
    {
        $value = $this->params[$key] ?? null;
        if (isset($value)) {
            return $key === 'password' ? $value : trim($value);
        } else {
            return null;
        }
    }

    /**
     * @param array<string, string> $errors
     */
    public function setErrors(array $errors): self
    {
        $this->errors = $errors;
        return $this;
    }

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