From 795288a23655e64120f2b0e1e40d0a49478fdd01 Mon Sep 17 00:00:00 2001 From: Simon Bigelmayr Date: Tue, 26 Nov 2024 11:43:30 +0100 Subject: [PATCH] direct use of instance instead of class name of model --- app/ModelStates/ModelStates/ModelState.php | 5 ++--- app/ModelStates/OtherModelStates/OtherModelState.php | 5 ++--- src/ModelStates/HasStateManager.php | 7 +++---- src/ModelStates/State.php | 3 +-- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/app/ModelStates/ModelStates/ModelState.php b/app/ModelStates/ModelStates/ModelState.php index 95ec5d5..d14e128 100644 --- a/app/ModelStates/ModelStates/ModelState.php +++ b/app/ModelStates/ModelStates/ModelState.php @@ -22,9 +22,8 @@ public static function defaultState(): ModelState return new StateA(); } - /** @return class-string */ - public static function stateManagerClass(): string + public static function stateManagerClass(): Model { - return StateManager::class; + return new StateManager(); } } diff --git a/app/ModelStates/OtherModelStates/OtherModelState.php b/app/ModelStates/OtherModelStates/OtherModelState.php index 7ce5934..6371d9a 100644 --- a/app/ModelStates/OtherModelStates/OtherModelState.php +++ b/app/ModelStates/OtherModelStates/OtherModelState.php @@ -23,9 +23,8 @@ public static function defaultState(): OtherModelState return new StateX(); } - /** @return class-string */ - public static function stateManagerClass(): string + public static function stateManagerClass(): Model { - return StateManager::class; + return new StateManager(); } } diff --git a/src/ModelStates/HasStateManager.php b/src/ModelStates/HasStateManager.php index e59762e..f2162ac 100644 --- a/src/ModelStates/HasStateManager.php +++ b/src/ModelStates/HasStateManager.php @@ -17,8 +17,7 @@ public static function bootHasStateManager(): void return; } - $stateManagerClass = $self->stateClass()::stateManagerClass(); - $stateManager = new $stateManagerClass(); + $stateManager = $self->stateClass()::stateManagerClass(); assert(in_array(IsStateManager::class, class_uses($stateManager))); $stateManager->setAttribute($stateManager::stateColumnName(), $self->stateClass()::defaultState()::name()); @@ -29,9 +28,9 @@ public static function bootHasStateManager(): void public function getStateAttribute(): State { - $stateManagerClass = $this->stateClass()::stateManagerClass(); - $stateManager = new $stateManagerClass(); + $stateManager = self::stateClass()::stateManagerClass(); assert(in_array(IsStateManager::class, class_uses($stateManager))); + $stateName = $this->stateManager->getAttribute($stateManager::stateColumnName()); assert(is_string($stateName)); diff --git a/src/ModelStates/State.php b/src/ModelStates/State.php index 3bbf52a..c216a5e 100644 --- a/src/ModelStates/State.php +++ b/src/ModelStates/State.php @@ -11,8 +11,7 @@ abstract public static function config(): StateConfig; abstract public static function defaultState(): self; - /** @return class-string */ - abstract public static function stateManagerClass(): string; + abstract public static function stateManagerClass(): Model; public static function name(): string {