Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor validation service #608

Merged
merged 18 commits into from
Oct 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions packages/framework/src/Support/Models/ValidationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
*/
class ValidationResult
{
public const PASSED = 0;
public const SKIPPED = 1;
public const FAILED = 2;

public string $message;
public string $tip;

Expand All @@ -24,31 +28,22 @@ public function __construct(string $defaultMessage = 'Generic check')
public function pass(?string $withMessage = null): static
{
$this->passed = true;
if ($withMessage) {
$this->message = $withMessage;
}

return $this;
return $this->withMessage($withMessage);
}

public function fail(?string $withMessage = null): static
{
$this->passed = false;
if ($withMessage) {
$this->message = $withMessage;
}

return $this;
return $this->withMessage($withMessage);
}

public function skip(?string $withMessage = null): static
{
$this->skipped = true;
if ($withMessage) {
$this->message = $withMessage;
}

return $this;
return $this->withMessage($withMessage);
}

public function withTip(string $withTip): static
Expand Down Expand Up @@ -81,13 +76,13 @@ public function failed(): bool
public function statusCode(): int
{
if ($this->skipped()) {
return 1;
return self::SKIPPED;
}
if ($this->passed()) {
return 0;
return self::PASSED;
}

return 2;
return self::FAILED;
}

public function message(): string
Expand All @@ -108,34 +103,43 @@ public function formattedMessage(?string $withTimeString = null): string
protected function formatResult(string $message): string
{
return match ($this->statusCode()) {
0 => $this->formatPassed($message),
2 => $this->formatFailed($message),
self::PASSED => $this->formatPassed($message),
self::FAILED => $this->formatFailed($message),
default => $this->formatSkipped($message),
};
}

protected function formatPassed(string $message): string
{
return '<fg=white;bg=green> PASS <fg=green> '.$message.'</></>';
return "<fg=white;bg=green> PASS <fg=green> $message</></>";
}

protected function formatFailed(string $message): string
{
return '<fg=gray;bg=yellow> FAIL <fg=yellow> '.$message.'</></>';
return "<fg=gray;bg=yellow> FAIL <fg=yellow> $message</></>";
}

protected function formatSkipped(string $message): string
{
return '<fg=white;bg=gray> SKIP <fg=gray> '.$message.'</></>';
return "<fg=white;bg=gray> SKIP <fg=gray> $message</></>";
}

protected function formatTimeString(string $time): string
{
return '<fg=gray> ('.$time.'ms)</>';
return "<fg=gray> ({$time}ms)</>";
}

protected function formatTip(string $tip): string
{
return '<fg=gray>'.$tip.'</>';
return "<fg=gray>$tip</>";
}

protected function withMessage(?string $withMessage): static
{
if ($withMessage) {
$this->message = $withMessage;
}

return $this;
}
}