|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\Http\Session; |
| 6 | + |
| 7 | +use Tempest\Container\Singleton; |
| 8 | +use Tempest\Validation\FailingRule; |
| 9 | + |
| 10 | +/** |
| 11 | + * Manages form validation errors and original input values in the session. |
| 12 | + */ |
| 13 | +#[Singleton] |
| 14 | +final readonly class FormSession |
| 15 | +{ |
| 16 | + private const string VALIDATION_ERRORS_KEY = '#validation_errors'; |
| 17 | + private const string ORIGINAL_VALUES_KEY = '#original_values'; |
| 18 | + |
| 19 | + public function __construct( |
| 20 | + private Session $session, |
| 21 | + ) {} |
| 22 | + |
| 23 | + /** |
| 24 | + * Stores validation errors for the next request. |
| 25 | + * |
| 26 | + * @param array<string,FailingRule[]> $errors |
| 27 | + */ |
| 28 | + public function setErrors(array $errors): void |
| 29 | + { |
| 30 | + $this->session->flash(self::VALIDATION_ERRORS_KEY, $errors); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Gets all validation errors. |
| 35 | + * |
| 36 | + * @return array<string,FailingRule[]> |
| 37 | + */ |
| 38 | + public function getErrors(): array |
| 39 | + { |
| 40 | + return $this->session->get(self::VALIDATION_ERRORS_KEY, []); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Gets validation errors for a specific field. |
| 45 | + * |
| 46 | + * @return FailingRule[] |
| 47 | + */ |
| 48 | + public function getErrorsFor(string $field): array |
| 49 | + { |
| 50 | + return $this->getErrors()[$field] ?? []; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Checks if there are any validation errors. |
| 55 | + */ |
| 56 | + public function hasErrors(): bool |
| 57 | + { |
| 58 | + return $this->getErrors() !== []; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Checks if a specific field has validation errors. |
| 63 | + */ |
| 64 | + public function hasError(string $field): bool |
| 65 | + { |
| 66 | + return $this->getErrorsFor($field) !== []; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Stores each field's original form values for the next request. |
| 71 | + * |
| 72 | + * @param array<string,mixed> $values |
| 73 | + */ |
| 74 | + public function setOriginalValues(array $values): void |
| 75 | + { |
| 76 | + $this->session->flash(self::ORIGINAL_VALUES_KEY, $values); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Gets all original form values. The keys are the form fields. |
| 81 | + * |
| 82 | + * @return array<string,mixed> |
| 83 | + */ |
| 84 | + public function values(): array |
| 85 | + { |
| 86 | + return $this->session->get(self::ORIGINAL_VALUES_KEY, []); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Gets the original value for a specific field. |
| 91 | + */ |
| 92 | + public function getOriginalValueFor(string $field, mixed $default = null): mixed |
| 93 | + { |
| 94 | + return $this->values()[$field] ?? $default; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Clears all validation errors and original values. |
| 99 | + */ |
| 100 | + public function clear(): void |
| 101 | + { |
| 102 | + $this->session->remove(self::VALIDATION_ERRORS_KEY); |
| 103 | + $this->session->remove(self::ORIGINAL_VALUES_KEY); |
| 104 | + } |
| 105 | +} |
0 commit comments