Documentation

InputValidator
in package

Centralized input validation for request parameters.

Provides methods for safely extracting and validating input from $_REQUEST, $_GET, $_POST, and $_FILES superglobals.

Tags
category

Lwt

author

HugoFara hugo.farajallah@protonmail.com

license

Unlicense http://unlicense.org/

link
https://hugofara.github.io/lwt/docs/php/
since
3.0.0

Table of Contents

Methods

getArray()  : array<string|int, mixed>
Get an array parameter from the request.
getBool()  : bool|null
Get a boolean parameter from the request.
getEmail()  : string
Get a valid email address from the request.
getEnum()  : string
Get a value from a predefined set of allowed values.
getFloat()  : float|null
Get a float parameter from the request.
getHtmlSafe()  : string
Get a sanitized string parameter for HTML output.
getInt()  : int|null
Get an integer parameter from the request.
getIntArray()  : array<string|int, int>
Get an array of integers from the request.
getIntEnum()  : int
Get an integer value from a predefined set of allowed values.
getIntParam()  : int
Get an integer parameter with a guaranteed non-null return.
getIntWithDb()  : int
Get an integer from request, persisting to database settings.
getJson()  : mixed
Get a JSON-decoded value from the request.
getMany()  : array<string, mixed>
Get multiple parameters at once.
getMethod()  : string
Get the request method (GET, POST, etc.).
getNonNegativeInt()  : int|null
Get a non-negative integer (>= 0) parameter from the request.
getPositiveInt()  : int|null
Get a positive integer (> 0) parameter from the request.
getString()  : string
Get a string parameter from the request.
getStringArray()  : array<string|int, string>
Get an array of strings from the request.
getStringFromGet()  : string
Get a string parameter from GET request.
getStringFromPost()  : string
Get a string parameter from POST request.
getStringMatching()  : string
Get a string that matches a regular expression pattern.
getStringWithDb()  : string
Get a string from request, persisting to database settings.
getUploadedFile()  : array<string|int, mixed>|null
Get an uploaded file's information.
getUploadedTextContent()  : string|null
Get the contents of an uploaded text file.
getUrl()  : string
Get a valid URL from the request.
has()  : bool
Check if a parameter exists in the request.
hasFromGet()  : bool
Check if a parameter exists in GET.
hasFromPost()  : bool
Check if a parameter exists in POST.
hasValue()  : bool
Check if a parameter exists and is not empty.
isGet()  : bool
Check if the request is a GET request.
isPost()  : bool
Check if the request is a POST request.
matchesPattern()  : bool
Validate that a string matches a regular expression pattern.
requireInt()  : int
Get a required integer parameter from the request.
sanitizeHtml()  : string
Sanitize a string for HTML output.

Methods

getArray()

Get an array parameter from the request.

public static getArray(string $key[, array<string|int, mixed> $default = [] ]) : array<string|int, mixed>
Parameters
$key : string

Parameter name

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

Default value if not set or invalid

Tags
psalm-return

array

Return values
array<string|int, mixed>

The validated array value

getBool()

Get a boolean parameter from the request.

public static getBool(string $key[, bool|null $default = null ]) : bool|null

Accepts: "1", "true", "yes", "on" for true "0", "false", "no", "off", "" for false

Parameters
$key : string

Parameter name

$default : bool|null = null

Default value if not set

Return values
bool|null

The validated boolean value

getEmail()

Get a valid email address from the request.

public static getEmail(string $key[, string $default = '' ]) : string
Parameters
$key : string

Parameter name

$default : string = ''

Default value if not set or invalid

Return values
string

The validated email or default

getEnum()

Get a value from a predefined set of allowed values.

public static getEnum(string $key, array<string|int, string> $allowed[, string $default = '' ]) : string
Parameters
$key : string

Parameter name

$allowed : array<string|int, string>

Array of allowed values

$default : string = ''

Default value if not set or not in allowed list

Return values
string

The validated value from the allowed set

getFloat()

Get a float parameter from the request.

public static getFloat(string $key[, float|null $default = null ][, float|null $min = null ][, float|null $max = null ]) : float|null
Parameters
$key : string

Parameter name

$default : float|null = null

Default value if not set or invalid

$min : float|null = null

Minimum allowed value (null for no minimum)

$max : float|null = null

Maximum allowed value (null for no maximum)

Return values
float|null

The validated float value, or null if invalid

getHtmlSafe()

Get a sanitized string parameter for HTML output.

public static getHtmlSafe(string $key[, string $default = '' ]) : string
Parameters
$key : string

Parameter name

$default : string = ''

Default value if not set

Return values
string

The sanitized string value

getInt()

Get an integer parameter from the request.

public static getInt(string $key[, int|null $default = null ][, int|null $min = null ][, int|null $max = null ]) : int|null
Parameters
$key : string

Parameter name

$default : int|null = null

Default value if not set or invalid

$min : int|null = null

Minimum allowed value (null for no minimum)

$max : int|null = null

Maximum allowed value (null for no maximum)

Return values
int|null

The validated integer value, or null if invalid

getIntArray()

Get an array of integers from the request.

public static getIntArray(string $key[, array<int, int> $default = [] ]) : array<string|int, int>

Filters out non-numeric values and returns only valid integers.

Parameters
$key : string

Parameter name

$default : array<int, int> = []

Default value if not set

Tags
psalm-param

list $default

psalm-return

list

Return values
array<string|int, int>

Array of validated integers

getIntEnum()

Get an integer value from a predefined set of allowed values.

public static getIntEnum(string $key, array<string|int, int> $allowed, int $default) : int
Parameters
$key : string

Parameter name

$allowed : array<string|int, int>

Array of allowed values

$default : int

Default value if not set or not in allowed list

Return values
int

The validated value from the allowed set

getIntParam()

Get an integer parameter with a guaranteed non-null return.

public static getIntParam(string $key, int $default[, int $min = PHP_INT_MIN ]) : int

Similar to getInt but always returns an integer, never null.

Parameters
$key : string

Parameter name

$default : int

Default value if not set or invalid

$min : int = PHP_INT_MIN

Minimum allowed value

Return values
int

The validated integer value

getIntWithDb()

Get an integer from request, persisting to database settings.

public static getIntWithDb(string $reqKey, string $dbKey[, int $default = 0 ]) : int

If the request parameter exists, saves it to database settings. Otherwise returns the database value or default.

Parameters
$reqKey : string

Request parameter key

$dbKey : string

Database settings key for persistence

$default : int = 0

Default value if neither exists

Return values
int

The current value

getJson()

Get a JSON-decoded value from the request.

public static getJson(string $key[, mixed $default = null ]) : mixed
Parameters
$key : string

Parameter name

$default : mixed = null

Default value if not set or invalid JSON

Return values
mixed

The decoded JSON value or default

getMany()

Get multiple parameters at once.

public static getMany(array<string, mixed> $schema) : array<string, mixed>
Parameters
$schema : array<string, mixed>

Array of key => default value pairs

Return values
array<string, mixed>

Array of validated values

getMethod()

Get the request method (GET, POST, etc.).

public static getMethod() : string
Return values
string

The request method in uppercase

getNonNegativeInt()

Get a non-negative integer (>= 0) parameter from the request.

public static getNonNegativeInt(string $key[, int|null $default = null ]) : int|null
Parameters
$key : string

Parameter name

$default : int|null = null

Default value if not set or invalid

Return values
int|null

The validated non-negative integer value

getPositiveInt()

Get a positive integer (> 0) parameter from the request.

public static getPositiveInt(string $key[, int|null $default = null ]) : int|null
Parameters
$key : string

Parameter name

$default : int|null = null

Default value if not set or invalid

Return values
int|null

The validated positive integer value

getString()

Get a string parameter from the request.

public static getString(string $key[, string $default = '' ][, bool $trim = true ]) : string
Parameters
$key : string

Parameter name

$default : string = ''

Default value if not set or invalid

$trim : bool = true

Whether to trim whitespace (default: true)

Return values
string

The validated string value

getStringArray()

Get an array of strings from the request.

public static getStringArray(string $key[, array<int, string> $default = [] ][, bool $trim = true ]) : array<string|int, string>

Filters out non-string values.

Parameters
$key : string

Parameter name

$default : array<int, string> = []

Default value if not set

$trim : bool = true

Whether to trim whitespace (default: true)

Tags
psalm-param

list $default

psalm-return

list

Return values
array<string|int, string>

Array of validated strings

getStringFromGet()

Get a string parameter from GET request.

public static getStringFromGet(string $key[, string $default = '' ][, bool $trim = true ]) : string
Parameters
$key : string

Parameter name

$default : string = ''

Default value if not set or invalid

$trim : bool = true

Whether to trim whitespace (default: true)

Return values
string

The validated string value

getStringFromPost()

Get a string parameter from POST request.

public static getStringFromPost(string $key[, string $default = '' ][, bool $trim = true ]) : string
Parameters
$key : string

Parameter name

$default : string = ''

Default value if not set or invalid

$trim : bool = true

Whether to trim whitespace (default: true)

Return values
string

The validated string value

getStringMatching()

Get a string that matches a regular expression pattern.

public static getStringMatching(string $key, string $pattern[, string $default = '' ]) : string
Parameters
$key : string

Parameter name

$pattern : string

Regular expression pattern

$default : string = ''

Default value if not set or doesn't match

Return values
string

The validated string or default

getStringWithDb()

Get a string from request, persisting to database settings.

public static getStringWithDb(string $reqKey, string $dbKey[, string $default = '' ]) : string

If the request parameter exists, saves it to database settings. Otherwise returns the database value or default.

Parameters
$reqKey : string

Request parameter key

$dbKey : string

Database settings key for persistence

$default : string = ''

Default value if neither exists

Return values
string

The current value

getUploadedFile()

Get an uploaded file's information.

public static getUploadedFile(string $key) : array<string|int, mixed>|null
Parameters
$key : string

Parameter name

Tags
psalm-return

array{name: string, type: string, tmp_name: string, error: int, size: int}|null

Return values
array<string|int, mixed>|null

File info array or null if not uploaded or error

getUploadedTextContent()

Get the contents of an uploaded text file.

public static getUploadedTextContent(string $key[, int $maxSize = 1048576 ][, bool $convertCrlf = true ]) : string|null
Parameters
$key : string

Parameter name

$maxSize : int = 1048576

Maximum file size in bytes (default: 1MB)

$convertCrlf : bool = true

Whether to convert CRLF to LF (default: true)

Return values
string|null

File contents or null if not uploaded or error

getUrl()

Get a valid URL from the request.

public static getUrl(string $key[, string $default = '' ]) : string
Parameters
$key : string

Parameter name

$default : string = ''

Default value if not set or invalid

Return values
string

The validated URL or default

has()

Check if a parameter exists in the request.

public static has(string $key) : bool
Parameters
$key : string

Parameter name

Return values
bool

True if the parameter exists

hasFromGet()

Check if a parameter exists in GET.

public static hasFromGet(string $key) : bool
Parameters
$key : string

Parameter name

Return values
bool

True if the parameter exists in GET

hasFromPost()

Check if a parameter exists in POST.

public static hasFromPost(string $key) : bool
Parameters
$key : string

Parameter name

Return values
bool

True if the parameter exists in POST

hasValue()

Check if a parameter exists and is not empty.

public static hasValue(string $key) : bool
Parameters
$key : string

Parameter name

Return values
bool

True if the parameter exists and is not empty

isGet()

Check if the request is a GET request.

public static isGet() : bool
Return values
bool

True if this is a GET request

isPost()

Check if the request is a POST request.

public static isPost() : bool
Return values
bool

True if this is a POST request

matchesPattern()

Validate that a string matches a regular expression pattern.

public static matchesPattern(string $value, string $pattern) : bool
Parameters
$value : string

Value to validate

$pattern : string

Regular expression pattern (empty pattern returns false)

Return values
bool

True if the value matches the pattern

requireInt()

Get a required integer parameter from the request.

public static requireInt(string $key[, int|null $min = null ][, int|null $max = null ]) : int
Parameters
$key : string

Parameter name

$min : int|null = null

Minimum allowed value (null for no minimum)

$max : int|null = null

Maximum allowed value (null for no maximum)

Tags
throws
InvalidArgumentException

If the parameter is missing or invalid

Return values
int

The validated integer value

sanitizeHtml()

Sanitize a string for HTML output.

public static sanitizeHtml(string $value) : string
Parameters
$value : string

String to sanitize

Return values
string

Sanitized string


        
On this page

Search results