From 58ef056f6744ce8b2d23d32e8bae6b7cb3a6f603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Mon, 16 Feb 2015 11:51:40 +0100 Subject: [PATCH] Fix magic methods in the StatefulTrait --- .../Entity/StatefulTrait.php | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/KPhoen/DoctrineStateMachineBehavior/Entity/StatefulTrait.php b/src/KPhoen/DoctrineStateMachineBehavior/Entity/StatefulTrait.php index 5c4b214..482180a 100644 --- a/src/KPhoen/DoctrineStateMachineBehavior/Entity/StatefulTrait.php +++ b/src/KPhoen/DoctrineStateMachineBehavior/Entity/StatefulTrait.php @@ -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 */ @@ -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))); } }