1
|
|
|
<?php |
2
|
|
|
namespace izzum\statemachine\loader; |
3
|
|
|
use izzum\statemachine\StateMachine; |
4
|
|
|
use izzum\statemachine\Exception; |
5
|
|
|
use izzum\statemachine\State; |
6
|
|
|
use izzum\statemachine\utils\Utils; |
7
|
|
|
use izzum\statemachine\Transition; |
8
|
|
|
use izzum\statemachine\persistence\PDO; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* LoaderArray supports configuration of a StateMachine by loading it from |
12
|
|
|
* an array of Transition instances. |
13
|
|
|
* |
14
|
|
|
* The LoaderArray should always be the object that is used by other Loader |
15
|
|
|
* implementations. |
16
|
|
|
* This means other LoaderInterface implementations should delegate the loading |
17
|
|
|
* to this class. You could implement the Loader interface and |
18
|
|
|
* use object composition for the LoaderArray. |
19
|
|
|
* |
20
|
|
|
* |
21
|
|
|
* A transition will be unique per StateMachine and is uniquely defined by the |
22
|
|
|
* tuple of a starting state name and a destination state name. |
23
|
|
|
* The data of the rule, command or the identity (===) of the states does not |
24
|
|
|
* matter when a Transition is added to a StateMachine. The machine will react |
25
|
|
|
* on a first come first serve basis. In short, just make sure your |
26
|
|
|
* configuration data is ok. |
27
|
|
|
* |
28
|
|
|
* TRICKY: if multiple transitions share the same State object (for example as |
29
|
|
|
* their origin/from state or their destination/to state) then at least make |
30
|
|
|
* sure that those states share the exact same data. |
31
|
|
|
* Ideally, they should point to the same State instance. |
32
|
|
|
* Otherwise, transitions and states are actually stored on the statemachine |
33
|
|
|
* on a first wins basis (the later transition/state instance is not stored). |
34
|
|
|
* |
35
|
|
|
* Transitions will be sorted before they are added to the machine based on |
36
|
|
|
* if they contain a regex or not. All regex transitions will be added to |
37
|
|
|
* the machine after the non-regex transitions have been added. |
38
|
|
|
* |
39
|
|
|
* |
40
|
|
|
* |
41
|
|
|
* @see Transitions |
42
|
|
|
* @see State |
43
|
|
|
* @see PDO |
44
|
|
|
* @author Rolf Vreijdenberger |
45
|
|
|
* |
46
|
|
|
*/ |
47
|
|
|
class LoaderArray implements Loader { |
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* @var Transition[] |
51
|
|
|
*/ |
52
|
|
|
protected $transitions; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @param Transition[] $transitions |
57
|
|
|
* the transitions to be loaded |
58
|
|
|
*/ |
59
|
19 |
|
public function __construct($transitions = array()) |
60
|
|
|
{ |
61
|
19 |
|
$this->transitions = array(); |
62
|
19 |
|
foreach ($transitions as $transition) { |
63
|
19 |
|
if (!is_a($transition, 'izzum\statemachine\Transition')) { |
64
|
1 |
|
throw new Exception('Expected Transition (or a subclass), found something else: ' . get_class($transition), Exception::BAD_LOADERDATA); |
65
|
|
|
} |
66
|
19 |
|
$this->add($transition); |
67
|
19 |
|
} |
68
|
19 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
*/ |
73
|
17 |
|
public function load(StateMachine $stateMachine) |
74
|
|
|
{ |
75
|
17 |
|
$count = 0; |
76
|
17 |
|
$unsorted = $this->getTransitions(); |
77
|
17 |
|
$has_regex = array(); |
78
|
17 |
|
$has_no_regex = array(); |
79
|
17 |
|
foreach ($unsorted as $transition) { |
80
|
17 |
|
$to = $transition->getStateTo(); |
81
|
17 |
|
$from = $transition->getStateFrom(); |
82
|
|
|
//sort on regexes. they should come last in an automated loader like this |
83
|
17 |
|
if ($from->isRegex() || $to->isRegex()) { |
84
|
12 |
|
$has_regex [] = $transition; |
85
|
12 |
|
} else { |
86
|
16 |
|
$has_no_regex [] = $transition; |
87
|
|
|
} |
88
|
17 |
|
} |
89
|
17 |
|
$sorted = array_merge($has_no_regex, $has_regex); |
90
|
|
|
|
91
|
|
|
// add the sorted transitions. the transitions added will set the |
92
|
|
|
// states (from/to) on the statemachine |
93
|
17 |
|
foreach ($sorted as $transition) { |
94
|
|
|
// when using transitions with 'regex' states, the statemachine will handle this for you. |
95
|
17 |
|
$count += $stateMachine->addTransition($transition); |
96
|
17 |
|
} |
97
|
17 |
|
return $count; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* add/overwrite a transition |
102
|
|
|
* |
103
|
|
|
* @param Transition $transition |
104
|
|
|
*/ |
105
|
19 |
|
public function add(Transition $transition) |
106
|
|
|
{ |
107
|
19 |
|
$this->transitions [$transition->getName()] = $transition; |
108
|
19 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* This method will return the transitions instances with the correct |
112
|
|
|
* references to each other. |
113
|
|
|
* |
114
|
|
|
* @return Transition[] |
115
|
|
|
*/ |
116
|
18 |
|
public function getTransitions() |
117
|
|
|
{ |
118
|
18 |
|
return $this->transitions; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* counts the number of contained transitions. |
123
|
|
|
* this is not the same as the possible amount of transitions added (because of regex transitions) |
124
|
|
|
* |
125
|
|
|
* @return int |
126
|
|
|
*/ |
127
|
4 |
|
public function count() |
128
|
|
|
{ |
129
|
4 |
|
return (int) count($this->transitions); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
public function toString() |
133
|
|
|
{ |
134
|
1 |
|
return get_class($this); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
public function __toString() |
138
|
|
|
{ |
139
|
1 |
|
return $this->toString(); |
140
|
|
|
} |
141
|
|
|
} |