Skip to content

Commit

Permalink
Removed passed and failed public properties from Validation state. Ex…
Browse files Browse the repository at this point in the history
…istence or lack of errors is used to determine that.
  • Loading branch information
phrenotype committed Sep 13, 2023
1 parent 526e51e commit 66b5fee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
17 changes: 3 additions & 14 deletions Helga/RuleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public function parse($subject, $key = null)

if (is_string(array_keys($this->__rules__)[0])) {
$state->isSingle = false;
$state->failed = true;
$state->passed = false;
foreach ($this->__rules__ as $k => $rule) {
if (!isset($state->errors[$k])) {
$state->errors[$k] = [];
Expand All @@ -37,8 +35,6 @@ public function parse($subject, $key = null)
$validator = new Validator($value, $k);
$validator->withRules($rule);
$state->errors[$k] = array_merge($state->errors[$k], $validator->errors());
$state->failed = ($state->failed || $validator->fails());
$state->passed = ($state->passed && $validator->passes());
}
foreach ($state->errors as $i => $arr) {
if (empty($arr)) {
Expand Down Expand Up @@ -78,14 +74,14 @@ public function parse($subject, $key = null)

if ($key) {
$params[] = $key;
}
}

if (!method_exists(Executioner::class, $function)) {
throw new \Error(sprintf("Unknown rule directive '%s'.", $function));
}

$eval = call_user_func_array(Executioner::class . "::" . $function, $params);
$eval = call_user_func_array(Executioner::class . "::" . $function, $params);

if (!$eval->return) {
if ($message) {
$eval->message = $message;
Expand All @@ -97,13 +93,6 @@ public function parse($subject, $key = null)
throw new \Error("Invalid rules format");
}

if (empty($errors)) {
$state->failed = false;
$state->passed = true;
} else {
$state->failed = true;
$state->passed = false;
}
$state->errors = $errors;
return $state;
}
Expand Down
10 changes: 8 additions & 2 deletions Helga/ValidationState.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
class ValidationState
{
public $isSingle;
public $passed;
public $failed;
public $errors = [];

public function passed(){
return empty($this->errors) === true;
}

public function failed(){
return empty($this->errors) === false;
}
}
10 changes: 5 additions & 5 deletions Helga/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public function __construct($subject, $key = null)
public function passes()
{
$vs = $this->eval();
if ($vs->passed) {
if ($vs->passed()) {
return true;
} else {
} else if($vs->failed()) {
return false;
}
}
Expand All @@ -79,9 +79,9 @@ public function passes()
public function fails()
{
$vs = $this->eval();
if ($vs->failed) {
if ($vs->failed()) {
return true;
} else {
} else if($vs->passed()) {
return false;
}
}
Expand Down Expand Up @@ -242,7 +242,7 @@ public function withRules(array $rules)

private function eval()
{
$vs = (new RuleParser($this->__rules__))->parse($this->__subject__, $this->__key__);
$vs = (new RuleParser($this->__rules__))->parse($this->__subject__, $this->__key__);
$this->__errors__ = $vs->errors;
return $vs;
}
Expand Down

0 comments on commit 66b5fee

Please sign in to comment.