1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/validize project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Validize\ValueObject; |
10
|
|
|
|
11
|
|
|
use Daikon\Interop\Assertion; |
12
|
|
|
use Daikon\ValueObject\ValueObjectInterface; |
13
|
|
|
use ReflectionClass; |
14
|
|
|
|
15
|
|
|
final class Severity implements ValueObjectInterface |
16
|
|
|
{ |
17
|
|
|
public const CRITICAL = 400; |
18
|
|
|
public const ERROR = 300; |
19
|
|
|
public const NOTICE = 200; |
20
|
|
|
public const SILENT = 100; |
21
|
|
|
public const SUCCESS = 0; |
22
|
|
|
public const UNPROCESSED = -1; |
23
|
|
|
|
24
|
|
|
private int $severity; |
25
|
|
|
|
26
|
|
|
/** @param int|string $severity */ |
27
|
8 |
|
public static function fromNative($severity): self |
28
|
|
|
{ |
29
|
8 |
|
Assertion::integerish($severity, 'Must be an integer.'); |
30
|
6 |
|
return new self((int)$severity); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
public static function critical(): self |
34
|
|
|
{ |
35
|
1 |
|
return new self(self::CRITICAL); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public static function error(): self |
39
|
|
|
{ |
40
|
1 |
|
return new self(self::ERROR); |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
public static function notice(): self |
44
|
|
|
{ |
45
|
1 |
|
return new self(self::NOTICE); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public static function silent(): self |
49
|
|
|
{ |
50
|
1 |
|
return new self(self::SILENT); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
public static function success(): self |
54
|
|
|
{ |
55
|
1 |
|
return new self(self::SUCCESS); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public static function unprocessed(): self |
59
|
|
|
{ |
60
|
1 |
|
return new self(self::UNPROCESSED); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @param self $comparator */ |
64
|
|
|
public function equals($comparator): bool |
65
|
|
|
{ |
66
|
|
|
Assertion::isInstanceOf($comparator, self::class); |
67
|
|
|
return $this->toNative() === $comparator->toNative(); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function isUnprocessed(): bool |
71
|
|
|
{ |
72
|
1 |
|
return $this->severity === self::UNPROCESSED; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
public function isSilent(): bool |
76
|
|
|
{ |
77
|
1 |
|
return $this->severity === self::SILENT; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function isSuccess(): bool |
81
|
|
|
{ |
82
|
1 |
|
return $this->severity === self::SUCCESS; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function isNotice(): bool |
86
|
|
|
{ |
87
|
1 |
|
return $this->severity === self::NOTICE; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function isError(): bool |
91
|
|
|
{ |
92
|
1 |
|
return $this->severity === self::ERROR; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function isCritical(): bool |
96
|
|
|
{ |
97
|
1 |
|
return $this->severity === self::CRITICAL; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function isGreaterThanOrEqual(Severity $comparator): bool |
101
|
|
|
{ |
102
|
|
|
return $this->toNative() >= $comparator->toNative(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function isLessThanOrEqual(Severity $comparator): bool |
106
|
|
|
{ |
107
|
|
|
return $this->toNative() <= $comparator->toNative(); |
108
|
|
|
} |
109
|
|
|
|
110
|
6 |
|
public function toNative(): int |
111
|
|
|
{ |
112
|
6 |
|
return $this->severity; |
113
|
|
|
} |
114
|
|
|
|
115
|
6 |
|
public function __toString(): string |
116
|
|
|
{ |
117
|
6 |
|
return (string)$this->severity; |
118
|
|
|
} |
119
|
|
|
|
120
|
6 |
|
private function __construct(int $severity) |
121
|
|
|
{ |
122
|
6 |
|
Assertion::inArray($severity, (new ReflectionClass($this))->getConstants(), 'Invalid value.'); |
123
|
6 |
|
$this->severity = $severity; |
124
|
6 |
|
} |
125
|
|
|
} |
126
|
|
|
|