1 | <?php |
||
2 | |||
3 | namespace CfdiUtils\Validate; |
||
4 | |||
5 | /** |
||
6 | * Status (immutable value object) |
||
7 | * Define the status used in an assertion |
||
8 | */ |
||
9 | class Status |
||
10 | { |
||
11 | public const STATUS_ERROR = 'ERROR'; |
||
12 | |||
13 | public const STATUS_WARNING = 'WARN'; |
||
14 | |||
15 | public const STATUS_NONE = 'NONE'; |
||
16 | |||
17 | public const STATUS_OK = 'OK'; |
||
18 | |||
19 | public const ORDER_MAP = [ |
||
20 | self::STATUS_ERROR => 1, |
||
21 | self::STATUS_WARNING => 2, |
||
22 | self::STATUS_NONE => 3, |
||
23 | self::STATUS_OK => 4, |
||
24 | ]; |
||
25 | |||
26 | private $status; |
||
27 | |||
28 | 451 | public function __construct(string $value) |
|
29 | { |
||
30 | // using values as keys for speed access |
||
31 | if ( |
||
32 | 451 | self::STATUS_ERROR !== $value && self::STATUS_WARNING !== $value |
|
33 | 451 | && self::STATUS_OK !== $value && self::STATUS_NONE !== $value |
|
34 | ) { |
||
35 | 1 | throw new \UnexpectedValueException('The status is not one of the defined valid constants'); |
|
36 | } |
||
37 | 450 | $this->status = $value; |
|
38 | } |
||
39 | |||
40 | 290 | public static function ok(): self |
|
41 | { |
||
42 | 290 | return new self(self::STATUS_OK); |
|
43 | } |
||
44 | |||
45 | 328 | public static function error(): self |
|
46 | { |
||
47 | 328 | return new self(self::STATUS_ERROR); |
|
48 | } |
||
49 | |||
50 | 10 | public static function warn(): self |
|
51 | { |
||
52 | 10 | return new self(self::STATUS_WARNING); |
|
53 | } |
||
54 | |||
55 | 371 | public static function none(): self |
|
56 | { |
||
57 | 371 | return new self(self::STATUS_NONE); |
|
58 | } |
||
59 | |||
60 | 6 | public function isError(): bool |
|
61 | { |
||
62 | 6 | return self::STATUS_ERROR === $this->status; |
|
63 | } |
||
64 | |||
65 | 1 | public function isWarning(): bool |
|
66 | { |
||
67 | 1 | return self::STATUS_WARNING === $this->status; |
|
68 | } |
||
69 | |||
70 | 43 | public function isOk(): bool |
|
71 | { |
||
72 | 43 | return self::STATUS_OK === $this->status; |
|
73 | } |
||
74 | |||
75 | 27 | public function isNone(): bool |
|
76 | { |
||
77 | 27 | return self::STATUS_NONE === $this->status; |
|
78 | } |
||
79 | |||
80 | 309 | public static function when(bool $condition, self $errorStatus = null): self |
|
81 | { |
||
82 | 309 | return ($condition) ? self::ok() : ($errorStatus ?: self::error()); |
|
83 | } |
||
84 | |||
85 | 426 | public function equalsTo(self $status): bool |
|
86 | { |
||
87 | 426 | return ($status->status === $this->status); |
|
88 | } |
||
89 | |||
90 | 1 | public function compareTo(self $status): int |
|
91 | { |
||
92 | 1 | return $this->comparableValue($this) <=> $this->comparableValue($status); |
|
93 | } |
||
94 | |||
95 | 1 | public static function comparableValue(self $status): int |
|
96 | { |
||
97 | 1 | return self::ORDER_MAP[$status->status]; |
|
98 | } |
||
99 | |||
100 | 397 | public function __toString(): string |
|
101 | { |
||
102 | 397 | return $this->status; |
|
103 | } |
||
104 | } |
||
105 |