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\Context; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ErrorCollection collects error messages being raised during transition. |
19
|
|
|
* |
20
|
|
|
* @package Netzmacht\Workflow |
21
|
|
|
*/ |
22
|
|
|
class ErrorCollection implements \IteratorAggregate |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Stored errors. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $errors = array(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Construct. |
33
|
|
|
* |
34
|
|
|
* @param array $errors Initial error messages. |
35
|
|
|
*/ |
36
|
|
|
public function __construct(array $errors = array()) |
37
|
|
|
{ |
38
|
|
|
$this->addErrors($errors); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add a new error. |
43
|
|
|
* |
44
|
|
|
* @param string $message Error message. |
45
|
|
|
* @param array $params Params for the error message. |
46
|
|
|
* @param ErrorCollection $collection Option. Child collection of the error. |
47
|
|
|
* |
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
|
|
public function addError(string $message, array $params = array(), ErrorCollection $collection = null) |
51
|
|
|
{ |
52
|
|
|
$this->errors[] = array($message, $params, $collection); |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Check if any error isset. |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function hasErrors(): bool |
63
|
|
|
{ |
64
|
|
|
return !empty($this->errors); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Count error messages. |
69
|
|
|
* |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
|
|
public function countErrors(): int |
73
|
|
|
{ |
74
|
|
|
return count($this->errors); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get an error by it's index. |
79
|
|
|
* |
80
|
|
|
* @param int $index Error index. |
81
|
|
|
* |
82
|
|
|
* @throws \InvalidArgumentException If error index is not set. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function getError(int $index): array |
87
|
|
|
{ |
88
|
|
|
if (isset($this->errors[$index])) { |
89
|
|
|
return $this->errors[$index]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
throw new \InvalidArgumentException('Error with index "' . $index . '" not set.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Reset error collection. |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function reset(): self |
101
|
|
|
{ |
102
|
|
|
$this->errors = array(); |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Add a set of errors. |
109
|
|
|
* |
110
|
|
|
* @param array $errors List of errors. |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function addErrors(array $errors): self |
115
|
|
|
{ |
116
|
|
|
foreach ($errors as $error) { |
117
|
|
|
list($message, $params, $collection) = (array) $error; |
118
|
|
|
|
119
|
|
|
$this->addError($message, $params, $collection); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get all errors. |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
public function getErrors(): array |
131
|
|
|
{ |
132
|
|
|
return $this->errors; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function getIterator(): iterable |
139
|
|
|
{ |
140
|
|
|
return new \ArrayIterator($this->errors); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Convert error collection to an array. |
145
|
|
|
* |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public function toArray(): array |
149
|
|
|
{ |
150
|
|
|
return array_map( |
151
|
|
|
function ($error) { |
152
|
|
|
if ($error[2]) { |
153
|
|
|
/** @var ErrorCollection $collection */ |
154
|
|
|
$collection = $error[2]; |
155
|
|
|
$error[2] = $collection->toArray(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $error; |
159
|
|
|
}, |
160
|
|
|
$this->errors |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|