PHPackages                             mvkasatkin/typecast - 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. mvkasatkin/typecast

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

mvkasatkin/typecast
===================

Typecast data by scheme

1.0.0(8y ago)011MITPHPPHP &gt;=7.0.0

Since Oct 14Pushed 8y ago1 watchersCompare

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

READMEChangelogDependencies (2)Versions (2)Used By (0)

Typecast
========

[](#typecast)

[![Build Status](https://camo.githubusercontent.com/524f41b0fcb5d4963cc624292d958ecb0f65a7ae989db5b47aad27e120625591/68747470733a2f2f7472617669732d63692e6f72672f6d766b617361746b696e2f74797065636173742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mvkasatkin/typecast)[![Coverage Status](https://camo.githubusercontent.com/cc57cb9e02f5886e83298d1224186c9f58975be723aacda814ea3f4c289d66e0/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d766b617361746b696e2f74797065636173742f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/mvkasatkin/typecast?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/f863e2164920fb8e71ee3ad77211e2204e84eaace2bf5af3752fb23db3a425b2/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d766b617361746b696e2f74797065636173742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mvkasatkin/typecast/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/a6132967c66e44ffffa2be87041642ad06f158dbfd15764ffd054e2202c0ab4b/68747470733a2f2f706f7365722e707567782e6f72672f6d766b617361746b696e2f74797065636173742f76657273696f6e)](https://packagist.org/packages/mvkasatkin/typecast)[![License](https://camo.githubusercontent.com/caad4fe79b9d6ebbb7ff8cc2496009aaf9e3aa2ebdecc4628076cf528aa72683/68747470733a2f2f706f7365722e707567782e6f72672f6d766b617361746b696e2f74797065636173742f6c6963656e7365)](https://packagist.org/packages/mvkasatkin/typecast)

Simple library for type casting of scalar variables or array data by **configurable scheme with nested levels**.

Installation
------------

[](#installation)

```
composer require mvkasatkin/typecast

```

Usage
-----

[](#usage)

### Scalar type casting

[](#scalar-type-casting)

simple types

```
use \Mvkasatkin\typecast\Cast;
use function \Mvkasatkin\typecast\cast;

cast('120',      Cast::INT);    // => 120 - same as (int)'120'
cast(120,        Cast::FLOAT);  // => 120.0 - same as (float)120
cast('1',        Cast::BOOL);   // => true - same as (true)'1'
cast(120,        Cast::STRING); // => '120' - same as (string)120
cast('120',      Cast::BINARY); // => '120' - binary string, same as (binary)'120'
cast('120',      Cast::ARRAY);  // => ['120'] - same as (array)'120'
cast('120',      Cast::UNSET);  // => null - same as (unset)'120'
cast(['a' => 1], Cast::OBJECT); // => stdClass - same as (object)['a' => 1']
```

array of type

```
use \Mvkasatkin\typecast\Cast;
use function \Mvkasatkin\typecast\cast;

cast(['1', 2, 3.0, null], [Cast::FLOAT]); // => [1.0, 2.0, 3.0, null]
```

custom type casting by closure

```
use \Mvkasatkin\typecast\Cast;
use function \Mvkasatkin\typecast\cast;

cast('1', function($value) { return (int)$value + 1; });   // => 2
```

with default value (by default = null)

```
use \Mvkasatkin\typecast\Cast;
use \Mvkasatkin\typecast\type\TypeInt;
use function \Mvkasatkin\typecast\cast;

cast('110', new TypeInt(140));   // => 110
cast(null, new TypeInt(140));   // => 140
```

### Type casting by scheme

[](#type-casting-by-scheme)

```
$importData = [...]; // some external data
$scheme = [
    'field.1' => Cast::INT,
    'field.2' => Cast::FLOAT,
    'field.3' => [
        'field.3.ids' => [Cast::INT], // array of integers
        'field.3.name' => Cast::STRING,
        'field.3.price' => function($value) { /* custom type casting */ }
    ],
    'field.4' => new TypeBool(false), // default false
];

$safeData = cast($importData, $scheme);
```

### Strict scheme

[](#strict-scheme)

A strict scheme will remove keys that are not in the scheme and add the keys with default values that it contains, but are not present in the input data.

```
$importData = [...]; // some external data
$scheme = [...]; // previous scheme
$strict = true;

$safeData = cast($importData, $scheme, $strict);
```

### Alternative object style

[](#alternative-object-style)

```
$scheme = new scheme([
    'field.1' => new TypeInt(),
    'field.2' => new TypeFloat(),
    'field.3' => [
        'field.3.ids' => new TypeArrayOfType(new TypeInt()), // array of integers
        'field.3.name' => new TypeString(),
        'field.3.price' => new TypeClosure(function($value) { /* custom type casting */ })
    ],
    'field.4' => new TypeBool(false), // default false
]);

$cast = new Cast($scheme)
$cast->process($importData); // useful in iterations
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

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

Unknown

Total

1

Last Release

3129d ago

### Community

Maintainers

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

---

Top Contributors

[![mvkasatkin](https://avatars.githubusercontent.com/u/3389061?v=4)](https://github.com/mvkasatkin "mvkasatkin (9 commits)")

---

Tags

typeconvertschemecasttypecast

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mvkasatkin-typecast/health.svg)

```
[![Health](https://phpackages.com/badges/mvkasatkin-typecast/health.svg)](https://phpackages.com/packages/mvkasatkin-typecast)
```

###  Alternatives

[phpoption/phpoption

Option Type for PHP

2.7k541.2M157](/packages/phpoption-phpoption)[webpatser/laravel-uuid

Laravel integration for webpatser/uuid - High-performance drop-in UUID replacements (15% faster than Ramsey). Provides Str macros, HasUuids trait, facades, and casts. RFC 4122/9562 compliant.

1.8k17.3M128](/packages/webpatser-laravel-uuid)[jetbrains/phpstorm-stubs

PHP runtime &amp; extensions header files for PhpStorm

1.4k27.7M68](/packages/jetbrains-phpstorm-stubs)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49444.8M97](/packages/marc-mabe-php-enum)[league/uri-components

URI components manipulation library

31932.3M66](/packages/league-uri-components)[akaunting/laravel-money

Currency formatting and conversion package for Laravel

7825.3M18](/packages/akaunting-laravel-money)

PHPackages © 2026

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