Skip to content

Commit

Permalink
Fix magic methods in the StatefulTrait
Browse files Browse the repository at this point in the history
  • 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.
22 changes: 13 additions & 9 deletions src/KPhoen/DoctrineStateMachineBehavior/Entity/StatefulTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace KPhoen\DoctrineStateMachineBehavior\Entity;

use Doctrine\Common\Inflector\Inflector;
use Finite\StateMachine\StateMachine;

/**
Expand All @@ -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]>
*/
Expand Down Expand Up @@ -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)
Expand All @@ -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)));
}
}

0 comments on commit 58ef056

Please sign in to comment.