Skip to content

Commit

Permalink
Server variables can contain floats - REQUEST_TIME_FLOAT
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Oct 15, 2023
1 parent d2a58c8 commit 770da67
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,33 @@ public function array(string $parameter): array
return array_reduce($this->rules, $callback, $value) ?? [];
}

/**
* @param string $parameter
* @param float|null $default
*
* @return float
*/
public function float(string $parameter, float $default = null): float
{
$value = $this->parameters[$parameter] ?? null;

if (is_numeric($value)) {
$value = (float) $value;
} else {
$value = null;
}

$callback = static fn (?float $value, Closure $rule): ?float => $rule($value);

$value = array_reduce($this->rules, $callback, $value) ?? $default;

if ($value === null) {
throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter));
}

return $value;
}

/**
* @param string $parameter
* @param int|null $default
Expand Down

0 comments on commit 770da67

Please sign in to comment.