diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-01-17 02:11:31 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-01-17 02:11:31 +0900 |
| commit | deacd0dfc195bca41af631114804d29937337cd8 (patch) | |
| tree | f1f83580e5bc892c0794ac41632bc0cce3498f65 /services/app/src/Form/FormState.php | |
| parent | 38ddeb28ec846ee966d0fe6873585d697a9ef373 (diff) | |
| download | phperkaigi-2024-albatross-deacd0dfc195bca41af631114804d29937337cd8.tar.gz phperkaigi-2024-albatross-deacd0dfc195bca41af631114804d29937337cd8.tar.zst phperkaigi-2024-albatross-deacd0dfc195bca41af631114804d29937337cd8.zip | |
.
Diffstat (limited to 'services/app/src/Form/FormState.php')
| -rw-r--r-- | services/app/src/Form/FormState.php | 62 |
1 files changed, 62 insertions, 0 deletions
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; + } +} |
