diff --git a/spec/SM/Callback/CascadeTransitionCallbackSpec.php b/spec/SM/Callback/CascadeTransitionCallbackSpec.php index 101839c..dcfa592 100644 --- a/spec/SM/Callback/CascadeTransitionCallbackSpec.php +++ b/spec/SM/Callback/CascadeTransitionCallbackSpec.php @@ -26,7 +26,7 @@ function it_applies($factory, TransitionEvent $event, DummyObject $object, State $factory->get($object, 'graph')->willReturn($sm); $sm->can('transition')->willReturn(true); - $sm->apply('transition', true)->shouldBeCalled(); + $sm->apply('transition', array(), true)->shouldBeCalled(); $this->apply($object, $event, 'transition', 'graph'); } @@ -45,7 +45,7 @@ function it_applies_with_default_graph( $factory->get($object, 'graph')->willReturn($sm1); $sm1->can('transition')->willReturn(true); - $sm1->apply('transition', true)->shouldBeCalled(); + $sm1->apply('transition', array(), true)->shouldBeCalled(); $this->apply($object, $event, 'transition'); } @@ -65,7 +65,7 @@ function it_applies_with_default_graph_and_default_transition( $factory->get($object, 'graph')->willReturn($sm1); $sm1->can('transition')->willReturn(true); - $sm1->apply('transition', true)->shouldBeCalled(); + $sm1->apply('transition', array(), true)->shouldBeCalled(); $this->apply($object, $event); } diff --git a/spec/SM/StateMachine/StateMachineSpec.php b/spec/SM/StateMachine/StateMachineSpec.php index 451fc2b..ba7176a 100644 --- a/spec/SM/StateMachine/StateMachineSpec.php +++ b/spec/SM/StateMachine/StateMachineSpec.php @@ -128,7 +128,7 @@ function it_does_nothing_if_transition_cannot_be_applied_in_soft_mode($object, $ $dispatcher->dispatch(Argument::any())->shouldNotBeCalled(); - $this->apply('confirm', true); + $this->apply('confirm', array(), true); } function it_throws_an_exception_if_transition_doesnt_exist_on_apply() diff --git a/src/SM/Callback/CascadeTransitionCallback.php b/src/SM/Callback/CascadeTransitionCallback.php index 366f1a9..1422c12 100644 --- a/src/SM/Callback/CascadeTransitionCallback.php +++ b/src/SM/Callback/CascadeTransitionCallback.php @@ -41,9 +41,10 @@ public function __construct(FactoryInterface $factory) * @param TransitionEvent $event Transition event * @param string|null $transition Transition that is to be applied (if null, same as the trigger) * @param string|null $graph Graph on which the new transition will apply (if null, same as the trigger) + * @param array $data Additional data to use when getting state * @param bool $soft If true, check if it can apply the transition first (no Exception thrown) */ - public function apply($objects, TransitionEvent $event, $transition = null, $graph = null, $soft = true) + public function apply($objects, TransitionEvent $event, $transition = null, $graph = null, array $data = array(), $soft = true) { if (!is_array($objects) && !$objects instanceof \Traversable) { $objects = array($objects); @@ -58,7 +59,7 @@ public function apply($objects, TransitionEvent $event, $transition = null, $gra } foreach ($objects as $object) { - $this->factory->get($object, $graph)->apply($transition, $soft); + $this->factory->get($object, $graph)->apply($transition, $data, $soft); } } } diff --git a/src/SM/StateMachine/StateMachine.php b/src/SM/StateMachine/StateMachine.php index d00164e..9af1884 100644 --- a/src/SM/StateMachine/StateMachine.php +++ b/src/SM/StateMachine/StateMachine.php @@ -111,7 +111,7 @@ public function can($transition) /** * {@inheritDoc} */ - public function apply($transition, $soft = false) + public function apply($transition, array $data = array(), $soft = false) { if (!$this->can($transition)) { if ($soft) { @@ -135,7 +135,9 @@ public function apply($transition, $soft = false) $this->callCallbacks($event, 'before'); - $this->setState($this->config['transitions'][$transition]['to']); + $this->setState( + $this->getStateForTransition($transition, $data) + ); $this->callCallbacks($event, 'after'); @@ -182,6 +184,11 @@ public function getPossibleTransitions() ); } + protected function getStateForTransition($transition, array $data = array()) + { + return $this->config['transitions'][$transition]['to']; + } + /** * Set a new state to the underlying object * diff --git a/src/SM/StateMachine/StateMachineInterface.php b/src/SM/StateMachine/StateMachineInterface.php index 1c614b7..d4ae0fa 100644 --- a/src/SM/StateMachine/StateMachineInterface.php +++ b/src/SM/StateMachine/StateMachineInterface.php @@ -30,13 +30,14 @@ public function can($transition); * Applies the transition on the underlying object * * @param string $transition Transition to apply + * @param array $data Additional data to use when getting state * @param bool $soft Soft means do nothing if transition can't be applied (no exception thrown) * * @return bool If the transition has been applied or not (in case of soft apply) * * @throws SMException If transition can't be applied or doesn't exist */ - public function apply($transition, $soft = false); + public function apply($transition, array $data = array(), $soft = false); /** * Returns the current state