|
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 InvalidArgumentException; |
|
11
|
|
|
use Psr\Log\LogLevel; |
|
12
|
|
|
use Throwable; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Holds information about an error or notice that happened on a specific |
|
16
|
|
|
* object. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Zaahid Bateson |
|
19
|
|
|
*/ |
|
20
|
|
|
class Error |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string The error message. |
|
24
|
|
|
*/ |
|
25
|
|
|
protected string $message; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string The PSR log level for this error. |
|
29
|
|
|
*/ |
|
30
|
|
|
protected string $psrLevel; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ErrorBag The object the error/notice occurred on. |
|
34
|
|
|
*/ |
|
35
|
|
|
protected ErrorBag $object; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var ?Throwable An Exception object if one happened, or null if not |
|
39
|
|
|
*/ |
|
40
|
|
|
protected ?Throwable $exception; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array<string, int> |
|
44
|
|
|
*/ |
|
45
|
|
|
private array $levelMap = [ |
|
46
|
|
|
LogLevel::EMERGENCY => 0, |
|
47
|
|
|
LogLevel::ALERT => 1, |
|
48
|
|
|
LogLevel::CRITICAL => 2, |
|
49
|
|
|
LogLevel::ERROR => 3, |
|
50
|
|
|
LogLevel::WARNING => 4, |
|
51
|
|
|
LogLevel::NOTICE => 5, |
|
52
|
|
|
LogLevel::INFO => 6, |
|
53
|
|
|
LogLevel::DEBUG => 7, |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* |
|
58
|
|
|
* @throws InvalidArgumentException if the passed $psrLogLevelAsErrorLevel |
|
59
|
|
|
* is not a known PSR log level (see \Psr\Log\LogLevel) |
|
60
|
|
|
*/ |
|
61
|
80 |
|
public function __construct(string $message, string $psrLogLevelAsErrorLevel, ErrorBag $object, ?Throwable $exception = null) |
|
62
|
|
|
{ |
|
63
|
80 |
|
if (!isset($this->levelMap[$psrLogLevelAsErrorLevel])) { |
|
64
|
1 |
|
throw new InvalidArgumentException($psrLogLevelAsErrorLevel . ' is not a known PSR Log Level'); |
|
65
|
|
|
} |
|
66
|
79 |
|
$this->message = $message; |
|
67
|
79 |
|
$this->psrLevel = $psrLogLevelAsErrorLevel; |
|
68
|
79 |
|
$this->object = $object; |
|
69
|
79 |
|
$this->exception = $exception; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns the error message. |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function getMessage() : string |
|
76
|
|
|
{ |
|
77
|
1 |
|
return $this->message; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns the PSR string log level for this error message. |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function getPsrLevel() : string |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->psrLevel; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns the class type the error occurred on. |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function getClass() : string |
|
92
|
|
|
{ |
|
93
|
1 |
|
return \get_class($this->object); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns the object the error occurred on. |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function getObject() : ErrorBag |
|
100
|
|
|
{ |
|
101
|
1 |
|
return $this->object; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns the exception that occurred, if any, or null. |
|
106
|
|
|
*/ |
|
107
|
1 |
|
public function getException() : ?Throwable |
|
108
|
|
|
{ |
|
109
|
1 |
|
return $this->exception; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns true if the PSR log level for this error is equal to or greater |
|
114
|
|
|
* than the one passed, e.g. passing LogLevel::ERROR would return true for |
|
115
|
|
|
* LogLevel::ERROR and LogLevel::CRITICAL, ALERT and EMERGENCY. |
|
116
|
|
|
*/ |
|
117
|
3 |
|
public function isPsrLevelGreaterOrEqualTo(string $minLevel) : bool |
|
118
|
|
|
{ |
|
119
|
3 |
|
$minIntLevel = $this->levelMap[$minLevel] ?? 1000; |
|
120
|
3 |
|
$thisLevel = $this->levelMap[$this->psrLevel]; |
|
121
|
3 |
|
return ($minIntLevel >= $thisLevel); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|