PHPackages                             dazet/type-utils - 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. dazet/type-utils

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

dazet/type-utils
================

Type casting utils for PHP.

v0.2.0(4y ago)632.3k↓16.7%1MITPHPPHP ^7.4 || ^8.0CI failing

Since Nov 18Pushed 4y ago1 watchersCompare

[ Source](https://github.com/dazet/type-utils)[ Packagist](https://packagist.org/packages/dazet/type-utils)[ RSS](/packages/dazet-type-utils/feed)WikiDiscussions main Synced yesterday

READMEChangelog (3)Dependencies (2)Versions (4)Used By (1)

Safe type casting utils for PHP
===============================

[](#safe-type-casting-utils-for-php)

[![Build Status](https://camo.githubusercontent.com/efe3540d89c1b167c7f576bad27739633fe69cf98d25180ba13669c848991a8a/68747470733a2f2f7472617669732d63692e6f72672f64617a65742f747970652d7574696c732e7376673f6272616e63683d6d61696e)](https://travis-ci.org/dazet/type-utils)

StringUtil
----------

[](#stringutil)

#### `StringUtil::canBeString(mixed $value): bool`

[](#stringutilcanbestringmixed-value-bool)

Returns `true` if `$value` can be safely casted to string: is scalar or object with `__toString` method or `null`.

`StringUtil::canBeString` method shortcut:

```
$array = ['string', 123, [], null];
array_filter($array, StringUtil::canBeString); // ['string', 123, null]
```

#### `StringUtil::toStringOrNull(mixed $value): ?string`

[](#stringutiltostringornullmixed-value-string)

Casts value to `string` if possible or returns `null`.

`StringUtil::toStringOrNull` method shortcut:

```
$a = ['string', 123, [], new stdClass()];
array_map(StringUtil::toStringOrNull, $a); // ['string', '123', null, null]
```

#### `StringUtil::toString(mixed $value): string`

[](#stringutiltostringmixed-value-string)

Casts value to `string` if possible or throws `InvalidTypeException`.

`StringUtil::toString` method shortcut:

```
$a = ['string', 123, null];
array_map(StringUtil::toString, $a); // ['string', '123', '']

$b = ['string', new stdClass()];
array_map(StringUtil::toString, $b); // throws InvalidTypeException
```

NumberUtil
----------

[](#numberutil)

#### `NumberUtil::canBeNumber(mixed $value): bool`

[](#numberutilcanbenumbermixed-value-bool)

Returns true if value can be safely casted to `int|float`, which means that:

- it is number
- or numeric string
- or can be casted to numeric string
- or is a boolean that can be transformed to `0|1`
- or is `null`

`NumberUtil::canBeNumber` method shortcut:

```
$array = [123, 1.23, '123', 'string', null];
array_filter($array, NumberUtil::canBeNumber); // [123, 1.23, '123', null]
```

#### `NumberUtil::toIntOrNull(mixed $value): ?int`

[](#numberutiltointornullmixed-value-int)

Casts value to `int` if possible or returns `null`.

`NumberUtil::toIntOrNull` method shortcut:

```
$a = [123, 1.23, 'string'];
array_map(NumberUtil::toIntOrNull, $a); // [123, 1, null]
```

#### `NumberUtil::toInt(mixed $value): int`

[](#numberutiltointmixed-value-int)

Casts value to `int` if possible or throws `InvalidTypeException`.

#### `NumberUtil::toFloatOrNull(mixed $value): ?float`

[](#numberutiltofloatornullmixed-value-float)

Casts value to `float` if possible or returns `null`.

`NumberUtil::toFloatOrNull` method shortcut:

```
$a = [123, 1.23, 'string'];
array_map(NumberUtil::toFloatOrNull, $a); // [123.0, 1.23, null]
```

#### `NumberUtil::toFloat(mixed $value): float`

[](#numberutiltofloatmixed-value-float)

Casts value to `float` if possible or throws `InvalidTypeException`.

ArrayUtil
---------

[](#arrayutil)

#### `ArrayUtil::canBeArray(mixed $value): bool`

[](#arrayutilcanbearraymixed-value-bool)

Returns true if `$value` is or can be converted to `array`, which means it is iterable or `null`.

`ArrayUtil::canBeArray` method shortcut:

```
$array = [['array'], new ArrayObject(['object']), 'string'];
array_filter($array, ArrayUtil::canBeArray); // [['array'], new ArrayObject(['object'])]
```

#### `ArrayUtil::toArrayOrNull(mixed $value): ?array`

[](#arrayutiltoarrayornullmixed-value-arrayintstring-mixed)

Transforms `$value` to `array` (keeping keys) or returns `null`.

`ArrayUtil::toArrayOrNull` method shortcut:

```
$a = ['a' => ['array'], 'b' => 'string', 'c' => new ArrayObject(['object'])];
array_map(ArrayUtil::toArrayOrNull, $a); // ['a' => ['array'], 'b' => null, 'c' => ['object']]

$iterate = function() {
    yield 9 => 'nine';
    yield 9 => 'nine';
    yield 9 => 'nine';
};
array_map(ArrayUtil::toArrayOrNull, $iterate()); // [9 => 'nine']
```

#### `ArrayUtil::toArrayListOrNull(mixed $value): ?array`

[](#arrayutiltoarraylistornullmixed-value-arrayint-mixed)

Transforms `$value` to `array-list` (reindex keys) or returns `null`.

`ArrayUtil::toArrayListOrNull` method shortcut:

```
$a = ['a' => ['array'], 'b' => 'string', 'c' => new ArrayObject(['object'])];
array_map(ArrayUtil::toArrayListOrNull, $a); // [['array'], null, ['object']]

$iterate = function() {
    yield 9 => 'nine';
    yield 9 => 'nine';
    yield 9 => 'nine';
};
array_map(ArrayUtil::toArrayOrNull, $iterate()); // ['nine', 'nine', 'nine']
```

#### `ArrayUtil::toArray(mixed $value): array`

[](#arrayutiltoarraymixed-value-arrayintstring-mixed)

Transforms `$value` to `array` (keeping keys) or throws `InvalidTypeException`.

#### `ArrayUtil::toArrayList(mixed $value): array`

[](#arrayutiltoarraylistmixed-value-arrayint-mixed)

Transforms `$value` to `array-list` (reindex keys) or throws `InvalidTypeException`.

#### `ArrayUtil::isCountable(mixed $value): bool`

[](#arrayutiliscountablemixed-value-bool)

Same as standard `is_countable`, additionally it allows to shortcut callable by `ArrayUtil::isCountable`.

#### `ArrayUtil::countOrNull(mixed $value): ?int`

[](#arrayutilcountornullmixed-value-int)

Returns count if `$value` is countable or returns `null`.

BooleanUtil
-----------

[](#booleanutil)

#### `BooleanUtil::canBeBool(mixed $value): bool`

[](#booleanutilcanbeboolmixed-value-bool)

Returns true if `$value` can be taken as boolean (true, false, 0, 1).

`BooleanUtil::canBeBool` shortcut also available.

#### `BooleanUtil::toBoolOrNull(mixed $value): ?bool`

[](#booleanutiltoboolornullmixed-value-bool)

Casts value to `bool` if possible or returns `null`.

#### `BooleanUtil::toBool(mixed $value): ?bool`

[](#booleanutiltoboolmixed-value-bool)

Casts value to `bool` if possible or throws `InvalidTypeException`.

DateUtil
--------

[](#dateutil)

#### `DateUtil::canBeDate(mixed $value): bool`

[](#dateutilcanbedatemixed-value-bool)

Returns `true` if `$value` can be converted to `DateTimeImmutable`, which means it has `DateTimeInterface` or is a string supported by `strtotime`.

`DateUtil::canBeDate` method shortcut:

```
$now = new DateTime('now');
$a = ['today', $now, 'never'];
array_filter($a, DateUtil::canBeDate); // ['today', $now]
```

#### `DateUtil::toDatetimeOrNull(mixed $value): ?DateTimeImmutable`

[](#dateutiltodatetimeornullmixed-value-datetimeimmutable)

Transforms value to `DateTimeImmutable` if possible or returns `null`.

Shortcut `DateUtil::toDatetimeOrNull` also available.

#### `DateUtil::toDatetime(mixed $value): DateTimeImmutable`

[](#dateutiltodatetimemixed-value-datetimeimmutable)

Transforms value to `DateTimeImmutable` if possible or throws `InvalidTypeException`.

Shortcut `DateUtil::toDatetime` also available.

#### `DateUtil::toDateFormatOrNull(mixed $value, string $format): ?string`

[](#dateutiltodateformatornullmixed-value-string-format-string)

Transforms `$value` to given date format if `$value` can be date or returns `null` otherwise.

#### `DateUtil::toDateFormat(mixed $value, string $format): string`

[](#dateutiltodateformatmixed-value-string-format-string)

Transforms `$value` to given date format if `$value` can be date or throws `InvalidTypeException`.

#### `DateUtil::toTimestampOrNull(mixed $value): ?int`

[](#dateutiltotimestampornullmixed-value-int)

Transforms `$value` to Unix timestamp if `$value` can be date or returns `null` otherwise.

#### `DateUtil::toTimestamp(mixed $value): int`

[](#dateutiltotimestampmixed-value-int)

Transforms `$value` to Unix timestamp if `$value` can be date or throws `InvalidTypeException`.

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity51

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 ~206 days

Total

3

Last Release

1640d ago

PHP version history (2 changes)v0.1.0PHP &gt;=7.4

v0.2.0PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/a666727b5929fa9808ae335ac65a816d5d5956b3277a3433c5994a165d5b7215?d=identicon)[dazet](/maintainers/dazet)

---

Top Contributors

[![dazet](https://avatars.githubusercontent.com/u/9936733?v=4)](https://github.com/dazet "dazet (5 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/dazet-type-utils/health.svg)

```
[![Health](https://phpackages.com/badges/dazet-type-utils/health.svg)](https://phpackages.com/packages/dazet-type-utils)
```

###  Alternatives

[digitalpianism/bugfixes

Bug fixes for Magento 1.9.3.0

352.0k](/packages/digitalpianism-bugfixes)

PHPackages © 2026

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