TypeChecker
A generic data type checker.
This allows to set a list of allowed atomic data types and fully qualified class names. It also provides a method to check if a value's data type matches at least one of these types.
The following lowercase keywords are recognized as atomic data types:
array, bool, callable, countable, double, float, int,
integer, iterable, long, null, numeric, object, resource,
scalar and string. Any other value is treated as the name of a class
or interface the value has to be an instance of. Additionally, mixed
allows any value.
Like in PHP's own type declarations, union types (e.g. int|string) and
nullable types (e.g. ?int) are supported. They are split into separate
types when set (e.g. ?int becomes null and int).
Tags
Table of Contents
Methods
- getAllowedTypes() : AllowedTypes
- Get allowed data types.
- hasAllowedType() : bool
- Check if a value's data type is allowed.
- isAllowedType() : bool
- Check if a data type is allowed.
- setAllowedTypes() : void
- Set allowed data types.
Methods
getAllowedTypes()
Get allowed data types.
public
getAllowedTypes() : AllowedTypes
Return values
AllowedTypes —The list of allowed data types
hasAllowedType()
Check if a value's data type is allowed.
public
hasAllowedType(mixed $value) : bool
Parameters
- $value : mixed
-
The value to check
Return values
bool —Whether the value's data type is allowed
isAllowedType()
Check if a data type is allowed.
public
isAllowedType(string $type) : bool
For union and nullable types, all of their types have to be allowed.
Parameters
- $type : string
-
The type to check
Tags
Return values
bool —Whether the data type is allowed
setAllowedTypes()
Set allowed data types.
public
setAllowedTypes([AllowedTypes $allowedTypes = [] ]) : void
Parameters
- $allowedTypes : AllowedTypes = []
-
Allowed data types