62 lines
1.7 KiB
PHP
62 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Framework\Http\Session;
|
|
|
|
/**
|
|
* This class provides functionality to store, retrieve, and manage form data
|
|
* in a session-based storage.
|
|
*/
|
|
final readonly class FormDataStorage
|
|
{
|
|
public function __construct(
|
|
private Session $session
|
|
){
|
|
// Sicherstellen, dass Formulardatenbereich existiert
|
|
if (!$this->session->has(SessionKey::FORM_DATA->value)) {
|
|
$this->session->set(SessionKey::FORM_DATA->value, []);
|
|
}
|
|
}
|
|
|
|
public function store(string $formId, array $data): void
|
|
{
|
|
$formData = $this->session->get(SessionKey::FORM_DATA->value, []);
|
|
$formData[$formId] = $data;
|
|
$this->session->set(SessionKey::FORM_DATA->value, $formData);
|
|
}
|
|
|
|
public function get(string $formId): array
|
|
{
|
|
$formData = $this->session->get(SessionKey::FORM_DATA->value, []);
|
|
return $formData[$formId] ?? [];
|
|
}
|
|
|
|
public function getField(string $formId, string $field, $default = null)
|
|
{
|
|
$data = $this->get($formId);
|
|
return $data[$field] ?? $default;
|
|
}
|
|
|
|
public function has(string $formId): bool
|
|
{
|
|
$formData = $this->session->get(SessionKey::FORM_DATA->value, []);
|
|
return !empty($formData[$formId]);
|
|
}
|
|
|
|
public function clear(string $formId): void
|
|
{
|
|
$formData = $this->session->get(SessionKey::FORM_DATA->value, []);
|
|
|
|
if (isset($formData[$formId])) {
|
|
unset($formData[$formId]);
|
|
$this->session->set(SessionKey::FORM_DATA->value, $formData);
|
|
}
|
|
}
|
|
|
|
public function clearAll(): void
|
|
{
|
|
$this->session->set(SessionKey::FORM_DATA->value, []);
|
|
}
|
|
}
|