Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional data can be used when getting the state for a transition #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions spec/SM/Callback/CascadeTransitionCallbackSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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');
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion spec/SM/StateMachine/StateMachineSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions src/SM/Callback/CascadeTransitionCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
}
11 changes: 9 additions & 2 deletions src/SM/StateMachine/StateMachine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');

Expand Down Expand Up @@ -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
*
Expand Down
3 changes: 2 additions & 1 deletion src/SM/StateMachine/StateMachineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down