-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix magic methods in the StatefulTrait
- Loading branch information
Kévin Gomez
committed
Feb 16, 2015
1 parent
cf61559
commit 58ef056
Showing
1 changed file
with
13 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace KPhoen\DoctrineStateMachineBehavior\Entity; | ||
|
||
use Doctrine\Common\Inflector\Inflector; | ||
use Finite\StateMachine\StateMachine; | ||
|
||
/** | ||
|
@@ -10,6 +11,7 @@ | |
* Expose magic methods based on the transition allowed by te state-machine: | ||
* * {TransitionName}(): apply the transition {TransitionName} ; | ||
* * can{TransitionName}(): test if the transition {TransitionName} can be applied. | ||
* * is{StateName}(): test if the state {StateName} is the current one. | ||
* | ||
* @author Kévin Gomez <[email protected]> | ||
*/ | ||
|
@@ -51,11 +53,10 @@ public function can($transition) | |
{ | ||
return $this->stateMachine->can($transition); | ||
} | ||
|
||
/** | ||
* | ||
* @param string $transition to be applied | ||
* | ||
* | ||
* @return mixed | ||
*/ | ||
public function apply($transition) | ||
|
@@ -70,16 +71,19 @@ public function __call($method, $arguments) | |
} | ||
|
||
$transitions = array_flip($this->stateMachine->getTransitions()); | ||
$states = array_flip($this->stateMachine->getStates()); | ||
$states = array_flip($this->stateMachine->getStates()); | ||
|
||
if (substr($method, 0, 3) === 'can' && isset($transitions[strtolower(substr($method, 3))])) { | ||
return $this->stateMachine->can(strtolower(substr($method, 3))); | ||
} elseif (substr($method, 0, 2) === 'is' && isset($states[strtolower(substr($method, 2))])) { | ||
return $this->stateMachine->getCurrentState()->getName() === strtolower(substr($method, 2)); | ||
$transition = Inflector::tableize(substr($method, 3)); | ||
$state = Inflector::tableize(substr($method, 2)); | ||
|
||
if (substr($method, 0, 3) === 'can' && isset($transitions[$transition])) { | ||
return $this->stateMachine->can($transition); | ||
} elseif (substr($method, 0, 2) === 'is' && isset($states[$state])) { | ||
return $this->stateMachine->getCurrentState()->getName() === $state; | ||
} elseif (isset($transitions[$method])) { | ||
return $this->stateMachine->apply($method); | ||
} | ||
|
||
throw new \BadMethodCallException(sprintf('The method "::%s()" on class "%s" does not exist.', $method, get_class($this))); | ||
} | ||
} |