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\Context\ErrorCollection; |
18
|
|
|
use Netzmacht\Workflow\Flow\Context\Properties; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Context provides extra information for a transition. |
22
|
|
|
* |
23
|
|
|
* @package Netzmacht\Workflow\Flow |
24
|
|
|
*/ |
25
|
|
|
class Context |
26
|
|
|
{ |
27
|
|
|
const NAMESPACE_DEFAULT = 'default'; |
28
|
|
|
|
29
|
|
|
const NAMESPACE_ENTITY = 'entity'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Properties which will be stored as state data. |
33
|
|
|
* |
34
|
|
|
* @var Properties |
35
|
|
|
*/ |
36
|
|
|
private $properties; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Transition payload. |
40
|
|
|
* |
41
|
|
|
* @var Properties |
42
|
|
|
*/ |
43
|
|
|
private $payload; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Error collection. |
47
|
|
|
* |
48
|
|
|
* @var ErrorCollection |
49
|
|
|
*/ |
50
|
|
|
private $errorCollection; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Construct. |
54
|
|
|
* |
55
|
|
|
* @param Properties $properties The properties to be stored. |
56
|
|
|
* @param Properties $payload The given parameters. |
57
|
|
|
* @param ErrorCollection|null $errorCollection Error collection. |
58
|
|
|
*/ |
59
|
|
|
public function __construct( |
60
|
|
|
Properties $properties = null, |
61
|
|
|
Properties $payload = null, |
62
|
|
|
ErrorCollection $errorCollection = null |
63
|
|
|
) { |
64
|
|
|
$this->properties = $properties ?: new Properties(); |
65
|
|
|
$this->payload = $payload ?: new Properties(); |
66
|
|
|
$this->errorCollection = $errorCollection ?: new ErrorCollection(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get properties. |
71
|
|
|
* |
72
|
|
|
* @return Properties |
73
|
|
|
*/ |
74
|
|
|
public function getProperties() |
75
|
|
|
{ |
76
|
|
|
return $this->properties; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get payload. |
81
|
|
|
* |
82
|
|
|
* @return Properties |
83
|
|
|
*/ |
84
|
|
|
public function getPayload() |
85
|
|
|
{ |
86
|
|
|
return $this->payload; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get error collection. |
91
|
|
|
* |
92
|
|
|
* @return ErrorCollection |
93
|
|
|
*/ |
94
|
|
|
public function getErrorCollection(): ErrorCollection |
95
|
|
|
{ |
96
|
|
|
return $this->errorCollection; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Add an error. |
101
|
|
|
* |
102
|
|
|
* @param string $message Error message. |
103
|
|
|
* @param array $params Params for the error message. |
104
|
|
|
* @param ErrorCollection $collection Option. Child collection of the error. |
105
|
|
|
* |
106
|
|
|
* @return self |
107
|
|
|
*/ |
108
|
|
|
public function addError(string $message, array $params = array(), ErrorCollection $collection = null): self |
109
|
|
|
{ |
110
|
|
|
$this->errorCollection->addError($message, $params, $collection); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get a new context with an empty error collection. |
117
|
|
|
* |
118
|
|
|
* @return Context |
119
|
|
|
*/ |
120
|
|
|
public function withEmptyErrorCollection(): Context |
121
|
|
|
{ |
122
|
|
|
return new Context($this->properties, $this->payload, new ErrorCollection()); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|