File tree Expand file tree Collapse file tree 11 files changed +133
-113
lines changed Expand file tree Collapse file tree 11 files changed +133
-113
lines changed Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace DesignPatterns \Behavioral \State ;
4+
5+ class OrderContext
6+ {
7+ /**
8+ * @var State
9+ */
10+ private $ state ;
11+
12+ public static function create (): OrderContext
13+ {
14+ $ order = new self ();
15+ $ order ->state = new StateCreated ();
16+
17+ return $ order ;
18+ }
19+
20+ public function setState (State $ state )
21+ {
22+ $ this ->state = $ state ;
23+ }
24+
25+ public function proceedToNext ()
26+ {
27+ $ this ->state ->proceedToNext ($ this );
28+ }
29+
30+ public function toString ()
31+ {
32+ return $ this ->state ->toString ();
33+ }
34+ }
Original file line number Diff line number Diff line change 2020
2121You can also find this code on `GitHub `_
2222
23- ContextOrder .php
23+ OrderContext .php
2424
25- .. literalinclude :: ContextOrder .php
25+ .. literalinclude :: OrderContext .php
2626 :language: php
2727 :linenos:
2828
29- StateOrder .php
29+ State .php
3030
31- .. literalinclude :: StateOrder .php
31+ .. literalinclude :: State .php
3232 :language: php
3333 :linenos:
3434
35- ShippingOrder .php
35+ StateCreated .php
3636
37- .. literalinclude :: ShippingOrder .php
37+ .. literalinclude :: StateCreated .php
3838 :language: php
3939 :linenos:
4040
41- CreateOrder .php
41+ StateShipped .php
4242
43- .. literalinclude :: CreateOrder.php
43+ .. literalinclude :: StateShipped.php
44+ :language: php
45+ :linenos:
46+
47+ StateDone.php
48+
49+ .. literalinclude :: StateDone.php
4450 :language: php
4551 :linenos:
4652
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace DesignPatterns \Behavioral \State ;
4+
5+ interface State
6+ {
7+ public function proceedToNext (OrderContext $ context );
8+
9+ public function toString (): string ;
10+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace DesignPatterns \Behavioral \State ;
4+
5+ class StateCreated implements State
6+ {
7+ public function proceedToNext (OrderContext $ context )
8+ {
9+ $ context ->setState (new StateShipped ());
10+ }
11+
12+ public function toString (): string
13+ {
14+ return 'created ' ;
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace DesignPatterns \Behavioral \State ;
4+
5+ class StateDone implements State
6+ {
7+ public function proceedToNext (OrderContext $ context )
8+ {
9+ // there is nothing more to do
10+ }
11+
12+ public function toString (): string
13+ {
14+ return 'done ' ;
15+ }
16+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace DesignPatterns \Behavioral \State ;
4+
5+ class StateShipped implements State
6+ {
7+ public function proceedToNext (OrderContext $ context )
8+ {
9+ $ context ->setState (new StateDone ());
10+ }
11+
12+ public function toString (): string
13+ {
14+ return 'shipped ' ;
15+ }
16+ }
You can’t perform that action at this time.
0 commit comments