Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get transitionable states from state on Model #17

Merged
merged 8 commits into from
Oct 28, 2019
15 changes: 15 additions & 0 deletions src/HasStates.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ public function transitionTo($state, string $field = null)
$this->{$field}->transitionTo($state);
}

/**
* @param \Spatie\ModelStates\State|string $state
* @param string $fromClass
*/
public function getAllowedTransitionsFrom($state, string $fromClass)
bjrnblm marked this conversation as resolved.
Show resolved Hide resolved
{
if (array_key_exists($state, static::getStateConfig())) {
bjrnblm marked this conversation as resolved.
Show resolved Hide resolved
$stateConfig = static::getStateConfig()[$state];

return $stateConfig->allowedTransitionsFrom($this, $fromClass);
}

throw InvalidConfig::unknownState($state, $this);
}

/**
* @param string $fromClass
* @param string $toClass
Expand Down
19 changes: 19 additions & 0 deletions src/StateConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ public function allowTransitions(array $transitions): StateConfig
return $this;
}

/**
* @param \Illuminate\Database\Eloquent\Model $model
bjrnblm marked this conversation as resolved.
Show resolved Hide resolved
* @param string $fromClass
*/
public function allowedTransitionsFrom(Model $model, string $fromClass)
bjrnblm marked this conversation as resolved.
Show resolved Hide resolved
{
$allowedTransitionsFrom = [];

foreach ($this->allowedTransitions as $allowedTransition => $value) {
list($from, $to) = explode('-', $allowedTransition);
bjrnblm marked this conversation as resolved.
Show resolved Hide resolved

if ($from === $fromClass) {
$allowedTransitionsFrom[] = get_class($this->stateClass::make($to, $model));
bjrnblm marked this conversation as resolved.
Show resolved Hide resolved
}
}

return $allowedTransitionsFrom;
}

/**
* @param string $from
* @param string $to
Expand Down
24 changes: 24 additions & 0 deletions tests/StateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Spatie\ModelStates\Tests\Dummy\States\Paid;
use Spatie\ModelStates\Exceptions\InvalidConfig;
use Spatie\ModelStates\Tests\Dummy\States\Created;
use Spatie\ModelStates\Tests\Dummy\States\Failed;
use Spatie\ModelStates\Tests\Dummy\States\Pending;
use Spatie\ModelStates\Tests\Dummy\States\PaymentState;
use Spatie\ModelStates\Tests\Dummy\States\PaidWithoutName;
Expand Down Expand Up @@ -254,4 +255,27 @@ public function to_json_works_properly()
$payment->toJson()
);
}

/** @test */
public function allowed_transactions_from_existing_state()
{
$payment = new Payment();

$allowedTransitions = $payment->getAllowedTransitionsFrom('state', Created::class);

$this->assertEquals(
$allowedTransitions,
[Pending::class, Failed::class]
);
}

/** @test */
public function allowed_transactions_from_non_existing_state()
{
$this->expectException(InvalidConfig::class);

$payment = new Payment();

$allowedTransitions = $payment->getAllowedTransitionsFrom('wrong', Created::class);
}
}