|
1
|
|
|
<?php |
|
2
|
|
|
namespace izzum\statemachine; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* an instance of Identifier uniquely identifies the statemachine to be used. |
|
6
|
|
|
* |
|
7
|
|
|
* A statemachine is always uniquely identified by the combination of an entity |
|
8
|
|
|
* id and a machine name (that provides the relation to the statemachine the |
|
9
|
|
|
* entity is governed by). |
|
10
|
|
|
* |
|
11
|
|
|
* the machine name should be a 'machine readable' string, since it will be stored in different |
|
12
|
|
|
* backends and might be used as a key there (eg: in redis). |
|
13
|
|
|
* |
|
14
|
|
|
* The entity id is something that uniquely identifies a domain model. Probably |
|
15
|
|
|
* something that is stored in your application, like a primary key in a table, a GUID or a hash. |
|
16
|
|
|
* |
|
17
|
|
|
* This object thus stores the minimum data needed from other processes in your |
|
18
|
|
|
* application domain to succesfully work with the statemachine. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Rolf Vreijdenberger |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
class Identifier { |
|
24
|
|
|
const NULL_ENTITY_ID = "-1"; |
|
25
|
|
|
const NULL_STATEMACHINE = 'null-machine'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* an entity id that represents the unique identifier for an application |
|
29
|
|
|
* domain specific object (entity) like 'Order', 'Customer' etc. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $entity_id; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* the statemachine that governs the state behaviour for this entity (eg |
|
37
|
|
|
* 'order'). |
|
38
|
|
|
* this is the name of the statemachine itself and is used in conjunction |
|
39
|
|
|
* with the entity_id to define what a statemachine is about. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $machine_name; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Constructor |
|
47
|
|
|
* |
|
48
|
|
|
* @param mixed $entity_id |
|
49
|
|
|
* the id of the domain specific entity (it will internally be |
|
50
|
|
|
* converted to a string) |
|
51
|
|
|
* @param string $machine_name |
|
52
|
|
|
* the name of the statemachine (eg: 'order') |
|
53
|
|
|
*/ |
|
54
|
97 |
|
public function __construct($entity_id, $machine_name) |
|
55
|
|
|
{ |
|
56
|
|
|
// convert $entity_id to string (it will likely be an int but a string |
|
57
|
|
|
// gives more flexibility) |
|
58
|
97 |
|
$this->setEntityId($entity_id); |
|
59
|
97 |
|
$this->machine_name = $machine_name; |
|
60
|
97 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* gets the statemachine name that handles the entity |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
53 |
|
public function getMachine() |
|
68
|
|
|
{ |
|
69
|
53 |
|
return $this->machine_name; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* set the id of the domain specific entity (it will internally be converted to a string) |
|
74
|
|
|
* @param mixed $entity_id |
|
75
|
|
|
*/ |
|
76
|
97 |
|
public function setEntityId($entity_id) |
|
77
|
|
|
{ |
|
78
|
97 |
|
$this->entity_id = trim("$entity_id"); |
|
79
|
97 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* gets the entity id that represents the unique identifier for the |
|
83
|
|
|
* application domain specific model. |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
45 |
|
public function getEntityId() |
|
88
|
|
|
{ |
|
89
|
45 |
|
return $this->entity_id; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* get the unique identifier representation for an Identifier, which |
|
94
|
|
|
* consists of the machine name and the entity_id in parseable form. |
|
95
|
|
|
* |
|
96
|
|
|
* @param boolean $readable |
|
97
|
|
|
* human readable or not. defaults to false |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
33 |
|
public function getId($readable = false) |
|
101
|
|
|
{ |
|
102
|
33 |
|
if ($readable) { |
|
103
|
18 |
|
$output = "machine: '" . $this->getMachine() . "', id: '" . $this->getEntityId() . "'"; |
|
104
|
18 |
|
} else { |
|
105
|
22 |
|
$output = $this->getMachine() . "_" . $this->getEntityId(); |
|
106
|
|
|
} |
|
107
|
33 |
|
return $output; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
3 |
|
public function toString() |
|
115
|
|
|
{ |
|
116
|
3 |
|
return get_class($this) . ' ' . $this->getId(true); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
3 |
|
public function __toString() |
|
124
|
|
|
{ |
|
125
|
3 |
|
return $this->toString(); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|