DataTypeHelper
in package
Tags
Table of Contents
Constants
- ARRAY = 'array'
- BOOLEAN = 'boolean'
- DOUBLE = 'double'
- FLOAT = 'float'
- INTEGER = 'integer'
- NULL = 'NULL'
- OBJECT = 'object'
- RESOURCE = 'resource'
- RESOURCE_CLOSED = 'resource (closed)'
- STRING = 'string'
- TYPE_CHECK_INVALID_TYPE = 8
- TYPE_CHECK_INVALID_TYPE_PARAMETER = 2
- TYPE_CHECK_INVALID_VALUE_PARAMETER = 1
- TYPE_CHECK_NON_EXISTING_CLASS = 16
- TYPE_CHECK_TYPE_NOT_IN_LIST = 32
- TYPE_CHECK_VALUE_IS_EMPTY = 4
- TYPE_CHECK_VALUE_IS_INSTANCE = 64
- TYPE_CHECK_VALUE_IS_NULL = 128
- UNKNOWN_TYPE = 'unknown type'
Methods
- classUsesTraits() : array<string|int, mixed>
- Method evaluates all the traits used in an object/class, including the ones inherited from parent classes
- ensureClassType() : void
- Ensures that the provided `$value` is of or implements any class, interface or trait as provided by the `$allowedTypes` parameter. The function throws an Exception if none of the provided types is matched. Please see self::matchClassType() for more information on the parameters.
- ensureType() : void
- Ensures that the provided `$value` is of or implements any type as provided by the `$allowedTypes` parameter.
- filterBool() : bool|null
- Returns the boolean value of `$value`, or NULL if it's not a boolean and cannot be converted.
- filterFloat() : float|null
- Returns the boolean value of `$value`, or NULL if it's not a float and cannot be converted.
- filterInt() : int|null
- Returns the boolean value of `$value`, or NULL if it's not an integer and cannot be converted.
- filterScalar() : bool|int|float|string|null
- Returns the boolean value of `$value`, or NULL if it's not a scalar.
- filterString() : string|null
- Returns the boolean value of `$value`, or NULL if it's not a string and cannot be converted.
- isClassType() : bool
- isType() : bool
- matchClassType() : string|null
- Checks if the class or object has one of the given classes, interfaces or traits as one of its parents or implements it.
- matchType() : string|null
- matchTypeHelper() : string|null
- parseTypes() : array<string|int, string>
- filterType() : mixed|null
- Helper variable that returns the input `$value` if it is matched against the `$allowedTypes`
Constants
ARRAY
public
mixed
ARRAY
= 'array'
BOOLEAN
public
mixed
BOOLEAN
= 'boolean'
DOUBLE
public
mixed
DOUBLE
= 'double'
FLOAT
public
mixed
FLOAT
= 'float'
INTEGER
public
mixed
INTEGER
= 'integer'
NULL
public
mixed
NULL
= 'NULL'
OBJECT
public
mixed
OBJECT
= 'object'
RESOURCE
public
mixed
RESOURCE
= 'resource'
RESOURCE_CLOSED
public
mixed
RESOURCE_CLOSED
= 'resource (closed)'
STRING
public
mixed
STRING
= 'string'
TYPE_CHECK_INVALID_TYPE
public
mixed
TYPE_CHECK_INVALID_TYPE
= 8
TYPE_CHECK_INVALID_TYPE_PARAMETER
public
mixed
TYPE_CHECK_INVALID_TYPE_PARAMETER
= 2
TYPE_CHECK_INVALID_VALUE_PARAMETER
public
mixed
TYPE_CHECK_INVALID_VALUE_PARAMETER
= 1
TYPE_CHECK_NON_EXISTING_CLASS
public
mixed
TYPE_CHECK_NON_EXISTING_CLASS
= 16
TYPE_CHECK_TYPE_NOT_IN_LIST
public
mixed
TYPE_CHECK_TYPE_NOT_IN_LIST
= 32
TYPE_CHECK_VALUE_IS_EMPTY
public
mixed
TYPE_CHECK_VALUE_IS_EMPTY
= 4
TYPE_CHECK_VALUE_IS_INSTANCE
public
mixed
TYPE_CHECK_VALUE_IS_INSTANCE
= 64
TYPE_CHECK_VALUE_IS_NULL
public
mixed
TYPE_CHECK_VALUE_IS_NULL
= 128
UNKNOWN_TYPE
public
mixed
UNKNOWN_TYPE
= 'unknown type'
Methods
classUsesTraits()
Method evaluates all the traits used in an object/class, including the ones inherited from parent classes
public
static classUsesTraits(string|object $class[, bool $autoload = true ]) : array<string|int, mixed>
Parameters
- $class : string|object
-
Class name or instance to be checked
- $autoload : bool = true
-
Indicates whether autoload should be performed for classes that are not yet loaded.
Tags
Return values
array<string|int, mixed> —an array of trait names used by the given class
ensureClassType()
Ensures that the provided `$value` is of or implements any class, interface or trait as provided by the `$allowedTypes` parameter. The function throws an Exception if none of the provided types is matched. Please see self::matchClassType() for more information on the parameters.
public
static ensureClassType(mixed $value, mixed $allowedTypes) : void
Parameters
- $value : mixed
- $allowedTypes : mixed
Tags
ensureType()
Ensures that the provided `$value` is of or implements any type as provided by the `$allowedTypes` parameter.
public
static ensureType(mixed $value, mixed $allowedTypes) : void
The function throws an Exception if none of the provided types is matched. Please see self::matchType() for more information on the parameters.
Parameters
- $value : mixed
- $allowedTypes : mixed
Tags
filterBool()
Returns the boolean value of `$value`, or NULL if it's not a boolean and cannot be converted.
public
static filterBool(mixed $value[, bool $strict = false ][, bool $throwException = false ]) : bool|null
See the parameter description for more information.
Parameters
- $value : mixed
-
value to be tested or converted
- $strict : bool = false
-
indicates if strict comparison should be performed: ``
- if TRUE,
$value
must already be of typeboolean
. In that case, its value is returned, NULL otherwise. - if FALSE, a conversion to
boolean
is attempted using(bool)
. No exception is thrown, ever. Note: "false", "off", "no" yield TRUE in this case. - if NULL, a conversion to
boolean
is attempted, where "1", "true", "on", and "yes" yield TRUE, "0", "false", "off", "no", and "" yield FALSE. ``
- if TRUE,
- $throwException : bool = false
-
throws an exception instead of returning
null
Tags
Return values
bool|null —see $strict
parameter for details
filterFloat()
Returns the boolean value of `$value`, or NULL if it's not a float and cannot be converted.
public
static filterFloat(mixed $value[, bool $strict = false ][, bool $throwException = false ]) : float|null
See the parameter description for more information.
Parameters
- $value : mixed
-
value to be tested or converted
- $strict : bool = false
-
indicates if strict comparison should be performed: ``
- if TRUE,
$value
must already be of typefloat
. - if FALSE, a conversion to
float
is attempted. ``
- if TRUE,
- $throwException : bool = false
-
throws an exception instead of returning
null
Tags
Return values
float|nullfilterInt()
Returns the boolean value of `$value`, or NULL if it's not an integer and cannot be converted.
public
static filterInt(mixed $value[, bool $strict = false ][, bool $throwException = false ]) : int|null
See the parameter description for more information.
Parameters
- $value : mixed
-
value to be tested or converted
- $strict : bool = false
-
indicates if strict comparison should be performed: ``
- if TRUE,
$value
must already be of typeint
. - if FALSE, a conversion to
int
is attempted. ``
- if TRUE,
- $throwException : bool = false
-
throws an exception instead of returning
null
Tags
Return values
int|nullfilterScalar()
Returns the boolean value of `$value`, or NULL if it's not a scalar.
public
static filterScalar(mixed $value[, bool $strict = false ][, bool $throwException = false ]) : bool|int|float|string|null
See the parameter description for more information.
Parameters
- $value : mixed
-
value to be tested or converted
- $strict : bool = false
-
indicates if strict comparison should be performed: ``
- if TRUE,
$value
must already be a scalar value. - if FALSE,
NULL
is also allowed (not throwing an exception, if$throw
is TRUE). ``
- if TRUE,
- $throwException : bool = false
-
throws an exception instead of returning
null
Tags
Return values
bool|int|float|string|nullfilterString()
Returns the boolean value of `$value`, or NULL if it's not a string and cannot be converted.
public
static filterString(mixed $value[, bool $strict = false ][, bool $throwException = false ]) : string|null
See the parameter description for more information.
Parameters
- $value : mixed
-
value to be tested or converted
- $strict : bool = false
-
indicates if strict comparison should be performed: ``
- if TRUE,
$value
must already be of typestring
. - if FALSE, a conversion to
string
is attempted. ``
- if TRUE,
- $throwException : bool = false
-
throws an exception instead of returning
null
Tags
Return values
string|nullisClassType()
public
static isClassType(mixed $value, string|array<string|int, string>|array<string|int, object> $allowedTypes) : bool
Parameters
- $value : mixed
-
Variable to be checked.
- $allowedTypes : string|array<string|int, string>|array<string|int, object>
-
Allowed types.
Tags
Return values
boolisType()
public
static isType(mixed $value, string|array<string|int, string>|array<string|int, object> $allowedTypes) : bool
Parameters
- $value : mixed
-
Variable to be checked.
- $allowedTypes : string|array<string|int, string>|array<string|int, object>
-
Allowed types.
Tags
Return values
boolmatchClassType()
Checks if the class or object has one of the given classes, interfaces or traits as one of its parents or implements it.
public
static matchClassType(string|object|null|mixed $value, string|array<string|int, string>|array<string|int, object> $allowedTypes[, bool $throwException = false ]) : string|null
Parameters
- $value : string|object|null|mixed
-
Object or classname to be checked. Null may be valid if included in $type. Everything else is invalid and either throws an error (default) or returns NULL, if $throw is false.
- $allowedTypes : string|array<string|int, string>|array<string|int, object>
-
(List of) allowed class, interface or trait names, or object instances. Object instances may only be passed as part of an array. In such a case, the object's type/class is used for comparison. If a string is provided, it will be split by
|
. If NULL value or the "NULL" string is included, NULL values are also allowed. * - $throwException : bool = false
-
throws an exception instead of returning
null
. Code of the thrown Exception is a bit-mask consisting of the following bits: ``- self::CLASS_CHECK_INVALID_CLASSNAME_PARAMETER: Invalid $className parameter
- self::CLASS_CHECK_INVALID_TYPE_PARAMETER: Invalid $type parameter
- self::CLASS_CHECK_VALUE_IS_EMPTY: Empty parameter
- self::CLASS_CHECK_INVALID_TYPE: Invalid type
- self::CLASS_CHECK_NON_EXISTING_CLASS: Non-existing class
- self::CLASS_CHECK_TYPE_NOT_IN_LIST: Class that is not in $type parameter
- self::CLASS_CHECK_VALUE_IS_INSTANCE: $className is an object instance
- self::CLASS_CHECK_VALUE_IS_NULL: NULL value ``
Tags
Return values
string|nullmatchType()
public
static matchType(mixed $value, string|array<string|int, string>|array<string|int, object> $allowedTypes[, bool $throwException = false ]) : string|null
Parameters
- $value : mixed
-
Variable to be checked.
- $allowedTypes : string|array<string|int, string>|array<string|int, object>
-
Allowed types. Valid input are ``
- simple type names as returned by gettype()
-
null
value or'NULL'
string - class, interface, or trait names
- class instances whose class type will be checked
-
callable
, e.g.is_scalar
`` rather that it's name.
- $throwException : bool = false
Tags
Return values
string|null —Returns the first match of $value
against the $allowedTypes
.
``
- If the matched type is a
NULL
value, the string "NULL" is returned. - If the matched type is an object instance, its class name is returned.
- If the matched type is a
callable
, the callable´s string representation (name) is returned. - If no match is found, a
NULL
value is returned. ``
matchTypeHelper()
protected
static matchTypeHelper(mixed $typeToCheck, mixed &$input, string $inputType[, array<string|int, mixed>|null &$inputTraits = null ]) : string|null
Parameters
- $typeToCheck : mixed
- $input : mixed
- $inputType : string
- $inputTraits : array<string|int, mixed>|null = null
Return values
string|nullparseTypes()
protected
static parseTypes(mixed &$allowedTypes[, bool|null &$allowNull = false ][, array<string|int, mixed>|null &$checkTraits = null ][, bool $allowCallables = true ][, bool $allowGetTypes = true ]) : array<string|int, string>
Parameters
- $allowedTypes : mixed
- $allowNull : bool|null = false
- $checkTraits : array<string|int, mixed>|null = null
- $allowCallables : bool = true
- $allowGetTypes : bool = true
Tags
Return values
array<string|int, string>filterType()
Helper variable that returns the input `$value` if it is matched against the `$allowedTypes`
private
static filterType(mixed $value, mixed $allowedTypes[, bool $throwException = false ]) : mixed|null
Parameters
- $value : mixed
- $allowedTypes : mixed
- $throwException : bool = false
Tags
Return values
mixed|null —The $value
if it matches any type given in $allowedTypes, or NULL otherwise