Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/Contracts/StateMachine.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

interface StateMachine
{
/**
* Return current state
*
* @return null|DefineStates&UnitEnum
*/
public function currentState();
public function __construct(HasStateMachine $hasStateMachine, string $field);

public function currentState(): null|(DefineStates&UnitEnum);

public function canBe(DefineStates&UnitEnum $status): bool;

Expand Down
5 changes: 1 addition & 4 deletions src/EnumStateMachine.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ public function __construct(
) {
}

/**
* @return null|DefineStates&UnitEnum
*/
public function currentState()
public function currentState(): null|(DefineStates&UnitEnum)
{
return $this->hasStateMachine->{$this->field};
}
Expand Down
7 changes: 7 additions & 0 deletions src/Providers/EnumataServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

use Illuminate\Support\ServiceProvider;
use Norotaro\Enumata\Console\Commands\ModelStateMakeCommand;
use Norotaro\Enumata\Contracts\StateMachine;
use Norotaro\Enumata\EnumStateMachine;

final class EnumataServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(StateMachine::class, EnumStateMachine::class);
}

public function boot(): void
{
if ($this->app->runningInConsole()) {
Expand Down
19 changes: 7 additions & 12 deletions src/Traits/EloquentHasStateMachines.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Javoscript\MacroableModels\Facades\MacroableModels;
use Norotaro\Enumata\Contracts\DefineStates;
use Norotaro\Enumata\Contracts\Nullable;
use Norotaro\Enumata\Contracts\StateMachine;
use Norotaro\Enumata\Exceptions\TransitionNotAllowedException;
use Norotaro\Enumata\EnumStateMachine;
use ReflectionEnum;

trait EloquentHasStateMachines
Expand Down Expand Up @@ -85,7 +85,7 @@ protected static function guardStateMachine(Model $model): void
{
$model->initEnumata(setDefaultValues: false);

/** @var EnumStateMachine $stateMachine */
/** @var StateMachine $stateMachine */
foreach ($model->getStateMachines() as $stateMachine) {
$field = $stateMachine->getField();

Expand All @@ -100,15 +100,7 @@ protected static function guardStateMachine(Model $model): void
$from = $model->getOriginal($field);
$to = $model->{$field};

$transitions = $from?->transitions();

if (!$transitions && in_array(Nullable::class, class_implements($to))) {
$nullableState = $to;

$transitions = $nullableState->initialTransitions();
}

if (!in_array($to, $transitions ?? [])) {
if (!$stateMachine->canBe($to)) {
throw new TransitionNotAllowedException($from, $to, $stateMachine, self::class);
}
}
Expand Down Expand Up @@ -156,7 +148,10 @@ protected static function itDefineStates($castTo): bool
protected function initStateMachineFor(string $field): void
{
if (!array_key_exists($field, $this->stateMachines)) {
$this->stateMachines[$field] = new EnumStateMachine($this, $field);
$this->stateMachines[$field] = app()->makeWith(StateMachine::class, [
'hasStateMachine' => $this,
'field' => $field,
]);
}
}

Expand Down