|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace ZBateson\MailMimeParser; |
|
9
|
|
|
|
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
use Psr\Log\LogLevel; |
|
12
|
|
|
use Throwable; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Provides a top-level abstract implementation of IErrorBag. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Zaahid Bateson |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class ErrorBag implements IErrorBag |
|
20
|
|
|
{ |
|
21
|
|
|
protected LoggerInterface $logger; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Error[] array of Error objects belonging to this object. |
|
25
|
|
|
*/ |
|
26
|
|
|
private array $errors = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var bool true once the object has been validated. |
|
30
|
|
|
*/ |
|
31
|
|
|
private bool $validated = false; |
|
32
|
|
|
|
|
33
|
113 |
|
public function __construct(LoggerInterface $logger) |
|
34
|
|
|
{ |
|
35
|
113 |
|
$this->logger = $logger; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Returns the class name. Override to identify objects in logs. |
|
40
|
|
|
* |
|
41
|
|
|
*/ |
|
42
|
6 |
|
public function getErrorLoggingContextName() : string |
|
43
|
|
|
{ |
|
44
|
6 |
|
return static::class; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Return any children ErrorBag objects. |
|
49
|
|
|
* |
|
50
|
|
|
* @return IErrorBag[] |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract protected function getErrorBagChildren() : array; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Perform any extra validation and call 'addError'. |
|
56
|
|
|
* |
|
57
|
|
|
* getErrors and getAllErrors call validate() if their $validate parameter |
|
58
|
|
|
* is true. validate() is only called once on an object with getErrors |
|
59
|
|
|
* getAllErrors. |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function validate() : void |
|
62
|
|
|
{ |
|
63
|
|
|
// do nothing |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
78 |
|
public function addError(string $message, string $psrLogLevel, ?Throwable $exception = null) : static |
|
67
|
|
|
{ |
|
68
|
78 |
|
$error = new Error($message, $psrLogLevel, $this, $exception); |
|
69
|
78 |
|
$this->errors[] = $error; |
|
70
|
78 |
|
$this->logger->log( |
|
71
|
78 |
|
$psrLogLevel, |
|
72
|
78 |
|
'{contextName} {message} {exception}', |
|
73
|
78 |
|
[ |
|
74
|
78 |
|
'contextName' => $this->getErrorLoggingContextName(), |
|
75
|
78 |
|
'message' => $message, |
|
76
|
78 |
|
'exception' => $exception ?? '' |
|
77
|
78 |
|
] |
|
78
|
78 |
|
); |
|
79
|
78 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
5 |
|
public function getErrors(bool $validate = false, string $minPsrLevel = LogLevel::ERROR) : array |
|
83
|
|
|
{ |
|
84
|
5 |
|
if ($validate && !$this->validated) { |
|
85
|
|
|
$this->validated = true; |
|
86
|
|
|
$this->validate(); |
|
87
|
|
|
} |
|
88
|
5 |
|
return \array_values(\array_filter( |
|
89
|
5 |
|
$this->errors, |
|
90
|
5 |
|
function($e) use ($minPsrLevel) { |
|
|
|
|
|
|
91
|
5 |
|
return $e->isPsrLevelGreaterOrEqualTo($minPsrLevel); |
|
92
|
5 |
|
} |
|
93
|
5 |
|
)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
public function hasErrors(bool $validate = false, string $minPsrLevel = LogLevel::ERROR) : bool |
|
97
|
|
|
{ |
|
98
|
4 |
|
return (\count($this->getErrors($validate, $minPsrLevel)) > 0); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
public function getAllErrors(bool $validate = false, string $minPsrLevel = LogLevel::ERROR) : array |
|
102
|
|
|
{ |
|
103
|
3 |
|
$arr = \array_values(\array_map( |
|
104
|
3 |
|
function($e) use ($validate, $minPsrLevel) { |
|
|
|
|
|
|
105
|
3 |
|
return $e->getAllErrors($validate, $minPsrLevel); |
|
106
|
3 |
|
}, |
|
107
|
3 |
|
$this->getErrorBagChildren() |
|
108
|
3 |
|
)); |
|
109
|
3 |
|
return \array_merge($this->getErrors($validate, $minPsrLevel), ...$arr); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
2 |
|
public function hasAnyErrors(bool $validate = false, string $minPsrLevel = LogLevel::ERROR) : bool |
|
113
|
|
|
{ |
|
114
|
2 |
|
if ($this->hasErrors($validate, $minPsrLevel)) { |
|
115
|
2 |
|
return true; |
|
116
|
|
|
} |
|
117
|
2 |
|
foreach ($this->getErrorBagChildren() as $ch) { |
|
118
|
2 |
|
if ($ch->hasAnyErrors($validate, $minPsrLevel)) { |
|
119
|
|
|
return true; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
2 |
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|