Skip to content

Commit

Permalink
Collect validation type
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed Jan 25, 2022
1 parent 2c4e94b commit 009dfb3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/Debug/ValidationCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,40 @@ public function getContents() : string

protected function renderErrors() : string
{
if ( ! $this->validation->getErrors()) {
return '<p>No errors.</p>';
if ( ! $this->hasData()) {
return '<p>No data has been validated.</p>';
}
$data = $this->getData();
$data = $data[\array_key_last($data)];
$errors = $this->validation->getErrors();
$countErrors = \count($errors);
\ob_start(); ?>
<p>The following errors occurred:</p>
<p>Validated <?=
$data['type'] === 'all'
? 'all rules against the data'
: 'only the rules with fields set in the data'
?> in <?= \round($data['end'] - $data['start'], 6) ?> seconds and <?=
$countErrors
? $countErrors . ' error' . ($countErrors === 1 ? '' : 's') . ' occurred:'
: 'no error occurred.'
?></p>
<?php
if ( ! $errors) {
return \ob_get_clean(); // @phpstan-ignore-line
}
$currentError = 1; ?>
<table>
<thead>
<tr>
<th>#</th>
<th>Field</th>
<th>Error Message</th>
</tr>
</thead>
<tbody>
<?php foreach ($this->validation->getErrors() as $field => $error): ?>
<?php foreach ($errors as $field => $error): ?>
<tr>
<td><?= $currentError++ ?></td>
<td><?= \htmlentities($field) ?></td>
<td><?= \htmlentities($error) ?></td>
</tr>
Expand Down
43 changes: 43 additions & 0 deletions src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Framework\Helpers\ArraySimple;
use Framework\Language\Language;
use Framework\Validation\Debug\ValidationCollector;
use InvalidArgumentException;
use JetBrains\PhpStorm\Pure;

Expand Down Expand Up @@ -61,6 +62,7 @@ class Validation
* @var Language
*/
protected Language $language;
protected ValidationCollector $debugCollector;

/**
* Validation constructor.
Expand Down Expand Up @@ -505,6 +507,18 @@ protected function run(array $fieldRules, array $data) : bool
*/
public function validate(array $data) : bool
{
if (isset($this->debugCollector)) {
$start = \microtime(true);
$validated = $this->run($this->getRules(), $data);
$end = \microtime(true);
$this->debugCollector->addData([
'start' => $start,
'end' => $end,
'validated' => $validated,
'type' => 'all',
]);
return $validated;
}
return $this->run($this->getRules(), $data);
}

Expand All @@ -516,6 +530,28 @@ public function validate(array $data) : bool
* @return bool
*/
public function validateOnly(array $data) : bool
{
if (isset($this->debugCollector)) {
$start = \microtime(true);
$validated = $this->validateOnlySet($data);
$end = \microtime(true);
$this->debugCollector->addData([
'start' => $start,
'end' => $end,
'validated' => $validated,
'type' => 'only',
]);
return $validated;
}
return $this->validateOnlySet($data);
}

/**
* @param array<string,mixed> $data
*
* @return bool
*/
protected function validateOnlySet(array $data) : bool
{
$fieldRules = \array_intersect_key(
$this->getRules(),
Expand All @@ -540,4 +576,11 @@ public function isRuleAvailable(string $rule) : bool
}
return false;
}

public function setDebugCollector(ValidationCollector $debugCollector) : static
{
$this->debugCollector = $debugCollector;
$this->debugCollector->setValidation($this);
return $this;
}
}

0 comments on commit 009dfb3

Please sign in to comment.