|
1
|
|
|
<?php |
|
2
|
|
|
namespace izzum\statemachine\builder; |
|
3
|
|
|
use izzum\statemachine\EntityBuilder; |
|
4
|
|
|
use izzum\statemachine\Identifier; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Always returns the same model; the one provided in the constructor. |
|
8
|
|
|
* This will thus not build the model when the statemachine asks for it, but |
|
9
|
|
|
* rather the model is built when your application code creates it. |
|
10
|
|
|
* |
|
11
|
|
|
* This class is useful if your domain model actually subclasses the |
|
12
|
|
|
* StateMachine class or uses object composition to use the statemachines' logic internally. |
|
13
|
|
|
* |
|
14
|
|
|
* In that case, you can construct the StateMachine in your models' constructor, |
|
15
|
|
|
* including the Context with this class in it and with the domain model itself ($this) as |
|
16
|
|
|
* an argument to the builder, so you can put our event handlers on the domain model and |
|
17
|
|
|
* have them respond to the statemachine. |
|
18
|
|
|
* |
|
19
|
|
|
* //eg: in the constructor of your domain model that uses object composition |
|
20
|
|
|
* $builder = new ModelBuilder($this); |
|
21
|
|
|
* $identifier = new Identifier($this->getId(), 'my-model-machine'); |
|
22
|
|
|
* $context = new Context($identifier, $builder); |
|
23
|
|
|
* $this->machine = new StateMachine($context); |
|
24
|
|
|
* |
|
25
|
|
|
* |
|
26
|
|
|
* eg: in the constructor of your domain model that uses StateMachine as its' superclass |
|
27
|
|
|
* $builder = new ModelBuilder($this); |
|
28
|
|
|
* $identifier = new Identifier($this->getId(), 'my-model-machine'); |
|
29
|
|
|
* $context = new Context($identifier, $builder); |
|
30
|
|
|
* parent::__construct($context) |
|
31
|
|
|
* |
|
32
|
|
|
* @link https://en.wikipedia.org/wiki/Object_composition |
|
33
|
|
|
* |
|
34
|
|
|
* @author Rolf Vreijdenberger |
|
35
|
|
|
* |
|
36
|
|
|
*/ |
|
37
|
|
|
class ModelBuilder extends EntityBuilder { |
|
38
|
|
|
/** |
|
39
|
|
|
* the model to be returned by the Context |
|
40
|
|
|
* |
|
41
|
|
|
* @var mixed |
|
42
|
|
|
*/ |
|
43
|
|
|
private $model; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* |
|
47
|
|
|
* @param mixed $model |
|
48
|
|
|
* the domain model you want to have returned from this class. |
|
49
|
|
|
*/ |
|
50
|
4 |
|
public function __construct($model) |
|
51
|
|
|
{ |
|
52
|
4 |
|
$this->model = $model; |
|
53
|
4 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritDoc} |
|
57
|
|
|
*/ |
|
58
|
4 |
|
protected function build(Identifier $identifier) |
|
59
|
|
|
{ |
|
60
|
|
|
// no building actually happens. we always return the same model. |
|
61
|
4 |
|
return $this->model; |
|
62
|
|
|
} |
|
63
|
|
|
} |