Skip to content

Commit

Permalink
Merge pull request #608 from hydephp/refactor-validation-service
Browse files Browse the repository at this point in the history
Refactor validation service
  • Loading branch information
caendesilva authored Oct 27, 2022
2 parents 712c98b + 1f5ba86 commit d9af621
Showing 1 changed file with 26 additions and 22 deletions.
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;
}
}

0 comments on commit d9af621

Please sign in to comment.