From 3ac7509499a41b95f968fa86749be9018b5b3597 Mon Sep 17 00:00:00 2001 From: asantibanez Date: Tue, 22 Dec 2020 12:24:31 -0500 Subject: [PATCH] Added `snake_case()` and `camelCase()` method for state machine field --- CHANGELOG.md | 4 ++++ README.md | 6 ++++-- src/Traits/HasStateMachines.php | 6 +++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03f5b53..1c1efc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `laravel-eloquent-state-machines` will be documented in this file +## v2.2.1 - 2020-12-22 + +- Added `snake_case()` and `camelCase()` method for state machine field. + ## v2.2.0 - 2020-12-21 - Added macros on query builder to interact with `state_history` diff --git a/README.md b/README.md index ce249c9..61baea1 100644 --- a/README.md +++ b/README.md @@ -194,12 +194,14 @@ one method per each field mapped in `$stateMachines`. Eg. For ```php -'status' => StatusStateMachine::class +'status' => StatusStateMachine::class, +'fulfillment_status' => FulfillmentStatusStateMachine::class ``` We will have an accompanying method ```php -status() +status(); +fulfillment_status(); // or fulfillmentStatus() ``` with which we can use to check our current state, history and apply transitions. diff --git a/src/Traits/HasStateMachines.php b/src/Traits/HasStateMachines.php index 7c7c186..7af95f0 100644 --- a/src/Traits/HasStateMachines.php +++ b/src/Traits/HasStateMachines.php @@ -23,6 +23,11 @@ public static function bootHasStateMachines() collect($model->stateMachines) ->each(function ($_, $field) use ($model) { + MacroableModels::addMacro(static::class, $field, function () use ($field) { + $stateMachine = new $this->stateMachines[$field]($field, $this); + return new State($this->{$stateMachine->field}, $stateMachine); + }); + $camelField = Str::of($field)->camel(); MacroableModels::addMacro(static::class, $camelField, function () use ($field) { @@ -30,7 +35,6 @@ public static function bootHasStateMachines() return new State($this->{$stateMachine->field}, $stateMachine); }); - $studlyField = Str::of($field)->studly(); Builder::macro("whereHas{$studlyField}", function ($callable) use ($field) {