1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Finite\Event\Callback; |
4
|
|
|
|
5
|
|
|
use Finite\Event\CallbackHandler; |
6
|
|
|
use Finite\Event\TransitionEvent; |
7
|
|
|
use Finite\StateMachine\StateMachineInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Concrete implementation of CallbackSpecification |
11
|
|
|
* |
12
|
|
|
* @author Yohan Giarelli <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class CallbackSpecification implements CallbackSpecificationInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $specs = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var StateMachineInterface |
23
|
|
|
*/ |
24
|
|
|
private $stateMachine; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param StateMachineInterface $sm |
28
|
|
|
* @param array $from |
29
|
|
|
* @param array $to |
30
|
|
|
* @param array $on |
31
|
|
|
*/ |
32
|
60 |
|
public function __construct(StateMachineInterface $sm, array $from, array $to, array $on) |
33
|
|
|
{ |
34
|
60 |
|
$this->stateMachine = $sm; |
35
|
|
|
|
36
|
|
|
$isExclusion = function ($str) { return 0 === strpos($str, '-'); }; |
37
|
|
|
$removeDash = function ($str) { return substr($str, 1); }; |
38
|
|
|
|
39
|
60 |
|
foreach (array('from', 'to', 'on') as $clause) { |
40
|
60 |
|
$excludedClause = 'excluded_' . $clause; |
41
|
|
|
|
42
|
60 |
|
$this->specs[$excludedClause] = array_filter(${$clause}, $isExclusion); |
43
|
60 |
|
$this->specs[$clause] = array_diff(${$clause}, $this->specs[$excludedClause]); |
44
|
60 |
|
$this->specs[$excludedClause] = array_map($removeDash, $this->specs[$excludedClause]); |
45
|
|
|
|
46
|
|
|
// For compatibility with old CallbackHandler. |
47
|
|
|
// To be removed in 2.0 |
48
|
60 |
|
if (in_array(CallbackHandler::ALL, $this->specs[$clause])) { |
49
|
12 |
|
$this->specs[$clause] = array(); |
50
|
|
|
} |
51
|
48 |
|
} |
52
|
60 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
|
|
*/ |
57
|
40 |
|
public function isSatisfiedBy(TransitionEvent $event) |
58
|
|
|
{ |
59
|
|
|
return |
60
|
40 |
|
$event->getStateMachine() === $this->stateMachine && |
61
|
40 |
|
$this->supportsClause('from', $event->getInitialState()->getName()) && |
62
|
40 |
|
$this->supportsClause('to', $event->getTransition()->getState()) && |
63
|
40 |
|
$this->supportsClause('on', $event->getTransition()->getName()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $clause |
68
|
|
|
* @param string $property |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
40 |
|
private function supportsClause($clause, $property) |
73
|
|
|
{ |
74
|
40 |
|
$excludedClause = 'excluded_' . $clause; |
75
|
|
|
|
76
|
|
|
return |
77
|
40 |
|
(0 === count($this->specs[$clause]) || in_array($property, $this->specs[$clause])) && |
78
|
40 |
|
(0 === count($this->specs[$excludedClause]) || !in_array($property, $this->specs[$excludedClause])); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|