PHPackages                             dgame/php-cast - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. dgame/php-cast

ActivePackage[Utility &amp; Helpers](/categories/utility)

dgame/php-cast
==============

v0.9.0(3y ago)28.8k—4.5%2MITPHPPHP ^8.0

Since Sep 11Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Dgame/php-cast)[ Packagist](https://packagist.org/packages/dgame/php-cast)[ RSS](/packages/dgame-php-cast/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (14)Versions (12)Used By (2)

Type Assumptions &amp; Assertions simplified
============================================

[](#type-assumptions--assertions-simplified)

Have you ever had to validate user data? Probably you have used something like [webmozarts/assert](https://github.com/webmozarts/assert):

```
$id = $data['id'] ?? null;
Assert::integer($id);
```

The problem is, even though we checked that `$id` must be an `int`, it is actually still seen as `mixed` (see [this example](https://phpstan.org/r/dca4ad02-603d-4fdc-814b-1cdfcfe508e7) 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`:

```
use function Dgame\Cast\Assume\int;

$id = int($data['id'] ?? 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:

```
use function Dgame\Cast\Assert\int;

$id = int($data['id'] ?? null);
```

Now `$id` is of type `int` or it fails with an `AssertionError`. A message for the `AssertionError` can also be optionally set:

```
use function Dgame\Cast\Assert\int;

$id = int($data['id'] ?? null, message: 'The id of the given user must be of type int');
```

You can do that for [int](#int), [float](#float), [bool](#bool), [string](#string), [number](#number), [scalar](#scalar) and [array](#array) values.

---

int
===

[](#int)

### intify

[](#intify)

With `intify` you get either the `int`-value or the `int`-casted `scalar` value, if any:

```
use function Dgame\Cast\Assume\intify;

$id = intify($data['id'] ?? null); // $id is of type int|null
```

### unsigned

[](#unsigned)

`unsigned` will return a non-null value, if the given value is a [number](#number) that is &gt;= 0.

```
use function Dgame\Cast\Assume\unsigned;

$id = unsigned($data['id'] ?? null); // $id is of type int|null and >= 0 if it is an int
```

### positive

[](#positive)

`positive` will return a non-null value, if the given value is a [number](#number) that is &gt; 0.

```
use function Dgame\Cast\Assume\positive;

$id = positive($data['id'] ?? null); // $id is of type int|null and > 0 if it is an int
```

### negative

[](#negative)

`negative` will return a non-null value, if the given value is a [number](#number) that is &lt; 0.

```
use function Dgame\Cast\Assume\negative;

$id = negative($data['id'] ?? null); // $id is of type int|null and < 0 if it is an int
```

float
=====

[](#float)

### floatify

[](#floatify)

With `floatify` you get either the `float`-value or the `float`-casted `scalar` value, if any:

```
use function Dgame\Cast\Assume\float;

$money = float($data['money'] ?? null); // $money is of type float|null
```

bool
====

[](#bool)

With `bool` you get the bool-value for `true`, `false`, `1`, `0`, `on`, `off`, `yes`, `no` or null.

```
use function Dgame\Cast\Assume\bool;

$checked = bool($data['checked'] ?? null); // $checked is of type bool|null
```

### boolify

[](#boolify)

With `boolify` you get either the `bool`-value or the `bool`-casted `scalar` value, if any:

```
use function Dgame\Cast\Assume\boolify;

$checked = boolify($data['checked'] ?? null); // $checked is of type bool|null
```

string
======

[](#string)

### stringify

[](#stringify)

With `stringify` you get either the `string`-value or the `string`-casted `scalar` value, if any:

```
use function Dgame\Cast\Assume\stringify;

$value = stringify($data['value'] ?? null); // $value is of type string|null
```

number
======

[](#number)

With `number` you get either the `int` or `float`-value or null, if it is neither.

```
use function Dgame\Cast\Assume\number;

$range = number($data['range'] ?? null); // $range is of type int|float|null
```

scalar
======

[](#scalar)

With `scalar` you get either the `int`, `float`, `bool` or `string`-value or null, if it is neither.

```
use function Dgame\Cast\Assume\scalar;

$value = scalar($data['value'] ?? null); // $value is of type int|float|bool|string|null
```

array
=====

[](#array)

With `collection` you can test whether your value is an `array`:

```
use function Dgame\Cast\Assume\collection;

$values = collection($data['values'] ?? null); // $values is of type array|null
```

And with `collectionOf` you can test whether your value is an `array` of type `T`:

```
use function Dgame\Cast\Assume\collectionOf;

$values = collectionOf('Dgame\Cast\Assume\int', $data['values'] ?? null); // $values is of type array|null
```

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`:

```
use function Dgame\Cast\Collection\filter;

$values = filter('Dgame\Cast\Assume\int', $data['values'] ?? []); // $values is of type array
```

But be aware that `filter` expects an `array` as input and not `mixed`!

### list

[](#list)

If you want to make sure, that you have a `list` of values (and not an assoc. array) you can use `listOf`:

```
use function Dgame\Cast\Assume\listOf;

$values = listOf('Dgame\Cast\Assume\int', $data['values'] ?? null); // $values is of type int[]|null or, to be more accurate, of type array|null
```

### map

[](#map)

If you want to make sure, that you have an assoc. array (and not a `list`) you can use `mapOf`:

```
use function Dgame\Cast\Assume\mapOf;

$values = mapOf('Dgame\Cast\Assume\int', $data['values'] ?? null); // $values is of type array|null
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~33 days

Recently: every ~71 days

Total

11

Last Release

1380d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2a9fa98c1a3e70a521430fc2fba266657b2c981b5d8a36bf236fad01f9846dcd?d=identicon)[Dgame](/maintainers/Dgame)

---

Top Contributors

[![Dgame](https://avatars.githubusercontent.com/u/2406877?v=4)](https://github.com/Dgame "Dgame (24 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleECS

Type Coverage Yes

### Embed Badge

![Health badge](/badges/dgame-php-cast/health.svg)

```
[![Health](https://phpackages.com/badges/dgame-php-cast/health.svg)](https://phpackages.com/packages/dgame-php-cast)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
