Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
36 / 36 |
|
100.00% |
19 / 19 |
CRAP | |
100.00% |
1 / 1 |
| ValidationResult | |
100.00% |
36 / 36 |
|
100.00% |
19 / 19 |
23 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| pass | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| fail | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| skip | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| withTip | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| tip | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| skipped | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| passed | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| failed | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| statusCode | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| message | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| formattedMessage | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| formatResult | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| formatPassed | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| formatFailed | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| formatSkipped | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| formatTimeString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| formatTip | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| withMessage | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Support\Models; |
| 6 | |
| 7 | class ValidationResult |
| 8 | { |
| 9 | final public const PASSED = 0; |
| 10 | final public const SKIPPED = 1; |
| 11 | final public const FAILED = 2; |
| 12 | |
| 13 | protected string $message; |
| 14 | protected string $tip; |
| 15 | |
| 16 | protected bool $passed = false; |
| 17 | protected bool $skipped = false; |
| 18 | |
| 19 | public function __construct(string $defaultMessage = 'Generic check') |
| 20 | { |
| 21 | $this->message = $defaultMessage; |
| 22 | } |
| 23 | |
| 24 | public function pass(?string $withMessage = null): static |
| 25 | { |
| 26 | $this->passed = true; |
| 27 | |
| 28 | return $this->withMessage($withMessage); |
| 29 | } |
| 30 | |
| 31 | public function fail(?string $withMessage = null): static |
| 32 | { |
| 33 | $this->passed = false; |
| 34 | |
| 35 | return $this->withMessage($withMessage); |
| 36 | } |
| 37 | |
| 38 | public function skip(?string $withMessage = null): static |
| 39 | { |
| 40 | $this->skipped = true; |
| 41 | |
| 42 | return $this->withMessage($withMessage); |
| 43 | } |
| 44 | |
| 45 | public function withTip(string $withTip): static |
| 46 | { |
| 47 | $this->tip = $withTip; |
| 48 | |
| 49 | return $this; |
| 50 | } |
| 51 | |
| 52 | public function tip(): string|false |
| 53 | { |
| 54 | return $this->tip ?? false; |
| 55 | } |
| 56 | |
| 57 | public function skipped(): bool |
| 58 | { |
| 59 | return $this->skipped; |
| 60 | } |
| 61 | |
| 62 | public function passed(): bool |
| 63 | { |
| 64 | return $this->passed; |
| 65 | } |
| 66 | |
| 67 | public function failed(): bool |
| 68 | { |
| 69 | return ! $this->passed; |
| 70 | } |
| 71 | |
| 72 | public function statusCode(): int |
| 73 | { |
| 74 | if ($this->skipped()) { |
| 75 | return self::SKIPPED; |
| 76 | } |
| 77 | if ($this->passed()) { |
| 78 | return self::PASSED; |
| 79 | } |
| 80 | |
| 81 | return self::FAILED; |
| 82 | } |
| 83 | |
| 84 | public function message(): string |
| 85 | { |
| 86 | return $this->message; |
| 87 | } |
| 88 | |
| 89 | public function formattedMessage(?string $withTimeString = null): string |
| 90 | { |
| 91 | $string = " {$this->formatResult($this->message)} {$this->formatTimeString($withTimeString)}"; |
| 92 | if ($this->tip()) { |
| 93 | $string .= "\n".str_repeat(' ', 9).$this->formatTip($this->tip); |
| 94 | } |
| 95 | |
| 96 | return $string; |
| 97 | } |
| 98 | |
| 99 | protected function formatResult(string $message): string |
| 100 | { |
| 101 | return match ($this->statusCode()) { |
| 102 | self::PASSED => $this->formatPassed($message), |
| 103 | self::FAILED => $this->formatFailed($message), |
| 104 | default => $this->formatSkipped($message), |
| 105 | }; |
| 106 | } |
| 107 | |
| 108 | protected function formatPassed(string $message): string |
| 109 | { |
| 110 | return "<fg=white;bg=green> PASS <fg=green> $message</></>"; |
| 111 | } |
| 112 | |
| 113 | protected function formatFailed(string $message): string |
| 114 | { |
| 115 | return "<fg=gray;bg=yellow> FAIL <fg=yellow> $message</></>"; |
| 116 | } |
| 117 | |
| 118 | protected function formatSkipped(string $message): string |
| 119 | { |
| 120 | return "<fg=white;bg=gray> SKIP <fg=gray> $message</></>"; |
| 121 | } |
| 122 | |
| 123 | protected function formatTimeString(string $time): string |
| 124 | { |
| 125 | return "<fg=gray>({$time}ms)</>"; |
| 126 | } |
| 127 | |
| 128 | protected function formatTip(string $tip): string |
| 129 | { |
| 130 | return "<fg=gray>$tip</>"; |
| 131 | } |
| 132 | |
| 133 | protected function withMessage(?string $withMessage): static |
| 134 | { |
| 135 | if ($withMessage) { |
| 136 | $this->message = $withMessage; |
| 137 | } |
| 138 | |
| 139 | return $this; |
| 140 | } |
| 141 | } |