Skip to content

Commit

Permalink
Merge pull request #26 from winzou/callback
Browse files Browse the repository at this point in the history
Allow callback to call object method
  • Loading branch information
winzou authored Oct 27, 2016
2 parents 4cab310 + 54c78de commit fdba189
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ $config = array(
'to-cancelled' => array(
'to' => array('cancelled'), // Will be called only for transitions going to this state
'do' => function() { var_dump('to cancel transition'); }
)
),
'cancel-date' => array(
'to' => array('cancelled'),
'do' => array('object', 'setCancelled'),
),
)
)
);
Expand Down
5 changes: 5 additions & 0 deletions examples/DomainObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public function setStateB($state)
{
$this->stateB = $state;
}

public function setConfirmedNow()
{
var_dump('I (the object) am set confirmed at '.date('Y-m-d').'.');
}
}
7 changes: 6 additions & 1 deletion examples/simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@
'to-cancelled' => array(
'to' => array('cancelled'), // Will be called only for transitions going to this state
'do' => function() { var_dump('to cancel transition'); }
)
),
'confirm-date' => array(
'on' => array('confirm'),
'do' => array('object', 'setConfirmedNow'), // 'object' will be replaced by the object undergoing the transition
),
)
)
);
Expand Down Expand Up @@ -92,6 +96,7 @@

// Return true, this transition is applied
// In addition, callback 'on-confirm' is called
// And callback 'confirm-date' calls the method 'setConfirmedNow' on the object itself
var_dump($stateMachine->apply('confirm'));

// Current state is confirmed
Expand Down
28 changes: 27 additions & 1 deletion src/SM/Callback/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use SM\Event\TransitionEvent;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\PropertyAccess\PropertyAccessor;

class Callback implements CallbackInterface
{
Expand Down Expand Up @@ -65,7 +66,9 @@ function($arg) use($expr, $event) {
);
}

return call_user_func_array($this->callable, $args);
$callable = $this->filterCallable($this->callable, $event);

return call_user_func_array($callable, $args);
}

/**
Expand Down Expand Up @@ -111,4 +114,27 @@ protected function isSatisfiedByClause($clause, $value)

return true;
}

/**
* @param callable|array $callable A callable or array with index 0 starting with "object" that will evaluated as a property path with "object" being the object undergoing the transition
* @param TransitionEvent $event
*
* @return callable
*/
protected function filterCallable($callable, TransitionEvent $event)
{
if (is_array($callable) && isset($callable[0]) && is_string($callable[0]) && 'object' === substr($callable[0], 0, 6)) {
$object = $event->getStateMachine()->getObject();

// callable could be "object.property" and not just "object", so we evaluate the "property" path
if ('object' !== $callable[0]) {
$accessor = new PropertyAccessor();
$object = $accessor->getValue($object, substr($callable[0], 7));
}

return array($object, $callable[1]);
}

return $callable;
}
}

0 comments on commit fdba189

Please sign in to comment.