From 7d5e516f1c7421c2466a6a072504b4c62d7e080e Mon Sep 17 00:00:00 2001 From: Hemp Date: Mon, 16 Dec 2024 23:49:07 -0600 Subject: [PATCH] wip --- README.md | 29 +++++++++++++++---- src/MachineState.php | 12 -------- src/Machinery.php | 4 +-- ...tateTrait.php => MachineryEnumeration.php} | 13 ++++----- src/MachineryState.php | 12 ++++++++ tests/Fixtures/Status.php | 8 ++--- 6 files changed, 48 insertions(+), 30 deletions(-) delete mode 100644 src/MachineState.php rename src/{MachineStateTrait.php => MachineryEnumeration.php} (55%) create mode 100644 src/MachineryState.php diff --git a/README.md b/README.md index 3d0c2dd..ba99fb5 100644 --- a/README.md +++ b/README.md @@ -17,15 +17,16 @@ composer require hemp/machinery ## Usage -First, create an enumeration that represents the states for your Eloquent model and the valid transitions between them: +First, create an enumeration that represents the states you wish to +model and the valid transitions between them: ```php -use Hemp\Machinery\MachineState; -use Hemp\Machinery\MachineStateTrait; +use Hemp\Machinery\MachineryState; +use Hemp\Machinery\MachineryEnumeration; -enum OrderStatus: string implements MachineState +enum OrderStatus: string implements MachineryState { - use MachineStateTrait; + use MachineryEnumeration; case Processing : 'processing'; case Shipped : 'shipped'; @@ -48,6 +49,24 @@ enum OrderStatus: string implements MachineState } ``` +Now you can use the `OrderStatus` enumeration to manage transitions +between states: + +```php +use Hemp\Machinery\Machinery; + +OrderStatus::Processing->canTransitionTo(OrderStatus::Shipped); // true +OrderStatus::Processing->transitionTo(OrderStatus::Shipped); // state is now 'shipped' + +OrderStatus::Shipped->canTransitionTo(OrderStatus::Delivered); // true +OrderStatus::Shipped->transitionTo(OrderStatus::Delivered); // state is now 'delivered' + +OrderStatus::Delivered->canTransitionTo(OrderStatus::Processing); // +OrderStatus::Delivered->transitionTo(OrderStatus::Processing); // Throws an exception... +``` + +## Using the state machine with Eloquent + Next, add a column to your Eloquent model's `casts` to store the state: ```php diff --git a/src/MachineState.php b/src/MachineState.php deleted file mode 100644 index 13bdf1d..0000000 --- a/src/MachineState.php +++ /dev/null @@ -1,12 +0,0 @@ - null; @@ -16,7 +16,7 @@ public function transitionTo($machineKey, MachineState $state, ?callable $sideEf return $this->{$machineKey}->transitionTo($state, $callable); } - public function canTransitionTo($machineKey, MachineState $state): bool + public function canTransitionTo($machineKey, MachineryState $state): bool { return $this->{$machineKey}->canTransitionTo($state); } diff --git a/src/MachineStateTrait.php b/src/MachineryEnumeration.php similarity index 55% rename from src/MachineStateTrait.php rename to src/MachineryEnumeration.php index 84a2c02..22170f9 100644 --- a/src/MachineStateTrait.php +++ b/src/MachineryEnumeration.php @@ -5,30 +5,29 @@ /** * @method static transitions() */ -trait MachineStateTrait +trait MachineryEnumeration { /** * @throws InvalidStateTransition */ - public function transitionTo(MachineState $state, ?callable $sideEffect = null): MachineState + public function transitionTo(MachineryState $state, ?callable $sideEffect = null): MachineryState { - $callback = $sideEffect ?? fn () => null; - if (! $this->canTransitionTo($state)) { + if (!$this->canTransitionTo($state)) { throw new InvalidStateTransition("Cannot transition from state [{$this->value}] to state [{$state->value}]."); } - $callback(); + call_user_func($sideEffect ?? fn() => null); return $state; } - public function canTransitionTo(MachineState $state): bool + public function canTransitionTo(MachineryState $state): bool { return in_array($state, self::transitions()[$this->value]); } - public function is(MachineState $state): bool + public function is(MachineryState $state): bool { return $this->value === $state->value; } diff --git a/src/MachineryState.php b/src/MachineryState.php new file mode 100644 index 0000000..5c9bc27 --- /dev/null +++ b/src/MachineryState.php @@ -0,0 +1,12 @@ +