Skip to content

Commit

Permalink
Merge pull request #208 from luckcolors/main
Browse files Browse the repository at this point in the history
Removed Model type enforcement and replaced with PHPDoc
  • Loading branch information
freekmurze authored Aug 1, 2022
2 parents b80959d + f053a2c commit 240ca66
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 26 deletions.
16 changes: 11 additions & 5 deletions src/DefaultTransition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

namespace Spatie\ModelStates;

use Illuminate\Database\Eloquent\Model;

class DefaultTransition extends Transition
{
protected Model $model;
protected $model;

protected string $field;

protected State $newState;

/**
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $field
* @param State $newState
*/
public function __construct(
Model $model,
$model,
string $field,
State $newState
) {
Expand All @@ -22,7 +25,10 @@ public function __construct(
$this->newState = $newState;
}

public function handle(): Model
/**
* @return \Illuminate\Database\Eloquent\Model
*/
public function handle()
{
$originalState = $this->model->{$this->field} ? clone $this->model->{$this->field} : null;

Expand Down
11 changes: 8 additions & 3 deletions src/Events/StateChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Spatie\ModelStates\Events;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\SerializesModels;
use Spatie\ModelStates\State;
use Spatie\ModelStates\Transition;
Expand All @@ -17,13 +16,19 @@ class StateChanged

public Transition $transition;

public Model $model;
public $model;

/**
* @param string|State|null $initialState
* @param string|State|null $finalState
* @param Transition $finalState
* @param \Illuminate\Database\Eloquent\Model $model
*/
public function __construct(
?State $initialState,
?State $finalState,
Transition $transition,
Model $model
$model
) {
$this->initialState = $initialState;
$this->finalState = $finalState;
Expand Down
22 changes: 18 additions & 4 deletions src/Exceptions/CouldNotPerformTransition.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
namespace Spatie\ModelStates\Exceptions;

use Exception;
use Illuminate\Database\Eloquent\Model;

class CouldNotPerformTransition extends Exception
{
public static function notAllowed(Model $model, $transitionClass): CouldNotPerformTransition
/**
* @param \Illuminate\Database\Eloquent\Model $model
* @param Transition $transitionClass
* @return CouldNotPerformTransition
*/
public static function notAllowed($model, $transitionClass): CouldNotPerformTransition
{
$modelClass = get_class($model);

Expand All @@ -16,14 +20,24 @@ public static function notAllowed(Model $model, $transitionClass): CouldNotPerfo
return TransitionNotAllowed::make($modelClass, $transitionClass);
}

public static function notFound(string $from, string $to, Model $model): CouldNotPerformTransition
/**
* @param string $from
* @param string $to
* @param \Illuminate\Database\Eloquent\Model $model
* @return CouldNotPerformTransition
*/
public static function notFound(string $from, string $to, $model): CouldNotPerformTransition
{
$modelClass = get_class($model);

return TransitionNotFound::make($from, $to, $modelClass);
}

public static function couldNotResolveTransitionField(Model $model): CouldNotPerformTransition
/**
* @param \Illuminate\Database\Eloquent\Model $model
* @return CouldNotPerformTransition
*/
public static function couldNotResolveTransitionField($model): CouldNotPerformTransition
{
$modelClass = get_class($model);

Expand Down
14 changes: 11 additions & 3 deletions src/Exceptions/InvalidConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
namespace Spatie\ModelStates\Exceptions;

use Exception;
use Illuminate\Database\Eloquent\Model;
use Spatie\ModelStates\HasStates;
use Spatie\ModelStates\State;
use Spatie\ModelStates\Transition;

class InvalidConfig extends Exception
{
public static function fieldNotFound(string $fieldName, Model $model): InvalidConfig
/**
* @param string $fieldName
* @param \Illuminate\Database\Eloquent\Model $model
* @return InvalidConfig
*/
public static function fieldNotFound(string $fieldName, $model): InvalidConfig
{
$modelClass = get_class($model);

Expand All @@ -37,7 +41,11 @@ public static function doesNotExtendBaseClass(string $class, string $baseClass):
return ClassDoesNotExtendBaseClass::make($class, $baseClass);
}

public static function resolveTransitionNotFound(Model $model): InvalidConfig
/**
* @param \Illuminate\Database\Eloquent\Model $model
* @return InvalidConfig
*/
public static function resolveTransitionNotFound($model): InvalidConfig
{
$modelClass = get_class($model);

Expand Down
3 changes: 1 addition & 2 deletions src/HasStates.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Spatie\ModelStates;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
Expand All @@ -14,7 +13,7 @@ trait HasStates

public static function bootHasStates(): void
{
self::creating(function (Model $model) {
self::creating(function ($model) {
/**
* @var \Spatie\ModelStates\HasStates $model
*/
Expand Down
33 changes: 26 additions & 7 deletions src/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Spatie\ModelStates;

use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use JsonSerializable;
use ReflectionClass;
Expand All @@ -14,15 +13,18 @@

abstract class State implements Castable, JsonSerializable
{
private Model $model;
private $model;

private StateConfig $stateConfig;

private string $field;

private static array $stateMapping = [];

public function __construct(Model $model)
/**
* @param \Illuminate\Database\Eloquent\Model $model
*/
public function __construct($model)
{
$this->model = $model;
$this->stateConfig = static::config();
Expand Down Expand Up @@ -95,7 +97,12 @@ public static function resolveStateClass($state): ?string
return $state;
}

public static function make(string $name, Model $model): State
/**
* @param string $name
* @param \Illuminate\Database\Eloquent\Model $model
* @return State
*/
public static function make(string $name, $model): State
{
$stateClass = static::resolveStateClass($name);

Expand All @@ -106,7 +113,10 @@ public static function make(string $name, Model $model): State
return new $stateClass($model);
}

public function getModel(): Model
/**
* @return \Illuminate\Database\Eloquent\Model
*/
public function getModel()
{
return $this->model;
}
Expand All @@ -131,7 +141,12 @@ public function setField(string $field): self
return $this;
}

public function transitionTo($newState, ...$transitionArgs): Model
/**
* @param string|State $newState
* @param mixed ...$transitionArgs
* @return \Illuminate\Database\Eloquent\Model
*/
public function transitionTo($newState, ...$transitionArgs)
{
$newState = $this->resolveStateObject($newState);

Expand All @@ -153,7 +168,11 @@ public function transitionTo($newState, ...$transitionArgs): Model
return $this->transition($transition);
}

public function transition(Transition $transition): Model
/**
* @param Transition $transition
* @return \Illuminate\Database\Eloquent\Model
*/
public function transition(Transition $transition)
{
if (method_exists($transition, 'canTransition')) {
if (! $transition->canTransition()) {
Expand Down
6 changes: 4 additions & 2 deletions tests/Dummy/Transitions/CustomDefaultTransition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Spatie\ModelStates\Tests\Dummy\Transitions;

use Illuminate\Database\Eloquent\Model;
use Spatie\ModelStates\DefaultTransition;

class CustomDefaultTransition extends DefaultTransition
{
public function handle(): Model
/**
* @return \Illuminate\Database\Eloquent\Model
*/
public function handle()
{
$originalState = $this->model->{$this->field} ? clone $this->model->{$this->field} : null;

Expand Down

0 comments on commit 240ca66

Please sign in to comment.