From aba476607d40049e91a904099f271d4df3aa30f3 Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 24 Jul 2026 15:22:26 +0200 Subject: [PATCH] Encode in session: notification level as string + wizard context as array --- src/Data/NotificationData.php | 12 ++++++++++- .../Commands/Wizards/IsWizardCommand.php | 12 ++++++----- .../Commands/Wizards/WizardCommandContext.php | 21 +++++++++++++++++-- src/Utils/SharpNotification.php | 10 ++++----- tests/Http/UpdateAssetsControllerTest.php | 2 +- tests/TestCase.php | 1 + 6 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/Data/NotificationData.php b/src/Data/NotificationData.php index 540e35442..2bf199025 100644 --- a/src/Data/NotificationData.php +++ b/src/Data/NotificationData.php @@ -11,8 +11,18 @@ final class NotificationData extends Data { public function __construct( public string $title, - public NotificationLevel $level, + public ?NotificationLevel $level, public ?string $message, public bool $autoHide, ) {} + + public static function from(array $notification): self + { + return new self( + title: $notification['title'], + level: NotificationLevel::tryFrom($notification['level']), + message: $notification['message'] ?? null, + autoHide: $notification['autoHide'] ?? true, + ); + } } diff --git a/src/EntityList/Commands/Wizards/IsWizardCommand.php b/src/EntityList/Commands/Wizards/IsWizardCommand.php index 930f7e245..0e1e88e97 100644 --- a/src/EntityList/Commands/Wizards/IsWizardCommand.php +++ b/src/EntityList/Commands/Wizards/IsWizardCommand.php @@ -17,10 +17,12 @@ trait IsWizardCommand protected function getWizardContext(): WizardCommandContext { if (! $this->wizardCommandContext) { - $this->wizardCommandContext = session()->get(sprintf('CWC.%s.%s', get_class($this), $this->getKey())); - if (! $this->wizardCommandContext) { - $this->wizardCommandContext = new WizardCommandContext(); - } + $context = session()->get(sprintf('CWC.%s.%s', get_class($this), $this->getKey())); + $this->wizardCommandContext = match (true) { + $context instanceof WizardCommandContext => $context, + is_array($context) => WizardCommandContext::fromArray($context), + default => new WizardCommandContext(), + }; } return $this->wizardCommandContext; @@ -29,7 +31,7 @@ protected function getWizardContext(): WizardCommandContext protected function toStep(string $step): array { if ($this->wizardCommandContext) { - session()->put(sprintf('CWC.%s.%s', get_class($this), $this->getKey()), $this->wizardCommandContext); + session()->put(sprintf('CWC.%s.%s', get_class($this), $this->getKey()), $this->wizardCommandContext->toArray()); } return [ diff --git a/src/EntityList/Commands/Wizards/WizardCommandContext.php b/src/EntityList/Commands/Wizards/WizardCommandContext.php index b073b726e..2a3a24ba9 100644 --- a/src/EntityList/Commands/Wizards/WizardCommandContext.php +++ b/src/EntityList/Commands/Wizards/WizardCommandContext.php @@ -3,11 +3,14 @@ namespace Code16\Sharp\EntityList\Commands\Wizards; use Code16\Sharp\Exceptions\Commands\SharpInvalidStepException; +use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Validation\Factory as Validator; -class WizardCommandContext +class WizardCommandContext implements Arrayable { - protected array $attributes = []; + public function __construct( + protected array $attributes = [], + ) {} public function setCurrentStep(string $step): self { @@ -42,4 +45,18 @@ class_basename(get_class($this)), ); } } + + public static function fromArray(array $data): static + { + return new static( + attributes: $data['attributes'] ?? [], + ); + } + + public function toArray(): array + { + return [ + 'attributes' => $this->attributes, + ]; + } } diff --git a/src/Utils/SharpNotification.php b/src/Utils/SharpNotification.php index 07e42abcc..f1131f428 100644 --- a/src/Utils/SharpNotification.php +++ b/src/Utils/SharpNotification.php @@ -15,7 +15,7 @@ public function __construct(string $title) $notifications[$this->id] = [ 'title' => $title, - 'level' => NotificationLevel::Info, + 'level' => NotificationLevel::Info->value, 'message' => null, 'autoHide' => true, ]; @@ -33,28 +33,28 @@ public function setDetail(string $detail): self public function setLevelSuccess(): self { return $this->update([ - 'level' => NotificationLevel::Success, + 'level' => NotificationLevel::Success->value, ]); } public function setLevelInfo(): self { return $this->update([ - 'level' => NotificationLevel::Info, + 'level' => NotificationLevel::Info->value, ]); } public function setLevelWarning(): self { return $this->update([ - 'level' => NotificationLevel::Warning, + 'level' => NotificationLevel::Warning->value, ]); } public function setLevelDanger(): self { return $this->update([ - 'level' => NotificationLevel::Danger, + 'level' => NotificationLevel::Danger->value, ]); } diff --git a/tests/Http/UpdateAssetsControllerTest.php b/tests/Http/UpdateAssetsControllerTest.php index f423cacf1..58270204e 100644 --- a/tests/Http/UpdateAssetsControllerTest.php +++ b/tests/Http/UpdateAssetsControllerTest.php @@ -30,5 +30,5 @@ $notifications = session('sharp_notifications'); $notification = collect($notifications)->first(); expect($notification['title'])->toBe('Assets updated successfully') - ->and($notification['level']->value)->toBe('success'); + ->and($notification['level'])->toBe('success'); }); diff --git a/tests/TestCase.php b/tests/TestCase.php index 632ac2fc9..4785b8ab7 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -37,6 +37,7 @@ public function defineEnvironment($app): void $app['config']->set('view.cache', false); $app['config']->set('inertia.testing.page_paths', [__DIR__.'/../resources/js/Pages']); $app['config']->set('database.default', 'testing'); + $app['config']->set('session.serialization', 'json'); $app['view']->addNamespace('fixtures', __DIR__.'/Fixtures/resources/views');