22
33Maybe state machines are not what you usually do with PHP but when you do... just use this.
44This simple yet powerful framework will keep your state machines within their desired state
5- transition cycles . You can also modify their behavior whilst running or just configure them
5+ transition schemas . You can also modify their behavior whilst running or just configure them
66dynamically and launch. You can define state transition conditions you want based upon
77anonymous functions or class methods. Fire events on each state transition with your favorite
88event dispatcher.
@@ -60,23 +60,6 @@ For clarity each state machine should have its own state dictionary defined.
6060 class Petition extends Machine {
6161
6262 protected $votesYes, $votesNo;
63-
64- public function init() {
65- $this->setInitState(PetitionEnum::DRAFT());
66-
67- // defines machine's allowed behavior
68- $this
69- // prevents changing state upon assertion when AlwaysFalseAssertion is given
70- ->allowTransition(PetitionEnum::DRAFT(), PetitionEnum::SENT(), new AlwaysFalseAssertion())
71- ->allowTransition(PetitionEnum::DRAFT(), PetitionEnum::CANCELED(), new AlwaysFalseAssertion())
72-
73- // when no Assertion is given uses DefaultCallbackAssertion which calls assertXToY methods
74- ->allowTransition(PetitionEnum::SENT(), PetitionEnum::VOTED())
75- ->allowTransition(PetitionEnum::VOTED(), PetitionEnum::ACCEPTED())
76- ->allowTransition(PetitionEnum::VOTED(), PetitionEnum::REJECTED())
77- ;
78-
79- }
8063
8164 public function send()
8265 {
@@ -124,12 +107,38 @@ For clarity each state machine should have its own state dictionary defined.
124107 }
125108```
126109
110+ ### Machine's transition schema definition
111+
112+ As of version 2 transition schema is decoupled from actual machine so it can be easily injected into any machine object
113+ or changed when is necessary to modify machine's behavior later.
114+
115+ ``` php
116+
117+ $schema = new Schema();
118+
119+ $schema
120+ ->setInitState(PetitionEnum::DRAFT())
121+ // prevents changing state upon assertion when AlwaysFalseAssertion is given
122+ ->allowTransition(PetitionEnum::DRAFT(), PetitionEnum::SENT(), new AlwaysFalseAssertion())
123+ ->allowTransition(PetitionEnum::DRAFT(), PetitionEnum::CANCELED(), new AlwaysFalseAssertion())
124+ // when no Assertion is given uses DefaultCallbackAssertion which calls assertXToY methods
125+ ->allowTransition(PetitionEnum::SENT(), PetitionEnum::VOTED())
126+ ->allowTransition(PetitionEnum::VOTED(), PetitionEnum::ACCEPTED())
127+ ->allowTransition(PetitionEnum::VOTED(), PetitionEnum::REJECTED());
128+
129+ ```
130+
131+ * Remark: By default (when no assertion object is given as parameter) ` Schema::allowTransition() ` method attaches ` DefaultCallbackAssertion ` .*
132+
133+
127134### Machine in-use
128135
129136``` php
130137
131138 $p = new Petition();
132- $p->init();
139+ $p
140+ ->setSchema($schema)
141+ ->init();
133142
134143 $p->run();
135144 // <nothing happens >
@@ -150,9 +159,32 @@ For clarity each state machine should have its own state dictionary defined.
150159
151160### Transition object
152161
153- Each transition object should have one or more assertion objects attached.
162+ Defined as change from one state to another. Has two basic functions:
163+ - allows to assert if this exact transition is valid considering defined transition conditions by them via
164+ attached assertions or conditions defined within transition class itself
165+ - allows to run certain code in ` Transition::onTransition() ` method when actual transition happens; this functionality
166+ doubles that defined within machine for situations when it's required to control it externally from within transition
167+ object. As well as with ` Machine::onTransition() ` method we can also dispatch events from here or use one common \
168+ transition object for several different machines sharing some functionality.
154169
155- * Remark: By default (when no assertion object is given as parameter) ` Machine::allowTransition() ` method attaches ` DefaultCallbackAssertion ` .*
170+ Example:
171+ ``` php
172+
173+ class SendToVotedTransition extends Transition {
174+ public function assert(MachineInterface $machine)
175+ {
176+ return $machine->hasVotes() ? true : false;
177+ }
178+
179+ public function onTransition(MachineInterface $machine)
180+ {
181+ $machine->getPetitionStats()->incrementVotedCount();
182+ }
183+ }
184+
185+ ```
186+
187+ Each transition object should have one or more assertion objects attached.
156188
157189
158190### Assertion behaviors
@@ -164,7 +196,7 @@ Results in automatic transition when machine is launched.
164196Be aware:
165197
166198``` php
167- $machine
199+ $machine->getSchema()
168200 ->allowTransition(MachineEnum::ONE(), MachineEnum::TWO(), new AlwaysTrueAssertion)
169201 ->allowTransition(MachineEnum::TWO(), MachineEnum::ONE(), new AlwaysTrueAssertion)
170202
@@ -179,11 +211,11 @@ changed upon `setMachineState()` method call only.
179211
180212#### DefaultCallbackAssertion
181213
182- Calls ` assertXToY ` method on machine object (or other object if specified) and makes transition decision upon its return.
214+ Calls ` assertXToY ` method on machine object and makes transition decision upon its return.
183215
184216#### CommonCallbackAssertion
185217
186- Calls ` assertTransition ` method on machine object (or other object if specified) and makes transition decision upon its return.
218+ Calls ` assertTransition ` method on machine object and makes transition decision upon its return.
187219
188220#### CallbackAssertion
189221
0 commit comments