aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/app/src/Form
diff options
context:
space:
mode:
Diffstat (limited to 'services/app/src/Form')
-rw-r--r--services/app/src/Form/FormBase.php53
-rw-r--r--services/app/src/Form/FormItem.php18
-rw-r--r--services/app/src/Form/FormState.php62
-rw-r--r--services/app/src/Form/FormSubmissionFailureException.php19
4 files changed, 152 insertions, 0 deletions
diff --git a/services/app/src/Form/FormBase.php b/services/app/src/Form/FormBase.php
new file mode 100644
index 0000000..cc29442
--- /dev/null
+++ b/services/app/src/Form/FormBase.php
@@ -0,0 +1,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;
+}
diff --git a/services/app/src/Form/FormItem.php b/services/app/src/Form/FormItem.php
new file mode 100644
index 0000000..47fe9c9
--- /dev/null
+++ b/services/app/src/Form/FormItem.php
@@ -0,0 +1,18 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Albatross\Form;
+
+final class FormItem
+{
+ public function __construct(
+ public readonly string $name,
+ public readonly string $type,
+ public readonly ?string $label = null,
+ public readonly bool $isRequired = false,
+ public readonly bool $isDisabled = false,
+ public readonly string $extra = '',
+ ) {
+ }
+}
diff --git a/services/app/src/Form/FormState.php b/services/app/src/Form/FormState.php
new file mode 100644
index 0000000..e56e8f0
--- /dev/null
+++ b/services/app/src/Form/FormState.php
@@ -0,0 +1,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;
+ }
+}
diff --git a/services/app/src/Form/FormSubmissionFailureException.php b/services/app/src/Form/FormSubmissionFailureException.php
new file mode 100644
index 0000000..0aa2c90
--- /dev/null
+++ b/services/app/src/Form/FormSubmissionFailureException.php
@@ -0,0 +1,19 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Albatross\Form;
+
+use RuntimeException;
+use Throwable;
+
+final class FormSubmissionFailureException extends RuntimeException
+{
+ public function __construct(
+ string $message = '',
+ int $code = 400,
+ Throwable $previous = null,
+ ) {
+ parent::__construct($message, $code, $previous);
+ }
+}