-
Notifications
You must be signed in to change notification settings - Fork 74
/
Callback.php
144 lines (123 loc) · 4.12 KB
/
Callback.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/*
* This file is part of the StateMachine package.
*
* (c) Alexandre Bacco
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SM\Callback;
use SM\Event\TransitionEvent;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\PropertyAccess\PropertyAccessor;
class Callback implements CallbackInterface
{
/**
* @var array
*/
protected $specs;
/**
* @var mixed
*/
protected $callable;
/**
* @param array $specs Specification for the Callback to be called
* @param mixed $callable Closure or Callable that will be called if specifications pass
*/
public function __construct(array $specs, $callable)
{
foreach (array('from', 'to', 'on', 'excluded_from', 'excluded_to', 'excluded_on') as $clause) {
if (!isset($specs[$clause])) {
$specs[$clause] = array();
} elseif (!is_array($specs[$clause])) {
$specs[$clause] = array($specs[$clause]);
}
}
$this->specs = $specs;
$this->callable = $callable;
}
/**
* @param TransitionEvent $event
*
* @return mixed The returned value from the callback
*/
public function call(TransitionEvent $event)
{
if (!isset($this->specs['args'])) {
$args = array($event);
} else {
$expr = new ExpressionLanguage();
$args = array_map(
function($arg) use($expr, $event) {
if (!is_string($arg)) {
return $arg;
}
return $expr->evaluate($arg, array(
'object' => $event->getStateMachine()->getObject(),
'event' => $event
));
}, $this->specs['args']
);
}
$callable = $this->filterCallable($this->callable, $event);
return call_user_func_array($callable, $args);
}
/**
* {@inheritDoc}
*/
public function __invoke(TransitionEvent $event)
{
if ($this->isSatisfiedBy($event)) {
return $this->call($event);
}
return true;
}
/**
* {@inheritDoc}
*/
public function isSatisfiedBy(TransitionEvent $event): bool
{
$config = $event->getConfig();
return
$this->isSatisfiedByClause('on', $event->getTransition())
&& $this->isSatisfiedByClause('from', $event->getState())
&& $this->isSatisfiedByClause('to', $config['to'])
;
}
/**
* @param string $clause The clause to check (on, from or to)
* @param string $value The value to check the clause against
*
* @return bool
*/
protected function isSatisfiedByClause(string $clause, string $value): bool
{
if (0 < count($this->specs[$clause]) && !in_array($value, $this->specs[$clause])) {
return false;
}
if (0 < count($this->specs['excluded_'.$clause]) && in_array($value, $this->specs['excluded_'.$clause])) {
return false;
}
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): callable
{
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;
}
}