1 | <?php |
||
21 | final class PayloadEnvelope |
||
22 | { |
||
23 | /** |
||
24 | * Flag if context changed |
||
25 | * |
||
26 | * @var bool |
||
27 | */ |
||
28 | private $hasChanged = false; |
||
29 | |||
30 | /** |
||
31 | * Current contexts state |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $state; |
||
36 | |||
37 | /** |
||
38 | * Current runs history |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private $history = []; |
||
43 | |||
44 | /** |
||
45 | * Actual context instance |
||
46 | * |
||
47 | * @var mixed |
||
48 | */ |
||
49 | private $subject; |
||
50 | |||
51 | 18 | private function __construct($subject) |
|
59 | |||
60 | 18 | public static function wrap($subject): PayloadEnvelope |
|
64 | |||
65 | /** |
||
66 | * Return true if state has changed (even if it was changed back to itself) |
||
67 | * |
||
68 | * @return bool |
||
69 | */ |
||
70 | 1 | public function hasChanged(): bool |
|
74 | |||
75 | /** |
||
76 | * Return current subject state |
||
77 | * |
||
78 | * @return null|string |
||
79 | */ |
||
80 | 7 | public function state() |
|
84 | |||
85 | /** |
||
86 | * Set new state to subject |
||
87 | * |
||
88 | * @param string $name state name |
||
89 | */ |
||
90 | 9 | public function changeState($name) |
|
100 | |||
101 | /** |
||
102 | * Return array with all states that were set in this run |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | 5 | public function history(): array |
|
110 | |||
111 | /** |
||
112 | * Return subject |
||
113 | * |
||
114 | * @return mixed |
||
115 | */ |
||
116 | 1 | public function subject() |
|
120 | |||
121 | /** |
||
122 | * Return true if subject implements Stateful interface |
||
123 | * |
||
124 | * @return bool |
||
125 | */ |
||
126 | 18 | private function isSubjectStateful() |
|
130 | } |
||
131 |