Oihana PHP System

getParamNumberRange.php

Table of Contents

Functions

getParamNumberRange()  : int|float|null
Retrieves a numeric parameter from the request and clamps it within a given range.

Functions

getParamNumberRange()

Retrieves a numeric parameter from the request and clamps it within a given range.

getParamNumberRange(ServerRequestInterface|null $request, string $name, int|float $min, int|float $max[, int|float|null $defaultValue = null ][, array<string|int, mixed> $args = [] ][, string $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : int|float|null

This helper calls getParam() and:

  • Converts the value to int or float.
  • Returns $defaultValue if the parameter is missing or not numeric.
  • Clamps the value between $min and $max.
  • Can throw NotFoundException if $throwable is true and parameter is missing.
Parameters
$request : ServerRequestInterface|null

The PSR-7 request instance.

$name : string

Parameter name or dot-notated path.

$min : int|float

Minimum allowed value.

$max : int|float

Maximum allowed value.

$defaultValue : int|float|null = null

Value returned if missing or invalid. Default null.

$args : array<string|int, mixed> = []

Optional defaults passed to getParam().

$strategy : string = HttpParamStrategy::BOTH

Source to search: BOTH, QUERY, BODY. Default BOTH.

$throwable : bool = false

Whether to throw NotFoundException if missing. Default false.

Tags
throws
NotFoundException

If $throwable is true and the parameter is not found.

example
// Query: ?price=15
$price = getParamNumberRange($request, 'price', 0, 100, 10); // 15

// Body: ['discount' => 150]
$discount = getParamNumberRange($request, 'discount', 0, 100, 0); // 100 (clamped)

// Missing value
$tax = getParamNumberRange($request, 'tax', 0, 50, 5); // 5 (default)
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
int|float|null

The numeric value clamped to the range [$min, $max], or $defaultValue/null if missing or invalid.


        
On this page

Search results