1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Dflydev\FiniteStateMachine\TransitionEvent; |
6
|
|
|
|
7
|
|
|
use Dflydev\FiniteStateMachine\Contracts\TransitionEventCallback; |
8
|
|
|
use Dflydev\FiniteStateMachine\State\State; |
9
|
|
|
use Dflydev\FiniteStateMachine\Transition\Transition; |
10
|
|
|
|
11
|
|
|
class TransitionEvent |
12
|
|
|
{ |
13
|
|
|
private string $when; |
14
|
|
|
private string $name; |
15
|
|
|
private array $transitionNames; |
16
|
|
|
private array $fromStateNames; |
17
|
|
|
private array $toStateNames; |
18
|
|
|
private TransitionEventCallback $transitionEventCallback; |
19
|
|
|
|
20
|
21 |
|
public function __construct( |
21
|
|
|
string $when, |
22
|
|
|
string $name, |
23
|
|
|
array $transitionNames, |
24
|
|
|
array $fromStateNames, |
25
|
|
|
array $toStateNames, |
26
|
|
|
TransitionEventCallback $transitionEventCallback |
27
|
|
|
) { |
28
|
21 |
|
$this->when = $when; |
29
|
21 |
|
$this->name = $name; |
30
|
21 |
|
$this->transitionNames = $transitionNames; |
31
|
21 |
|
$this->fromStateNames = $fromStateNames; |
32
|
21 |
|
$this->toStateNames = $toStateNames; |
33
|
21 |
|
$this->transitionEventCallback = $transitionEventCallback; |
34
|
21 |
|
} |
35
|
|
|
|
36
|
21 |
|
public function when(): string |
37
|
|
|
{ |
38
|
21 |
|
return $this->when; |
39
|
|
|
} |
40
|
|
|
|
41
|
21 |
|
public function name(): string |
42
|
|
|
{ |
43
|
21 |
|
return $this->name; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function transitionNames(): array |
47
|
|
|
{ |
48
|
|
|
return $this->transitionNames; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function fromStateNames(): array |
52
|
|
|
{ |
53
|
|
|
return $this->fromStateNames; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function toStateNames(): array |
57
|
|
|
{ |
58
|
|
|
return $this->toStateNames; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function transitionEventCallback(): TransitionEventCallback |
62
|
|
|
{ |
63
|
|
|
return $this->transitionEventCallback; |
64
|
|
|
} |
65
|
|
|
|
66
|
7 |
|
public function fireIfMatches( |
67
|
|
|
string $when, |
68
|
|
|
object $object, |
69
|
|
|
Transition $transition, |
70
|
|
|
State $fromState, |
71
|
|
|
State $toState |
72
|
|
|
): void { |
73
|
7 |
|
if ($when !== $this->when) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
7 |
|
$matches = []; |
78
|
|
|
|
79
|
7 |
|
if (count($this->transitionNames) > 0) { |
80
|
7 |
|
$matches[] = in_array($transition->name(), $this->transitionNames); |
81
|
|
|
} |
82
|
|
|
|
83
|
7 |
|
if (count($this->fromStateNames) > 0) { |
84
|
7 |
|
$matches[] = in_array($fromState->name(), $this->fromStateNames); |
85
|
|
|
} |
86
|
|
|
|
87
|
7 |
|
if (count($this->toStateNames) > 0) { |
88
|
|
|
$matches[] = in_array($toState->name(), $this->toStateNames); |
89
|
|
|
} |
90
|
|
|
|
91
|
7 |
|
if (count($matches) === 0) { |
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
7 |
|
if (in_array(false, $matches)) { |
96
|
6 |
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
$this->transitionEventCallback->__invoke($when, $object, $transition, $fromState, $toState); |
100
|
1 |
|
} |
101
|
|
|
} |
102
|
|
|
|