Explicit handling of type coercions and enforcement in PHP in order to deal with unknown data in a more predictable and safe way.
composer require squirrelphp/types
- Introduction
- Coercion behavior overview
- Test if value can be coerced
- Coerce a value
- Enforce a type for a value
When getting values from a database or an API (or even just another PHP component you do not know) you have an expectation of what type of value you will get, but you might get something more or less different.
A database might give you the string "1" for a boolean, or the integer 1, and that would often be fine (many databases have no boolean type, so some coercion is often necessary). But getting the string "hello" or the integer -30 should not be fine for a boolean and most likely points to a mistake - maybe the expected type is wrong, or the wrong field in the database was retrieved.
This small component coerces and enforces types and is fully tested to behave in a predictable way. This should often be better than using other coercion methods like explicit casts (bool)
/ boolval()
, as these will coerce any value, while this component will reject unreasonable values and throw a TypeError. It coerces less values than PHP does in coercive typing mode, as PHP accepts quite a few questionable values for legacy/BC reasons.
All argument flags mentioned below are set to false by default for a more conservative coercion behavior. Enable them only if it is necessary for your use case.
- Always accepts bools
- Accepts "0" and "1" strings
- Accepts 0 and 1 ints
- Only allows "" string if the argument
$allowEmptyString
is set to true - Only allows 0.0 and 1.0 floats if the argument
$allowFloat
is set to true
- Always accepts ints
- Accepts floats and numeric strings without a fractional part
- Only allows bools if the argument
$allowBool
is set to true
- Always accepts ints and floats
- Accepts numeric strings
- Only allows bools if the argument
$allowBool
is set to true
- Always accepts strings
- Accepts any ints and floats
- Only allows bools if the argument
$allowBool
is set to true - Only allows Stringable objects if the argument
$allowStringable
is set to true
All these functions have the mixed $value
as their first argument and return true or false:
Returns true if $value
is one of the following:
- A boolean
- A string with value '0' or '1'
- An int with value 0 or 1
- An empty string - only if the argument
$allowEmptyString
is set to true - A float with value 0 or 1 - only if the
$allowFloat
argument is set to true
For any other values it returns false.
Returns true if $value
is one of the following:
- An integer
- A float without fractional part
- A numeric string without fractional part
- A boolean - only if the
$allowBool
argument is set to true
For any other values it returns false.
Returns true if $value
is one of the following:
- An integer or float
- A numeric string
- A boolean - only if the
$allowBool
argument is set to true
For any other values it returns false.
Returns true if $value
is one of the following:
- A string
- An integer or float
- A boolean - only if the
$allowBool
argument is set to true - A Stringable object - only if the
$allowStringable
argument is set to true
For any other values it returns false.
Specifically tests a string if it can be coerced to a boolean. $value
must be '0' or '1'.
Specifically tests an integer if it can be coerced to a boolean. $value
must be 0 or 1.
Specifically tests a float if it can be coerced to a boolean. $value
must be 0 or 1.
Specifically tests a float if it can be coerced to an integer. $value
must not have a fractional part.
Specifically tests a string if it can be coerced to an integer. $value
must be a numeric string and not have a fractional part.
Specifically tests a string if it can be coerced to a float. $value
must be a numeric string.
All these functions have the mixed $value
as their first argument and return the type they are coercing (or throw a TypeError).
Returns a boolean if the given value is coerceable, see Coerceable::toBool for valid values, or a TypeError if the value is not coerceable.
Returns an integer if the given value is coerceable, see Coerceable::toInt for valid values, or a TypeError if the value is not coerceable.
Returns a float if the given value is coerceable, see Coerceable::toFloat for valid values, or a TypeError if the value is not coerceable.
Returns a string if the given value is coerceable, see Coerceable::toString for valid values, or a TypeError if the value is not coerceable.
Returns a boolean if the given string is coerceable, see Coerceable::stringToBool for valid values, or a TypeError if the value is not coerceable.
Returns a boolean if the given int is coerceable, see Coerceable::intToBool for valid values, or a TypeError if the value is not coerceable.
Returns a boolean if the given float is coerceable, see Coerceable::floatToBool for valid values, or a TypeError if the value is not coerceable.
Returns an integer if the given float is coerceable, see Coerceable::floatToInt for valid values, or a TypeError if the value is not coerceable.
Returns an integer if the given string is coerceable, see Coerceable::stringToInt for valid values, or a TypeError if the value is not coerceable.
Returns a float if the given string is coerceable, see Coerceable::stringToFloat for valid values, or a TypeError if the value is not coerceable.
Coerces a boolean to a string, returning either '0' or '1'.
Coerces an integer to a string, returning a numeric string.
Coerces a float to a string, returning a numeric string.
All these functions have the mixed $value
as their only argument and return the type they are enforcing, according to the same logic as strict mode in PHP.
Returns $value
as a boolean if it is a boolean. Throws a TypeError otherwise.
Returns $value
as an integer if it is an integer. Throws a TypeError otherwise.
Returns $value
as a float if it is an integer or a float. Throws a TypeError otherwise.
Returns $value
as a string if it is a string. Throws a TypeError otherwise.