From deacd0dfc195bca41af631114804d29937337cd8 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Wed, 17 Jan 2024 02:11:31 +0900 Subject: . --- services/app/src/Form/FormBase.php | 53 ++++++++++++++++++ services/app/src/Form/FormItem.php | 18 +++++++ services/app/src/Form/FormState.php | 62 ++++++++++++++++++++++ .../src/Form/FormSubmissionFailureException.php | 19 +++++++ 4 files changed, 152 insertions(+) create mode 100644 services/app/src/Form/FormBase.php create mode 100644 services/app/src/Form/FormItem.php create mode 100644 services/app/src/Form/FormState.php create mode 100644 services/app/src/Form/FormSubmissionFailureException.php (limited to 'services/app/src/Form') 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 @@ +, state: array, errors: array} + */ + 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 + */ + abstract protected function items(): array; + + /** + * @return array + */ + 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 @@ + + */ + private array $errors = []; + + /** + * @param array $params + */ + public function __construct(private readonly array $params = []) + { + } + + public static function fromRequest(ServerRequestInterface $request): self + { + return new self((array)$request->getParsedBody()); + } + + /** + * @return array + */ + 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 $errors + */ + public function setErrors(array $errors): self + { + $this->errors = $errors; + return $this; + } + + /** + * @return array + */ + 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 @@ +