1 | <?php |
||
18 | class Utils { |
||
19 | const STATE_CONCATENATOR = '_to_'; |
||
20 | |||
21 | |||
22 | /** |
||
23 | * Checks a fully loaded statemachine for a valid configuration. |
||
24 | * |
||
25 | * This is useful in a debug situation so you can check for the validity of |
||
26 | * rules, commands and callables in guard logic, transition logic and state entry/exit logic |
||
27 | * before they hit a |
||
28 | * |
||
29 | * This does not instantiate any objects. It just checks if callables can be found |
||
30 | * and if classes for rules and commands can be found |
||
31 | * |
||
32 | * @param StateMachine $machine |
||
33 | * @return Exception[] an array of exceptions if anything is wrong with the configuration |
||
34 | */ |
||
35 | 3 | public static function checkConfiguration(StateMachine $machine) |
|
64 | |||
65 | /** |
||
66 | * @param callable $callable |
||
67 | * @param string $type a type description of the callable eg: State::CALLABLE_ENTRY |
||
68 | * @param string $info extra info for exception message purposes |
||
69 | * @param Context $context optional: the context the callable is called in |
||
70 | * @return Exception if the callable does not pass the check |
||
71 | */ |
||
72 | 3 | private static function getExceptionForCheckingCallable($callable, $type, $info, $context = null) |
|
81 | |||
82 | |||
83 | /** |
||
84 | * @param callable $callable |
||
85 | * @param string $type a type description of the callable eg: State::CALLABLE_ENTRY |
||
86 | * @param string $info extra info for exception message purposes |
||
87 | * @param Context $context optional: the context the callable is called in |
||
88 | * @return bool |
||
89 | * @throws Exception if the callable does not pass the check |
||
90 | */ |
||
91 | 16 | public static function checkCallable($callable, $type, $info, $context = null) |
|
100 | |||
101 | |||
102 | /** |
||
103 | * gets the transition name by two state names, using the default convention |
||
104 | * for a transition name (which is concatenating state-from to state-to with |
||
105 | * '_to_') |
||
106 | * |
||
107 | * @param string $from |
||
108 | * the state from which the transition is made |
||
109 | * @param string $to |
||
110 | * the state to which the transition will be made |
||
111 | * @return string <state_from>_to_<state_to> |
||
112 | */ |
||
113 | 68 | public static function getTransitionName($from, $to) |
|
117 | |||
118 | /** |
||
119 | * returns the associated Command for the entry/exit/transition action on a |
||
120 | * State or a Transition. |
||
121 | * the Command will be configured with the 'reference' of the stateful |
||
122 | * object |
||
123 | * |
||
124 | * @param string $command_name |
||
125 | * entry~,exit~ or transition command name. |
||
126 | * multiple commands can be split by a ',' in which case a |
||
127 | * composite command will be returned. |
||
128 | * @param Context $context |
||
129 | * to be able to get the entity |
||
130 | * @return ICommand |
||
131 | * @throws Exception |
||
132 | */ |
||
133 | 41 | public static function getCommand($command_name, Context $context) |
|
170 | |||
171 | /** |
||
172 | * Always returns an izzum exception (converts a non-izzum exception to an |
||
173 | * izzum exception). |
||
174 | * optionally throws it. |
||
175 | * |
||
176 | * @param \Exception $e |
||
177 | * @param int $code |
||
178 | * @return Exception |
||
179 | * @throws Exception |
||
180 | */ |
||
181 | 4 | public static function wrapToStateMachineException(\Exception $e, $code, $throw = false) |
|
192 | |||
193 | /** |
||
194 | * get all states that match a possible regex state from the set of states |
||
195 | * provided |
||
196 | * |
||
197 | * @param State $regex |
||
198 | * a possible regex state. |
||
199 | * @param State[] $targets |
||
200 | * all target State instances that we check the regex against. |
||
201 | * @return State[] an array of State instances from the $targets State |
||
202 | * instances that matched the (negated) regex, or the $regex State if it was |
||
203 | * not a regex State after all. |
||
204 | * @link https://php.net/manual/en/function.preg-match.php |
||
205 | * @link http://regexr.com/ for trying out regular expressions |
||
206 | */ |
||
207 | 44 | public static function getAllRegexMatchingStates(State $regex, $targets) |
|
222 | |||
223 | /** |
||
224 | * does an input regex state match a target states' name? |
||
225 | * |
||
226 | * @param State $regex |
||
227 | * the regex state |
||
228 | * @param State $target |
||
229 | * the state to match the regular expression to |
||
230 | * @return boolean |
||
231 | * @link https://php.net/manual/en/function.preg-match.php |
||
232 | * @link http://regexr.com/ for trying out regular expressions |
||
233 | */ |
||
234 | 20 | public static function matchesRegex(State $regex, State $target) |
|
247 | } |