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