Validates that a value is of a specific type.
If an array with multiple types is provided, it will validate if the value is of at least one of the given types.
For example, if ['alpha', 'numeric']
is provided, it will validate if the value is of type alpha
or of type numeric
.
Type(
string|array $constraint,
?string $message = null
);
// single type
Validator::type('string')->validate('green'); // true
Validator::type('alphanumeric')->validate('gr33n'); // true
// multiple types
// validates if value is of at least one of the provided types
Validator::type(['alpha', 'numeric'])->validate('green'); // true (alpha)
Validator::type(['alpha', 'numeric'])->validate('33'); // true (numeric)
Validator::type(['alpha', 'numeric'])->validate('gr33n'); // false (not alpha nor numeric)
// class or interface type
Validator::type(\DateTime::class)->validate(new \DateTime()); // true
Validator::type(\DateTimeInterface::class)->validate(new \DateTime()); // true
Note
An UnexpectedValueException
will be thrown when a constraint type, class or interface is invalid.
type: string
|array
required
Type(s) to validate the input value type. Can validate instances of classes and interfaces.
If an array with multiple types is provided, it will validate if the value is of at least one of the given types.
For example, if ['alpha', 'numeric']
is provided, it will validate if the value is of type alpha
or of type numeric
.
Available data type constraints:
bool
,boolean
int
,integer
,long
float
,double
,real
numeric
string
scalar
array
iterable
countable
callable
object
resource
null
Available character type constraints:
alphanumeric
alpha
digit
control
punctuation
hexadecimal
graph
printable
whitespace
lowercase
uppercase
type: ?string
default: The {{ name }} value should be of type {{ constraint }}, {{ value }} given.
Message that will be shown if input value is not of a specific type.
The following parameters are available:
Parameter | Description |
---|---|
{{ value }} |
The current invalid value |
{{ name }} |
Name of the invalid value |
{{ constraint }} |
The valid type(s) |
0.2.0
Created