1 | <?php |
||
23 | final class PayloadEnvelope implements Payload |
||
24 | { |
||
25 | /** |
||
26 | * Flag if context changed |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | private $hasChanged = false; |
||
31 | |||
32 | /** |
||
33 | * Current contexts state |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | private $state; |
||
38 | |||
39 | /** |
||
40 | * Current runs history |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | private $history = []; |
||
45 | |||
46 | /** |
||
47 | * Actual context instance |
||
48 | * |
||
49 | * @var mixed |
||
50 | */ |
||
51 | private $subject; |
||
52 | |||
53 | 16 | private function __construct($subject) |
|
57 | |||
58 | /** |
||
59 | * @param mixed $subject |
||
60 | * |
||
61 | * @return PayloadEnvelope |
||
62 | */ |
||
63 | 16 | public static function wrap($subject): PayloadEnvelope |
|
67 | |||
68 | /** |
||
69 | * Return true if state has changed (even if it was changed back to itself) |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | 1 | public function hasChanged(): bool |
|
77 | |||
78 | /** |
||
79 | * Return current subject state |
||
80 | * |
||
81 | * @return null|string |
||
82 | */ |
||
83 | 6 | public function state() |
|
87 | |||
88 | /** |
||
89 | * Set new state to subject |
||
90 | * |
||
91 | * @param string $name state name |
||
92 | */ |
||
93 | 8 | public function changeState($name) |
|
99 | |||
100 | /** |
||
101 | * Return array with all states that were set in this run |
||
102 | * |
||
103 | * @return array |
||
104 | */ |
||
105 | 5 | public function history(): array |
|
109 | |||
110 | /** |
||
111 | * Return subject |
||
112 | * |
||
113 | * @return mixed |
||
114 | */ |
||
115 | 1 | public function subject() |
|
119 | } |
||
120 |