Skip to content

Commit b07cafa

Browse files
committed
documentation update
1 parent 5fd2219 commit b07cafa

1 file changed

Lines changed: 51 additions & 4 deletions

File tree

README.md

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ event dispatcher.
1212

1313
### States' dictionary
1414

15+
For clarity each state machine should have its own state dictionary defined.
16+
1517
```php
1618

1719
/**
1820
* @method static DRAFT()
1921
* @method static SENT()
20-
* ... // this should make your IDE believe those method exist
22+
* ... // this should make your IDE believe these methods exist
2123
*
2224
*/
2325
class PetitionEnum extends StateEnum {
@@ -29,6 +31,10 @@ event dispatcher.
2931
REJECTED = 'rejected',
3032
CANCELED = 'canceled';
3133

34+
// States names should be defined lowercase without spaces and special characters due to
35+
// automatically determined assertion method names when in use with DefaultCallbackAssertion
36+
// @todo replace DefaultCallbackAssertion behavior in this matter
37+
3238
}
3339
```
3440
### Machine class
@@ -83,24 +89,25 @@ event dispatcher.
8389

8490
public function assertVotedToAccepted()
8591
{
92+
// condition for transition from state VOTED to ACCEPTED
8693
return $this->votesYes > $this->votesNo ? true : false;
8794
}
8895

8996
public function assertVotedToRejected()
9097
{
98+
// condition for transition from state VOTED to REJECTED
9199
return $this->votesYes <= $this->votesNo ? true : false;
92100
}
93101

94102
public function onTransition(Transition $transition)
95103
{
96-
/* for purpose of this example we only echo this transition but you can
97-
easily dispatch an event from here */
104+
// for purpose of this example we only echo this transition but you can easily dispatch an event from here
98105
echo 'State changed from ' . $transition->getFromState() . ' to ' . $transition->getToState() . PHP_EOL;
99106
}
100107
}
101108
```
102109

103-
### Machine in-use
110+
### Machine in-use
104111

105112
```php
106113

@@ -123,4 +130,44 @@ event dispatcher.
123130
// State changed from voted to accepted
124131

125132
```
133+
134+
### Transition object
135+
136+
Each transition object should have one or more assertion objects attached.
137+
138+
*Remark: By default (when no assertion object is given as parameter) `Machine::allowTransition()` method attaches `DefaultCallbackAssertion`.*
139+
126140

141+
### Assertion behaviors
142+
143+
#### AlwaysTrueAssertion
144+
145+
Results in automatic transition when machine is launched.
146+
147+
Be aware:
148+
149+
```php
150+
$machine
151+
->allowTransition(MachineEnum::ONE(), MachineEnum::TWO(), new AlwaysTrueAssertion)
152+
->allowTransition(MachineEnum::TWO(), MachineEnum::ONE(), new AlwaysTrueAssertion)
153+
154+
$machine->run();
155+
// this will result in endless loop of state changes
156+
```
157+
158+
#### AlwaysFalseAssertion
159+
160+
Results in no transition when machine is launched. Use this kind of assertion when machine state is supposed to be
161+
changed upon `setMachineState()` method call only.
162+
163+
#### DefaultCallbackAssertion
164+
165+
Calls `assertXToY` method on machine object (or other object if specified) and makes transition decision upon its return.
166+
167+
#### CommonCallbackAssertion
168+
169+
Calls `assertTransition` method on machine object (or other object if specified) and makes transition decision upon its return.
170+
171+
#### CallbackAssertion
172+
173+
Calls user specified method to assert if state transition should proceed.

0 commit comments

Comments
 (0)