PHPackages                             fredemmott/type-assert - 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. fredemmott/type-assert

Abandoned → [hhvm/type-assert](/?search=hhvm%2Ftype-assert)Library

fredemmott/type-assert
======================

Convert untyped data to typed data

v3.3.1(7y ago)036.4k2MITHack

Since Sep 28Pushed 5y agoCompare

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

READMEChangelogDependencies (6)Versions (23)Used By (2)

TypeAssert [![Build Status](https://camo.githubusercontent.com/aa387cb199eb0c5510616a09f6bf55279019e854cdac743bd11a7a9a0dfff514/68747470733a2f2f7472617669732d63692e6f72672f6868766d2f747970652d6173736572742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/hhvm/type-assert)
==========================================================================================================================================================================================================================================================================================

[](#typeassert-)

Hack library for converting untyped data to typed data.

Warning for `TypeAssert::matches_type_structure()`
--------------------------------------------------

[](#warning-for-typeassertmatches_type_structure)

`TypeStructure`, `type_structure()`, and `ReflectionTypeAlias::getTypeStructures()`are experimental features of HHVM, and not supported by Facebook or the HHVM team. This means that `matches_type_structure()` may need to be removed in a future release without warning.

`matches_type_structure()` is based on these APIs as there is not currently a viable alternative.

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

[](#installation)

```
composer require hhvm/type-assert

```

Usage
-----

[](#usage)

TypeAssert provides functions that take a mixed input, and will either return it unmodified (but with type data) or throw an exception; for example:

```
 123]); // passes: $x is a dict
$x = $spec->assertType(dict['foo' => '123']); // fails
$x = $spec->assertType(dict[123 => 456]); // fails
$x = $spec->assertType(dict[123 => 456]); // fails

$x = $spec->coerceType(dict[123 => '456']); // passes: $x is dict['123' => 456];
```

Shapes and tuples are not supported, as they can not be expressed generically.

`matches_type_structure(TypeStructure, mixed): T`
-------------------------------------------------------

[](#matches_type_structurettypestructuret-mixed-t)

Asserts that a variable matches the given type structure; these can be arbitrary nested shapes. This is particular useful for dealing with JSON responses.

```
 int,
    'user' => string,
    'data' => shape(
      /* ... */
    ),
  );

  public static function getAPIResponse(): self::TAPIResponse {
    $json_string = file_get_contents('https://api.example.com');
    $array = json_decode($json_string, /* associative = */ true);
    return TypeAssert\matches_type_structure(
      type_structure(self::class, 'TAPIResponse'),
      $array,
    );
  }
}
```

You can use `type_structure()` to get a `TypeStructure` for a type constant, or `ReflectionTypeAlias::getTypeStructure()` for top-level type aliases.

`not_null(?T): T`
--------------------

[](#not_nulltt-t)

Throws if it's null, and refines the type otherwise - for example:

```
 string
  needs_int(TypeAssert\not_null($bar)); // ?int => int
}
```

`is_instance_of(classname, mixed): T`
-------------------------------------------

[](#is_instance_oftclassnamet-mixed-t)

Asserts that the input is an object of the given type; for example:

```
