1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Workflow library. |
5
|
|
|
* |
6
|
|
|
* @package workflow |
7
|
|
|
* @author David Molineus <[email protected]> |
8
|
|
|
* @copyright 2014-2017 netzmacht David Molineus |
9
|
|
|
* @license LGPL 3.0 https://github.com/netzmacht/workflow |
10
|
|
|
* @filesource |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace Netzmacht\Workflow\Flow; |
16
|
|
|
|
17
|
|
|
use Netzmacht\Workflow\Flow\Security\Permission; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Step defines fixed step in the workflow process. |
21
|
|
|
* |
22
|
|
|
* @package Netzmacht\Workflow\Flow |
23
|
|
|
*/ |
24
|
|
|
class Step extends Base |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* The allowed transition names. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $allowedTransitions = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Step is a final step. |
35
|
|
|
* |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
private $final = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Assigned permission. |
42
|
|
|
* |
43
|
|
|
* @var Permission|null |
44
|
|
|
*/ |
45
|
|
|
private $permission; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Consider if step is final. |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function isFinal(): bool |
53
|
|
|
{ |
54
|
|
|
return $this->final; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Mark step as final. |
59
|
|
|
* |
60
|
|
|
* @param bool $final Step is a final step. |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function setFinal(bool $final): self |
65
|
|
|
{ |
66
|
|
|
$this->final = $final; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Allow a transition. |
73
|
|
|
* |
74
|
|
|
* @param string $transitionName The name of the allowed transition. |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function allowTransition(string $transitionName): self |
79
|
|
|
{ |
80
|
|
|
if (!in_array($transitionName, $this->allowedTransitions)) { |
81
|
|
|
$this->allowedTransitions[] = $transitionName; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Disallow a transition. |
89
|
|
|
* |
90
|
|
|
* @param string $transitionName The name of the disallowed transition. |
91
|
|
|
* |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function disallowTransition(string $transitionName): self |
95
|
|
|
{ |
96
|
|
|
$key = array_search($transitionName, $this->allowedTransitions); |
97
|
|
|
|
98
|
|
|
if ($key !== false) { |
99
|
|
|
unset($this->allowedTransitions[$key]); |
100
|
|
|
$this->allowedTransitions = array_values($this->allowedTransitions); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get all allowed transition names. |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function getAllowedTransitions(): array |
112
|
|
|
{ |
113
|
|
|
if ($this->isFinal()) { |
114
|
|
|
return array(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->allowedTransitions; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Consider if transition is allowed. |
122
|
|
|
* |
123
|
|
|
* @param string $transitionName The name of the checked transition. |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function isTransitionAllowed(string $transitionName): bool |
128
|
|
|
{ |
129
|
|
|
if ($this->isFinal()) { |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return in_array($transitionName, $this->allowedTransitions); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Consider if step has a specific permission. |
138
|
|
|
* |
139
|
|
|
* @param Permission $permission Permission to be checked. |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
public function hasPermission(Permission $permission): bool |
144
|
|
|
{ |
145
|
|
|
if ($this->permission) { |
146
|
|
|
return $this->permission->equals($permission); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get permission of the step. If none is assigned it returns null. |
154
|
|
|
* |
155
|
|
|
* @return Permission|null |
156
|
|
|
*/ |
157
|
|
|
public function getPermission():? Permission |
158
|
|
|
{ |
159
|
|
|
return $this->permission; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set permission. |
164
|
|
|
* |
165
|
|
|
* @param Permission $permission Permission to be set. |
166
|
|
|
* |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
|
|
public function setPermission(Permission $permission): self |
170
|
|
|
{ |
171
|
|
|
$this->permission = $permission; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|