Download the PHP package dgame/php-cast without Composer
On this page you can find all versions of the php package dgame/php-cast. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package php-cast
Type Assumptions & Assertions simplified
Have you ever had to validate user data? Probably you have used something like webmozarts/assert:
The problem is, even though we checked that $id
must be an int
, it is actually still seen as mixed
(see this example for phpstan).
To change this, you need to write / use your own phpstan rule that makes phpstan believe that it will now always be an int
.
So if you use your own verification methods, you must also write / use your own phpstan rules.
This package tries to simplify that. To verify that something is an int
, you can assume that it must be an int
. If it is not, you get null
:
With this, $id
is of type int|null
for phpstan, psalm, phpstorm and so on.
If you want to assert that it is an int
, you can do that too by using:
Now $id
is of type int
or it fails with an AssertionError
. A message for the AssertionError
can also be optionally set:
You can do that for array values.
int
intify
With intify
you get either the int
-value or the int
-casted scalar
value, if any:
unsigned
unsigned
will return a non-null value, if the given value is a number that is >= 0.
positive
positive
will return a non-null value, if the given value is a number that is > 0.
negative
negative
will return a non-null value, if the given value is a number that is < 0.
float
floatify
With floatify
you get either the float
-value or the float
-casted scalar
value, if any:
bool
With bool
you get the bool-value for true
, false
, 1
, 0
, on
, off
, yes
, no
or null.
boolify
With boolify
you get either the bool
-value or the bool
-casted scalar
value, if any:
string
stringify
With stringify
you get either the string
-value or the string
-casted scalar
value, if any:
number
With number
you get either the int
or float
-value or null, if it is neither.
scalar
With scalar
you get either the int
, float
, bool
or string
-value or null, if it is neither.
array
With collection
you can test whether your value is an array
:
And with collectionOf
you can test whether your value is an array
of type T
:
If not all values in the array
are of type int
, you get null
. If you just want to filter the non-int
values, you can do that by using filter
:
But be aware that filter
expects an array<int|string, mixed>
as input and not mixed
!
list
If you want to make sure, that you have a list
of values (and not an assoc. array) you can use listOf
:
map
If you want to make sure, that you have an assoc. array (and not a list
) you can use mapOf
: