aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/app/src/Form/FormBase.php
blob: cc2944247a2bddc7f6d7225db73b41d5c7db07ae (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
<?php

declare(strict_types=1);

namespace Nsfisis\Albatross\Form;

abstract class FormBase
{
    public function __construct(
        protected readonly FormState $state,
    ) {
    }

    /**
     * @return array{action: ?string, submit_label: string, items: list<FormItem>, state: array<string, string>, errors: array<string, string>}
     */
    public function toTemplateVars(): array
    {
        return [
            'action' => $this->action(),
            'submit_label' => $this->submitLabel(),
            'items' => $this->items(),
            'state' => $this->state->getParams(),
            'errors' => $this->state->getErrors(),
        ];
    }

    abstract public function pageTitle(): string;

    abstract public function redirectUrl(): string;

    protected function action(): ?string
    {
        return null;
    }

    abstract protected function submitLabel(): string;

    /**
     * @return list<FormItem>
     */
    abstract protected function items(): array;

    /**
     * @return array<string, mixed>
     */
    public function getRenderContext(): array
    {
        return [];
    }

    abstract public function submit(): void;
}