fix: pass scope for all eval methods

This commit is contained in:
Victor Westerlund 2023-11-20 15:14:56 +01:00
parent 7fc27ff89c
commit 8a46db2545
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) { return match($this->type) {
Type::NUMBER => $this->eval_type($value) && $value >= $this->min, Type::NUMBER => $this->eval_type($value, $scope) && $value >= $this->min,
Type::STRING => $this->eval_type($value) && strlen($value) >= $this->min, Type::STRING => $this->eval_type($value, $scope) && strlen($value) >= $this->min,
Type::ARRAY, 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 default => true
}; };
} }
public function eval_max(mixed $value): bool { public function eval_max(mixed $value, Scope $scope): bool {
return match($this->type) { return match($this->type) {
Type::NUMBER => $this->eval_type($value) && $value <= $this->max, Type::NUMBER => $this->eval_type($value, $scope) && $value <= $this->max,
Type::STRING => $this->eval_type($value) && strlen($value) <= $this->max, Type::STRING => $this->eval_type($value, $scope) && strlen($value) <= $this->max,
Type::ARRAY, 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 default => true
}; };
} }

View file

@ -84,11 +84,11 @@
$this->add_error($name, "Value must be of type '{$rules->type->name}'"); $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}"); $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}"); $this->add_error($name, "Value must be smaller or equal to {$rules->max}");
} }
} }