fix: pass scope for all eval methods (#4)

This commit is contained in:
Victor Westerlund 2023-11-20 15:15:34 +01:00 committed by GitHub
parent 7fc27ff89c
commit 6583f87866
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 10 deletions

View file

@ -141,22 +141,22 @@
};
}
public function eval_min(mixed $value): bool {
public function eval_min(mixed $value, Scope $scope): bool {
return match($this->type) {
Type::NUMBER => $this->eval_type($value) && $value >= $this->min,
Type::STRING => $this->eval_type($value) && strlen($value) >= $this->min,
Type::NUMBER => $this->eval_type($value, $scope) && $value >= $this->min,
Type::STRING => $this->eval_type($value, $scope) && strlen($value) >= $this->min,
Type::ARRAY,
Type::OBJECT => $this->eval_type($value) && count($value) >= $this->min,
Type::OBJECT => $this->eval_type($value, $scope) && count($value) >= $this->min,
default => true
};
}
public function eval_max(mixed $value): bool {
public function eval_max(mixed $value, Scope $scope): bool {
return match($this->type) {
Type::NUMBER => $this->eval_type($value) && $value <= $this->max,
Type::STRING => $this->eval_type($value) && strlen($value) <= $this->max,
Type::NUMBER => $this->eval_type($value, $scope) && $value <= $this->max,
Type::STRING => $this->eval_type($value, $scope) && strlen($value) <= $this->max,
Type::ARRAY,
Type::OBJECT => $this->eval_type($value) && count($value) <= $this->max,
Type::OBJECT => $this->eval_type($value, $scope) && count($value) <= $this->max,
default => true
};
}

View file

@ -84,11 +84,11 @@
$this->add_error($name, "Value must be of type '{$rules->type->name}'");
}
if ($rules->min && !$rules->eval_min($value)) {
if ($rules->min && !$rules->eval_min($value, $scope)) {
$this->add_error($name, "Value must be larger or equal to {$rules->min}");
}
if ($rules->max && !$rules->eval_max($value)) {
if ($rules->max && !$rules->eval_max($value, $scope)) {
$this->add_error($name, "Value must be smaller or equal to {$rules->max}");
}
}