PHPackages                             wildphp/type-definitions - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. wildphp/type-definitions

AbandonedArchivedProject[Validation &amp; Sanitization](/categories/validation)

wildphp/type-definitions
========================

Type definition and validation library

0.1(5y ago)0211MITPHPPHP ^7.2|^8.0

Since May 16Pushed 4y ago1 watchersCompare

[ Source](https://github.com/WildPHP/type-definitions)[ Packagist](https://packagist.org/packages/wildphp/type-definitions)[ RSS](/packages/wildphp-type-definitions/feed)WikiDiscussions main Synced 1w ago

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

Type definition library
=======================

[](#type-definition-library)

---

[![Code analysis](https://github.com/WildPHP/type-definitions/actions/workflows/analysis.yml/badge.svg)](https://github.com/WildPHP/type-definitions/actions/workflows/analysis.yml)[![Latest Stable Version](https://camo.githubusercontent.com/1228ff0ab5fd92c5dd90ceedac53ea200aa67a758e6707332e5487ad921c6560/68747470733a2f2f706f7365722e707567782e6f72672f77696c647068702f747970652d646566696e6974696f6e732f762f737461626c65)](https://packagist.org/packages/wildphp/type-definitions)[![Latest Unstable Version](https://camo.githubusercontent.com/c63fc80d0529f1bd7bc1438b98128620b2299f93a2ec00e147fe9373d56d2395/68747470733a2f2f706f7365722e707567782e6f72672f77696c647068702f747970652d646566696e6974696f6e732f762f756e737461626c65)](https://packagist.org/packages/wildphp/type-definitions)[![Total Downloads](https://camo.githubusercontent.com/08bded0f50acfd75dc9c82f21d8482ff77eb2ab452051acf334a875c3a6bb4dd/68747470733a2f2f706f7365722e707567782e6f72672f77696c647068702f747970652d646566696e6974696f6e732f646f776e6c6f616473)](https://packagist.org/packages/wildphp/type-definitions)

Library to provide type validation and expression. Includes an interpreter to transform short and concise syntax into usable types.

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

[](#installation)

In order to use this library, install it through [Composer](https://getcomposer.org):

```
$ composer require wildphp/type-definitions

```

Type definitions using classes
------------------------------

[](#type-definitions-using-classes)

Type definitions can be built using the classes in this library. There are 3 main classes:

- `PrimitiveTypeDefinition`
- `ClassTypeDefinition`
- `ArrayTypeDefinition`

### PrimitiveTypeDefinition

[](#primitivetypedefinition)

Primitive type definitions are those who refer to scalar or otherwise built-in types. `PrimitiveTypeDefinition` acts as a wrapper around [gettype()](https://www.php.net/manual/en/function.gettype.php).

For example:

```
$definition = new \WildPHP\TypeDefinitions\PrimitiveTypeDefinition('string');

echo $definition->validate('this is a valid string'); // true

echo $definition->validate(1); // false
echo $definition->validate(false); // false
```

Alternatively, types may be specified using one of the constants on the `PrimitiveTypeDefinition` class. These map to the type specifiers for `gettype()` and are recommended to be used instead of specifying types manually.

```
$definition = new \WildPHP\TypeDefinitions\PrimitiveTypeDefinition(\WildPHP\TypeDefinitions\PrimitiveTypeDefinition::STRING);
```

### ClassTypeDefinition

[](#classtypedefinition)

Class type definitions validate their value using [instanceof](https://www.php.net/manual/en/language.operators.type.php). This means that any class will pass this definition if it is an instance of, or inheritor of, the given class.

`null` is also considered a valid value.

For example:

```
$definition = new \WildPHP\TypeDefinitions\ClassTypeDefinition(stdClass::class);

$object = new stdClass();

echo $definition->validate($object); // true
echo $definition->validate(null); // true

echo $definition->validate('this is a string'); // false
echo $definition->validate(1); // false
echo $definition->validate(false); // false
```

### ArrayTypeDefinition

[](#arraytypedefinition)

Array type definitions validate their values using a child type definition (a content definition). This makes it possible to write complex nested structures.

Please note that ArrayTypeDefinition only validates values and not keys. Key validation would not make a lot of sense, since PHP itself limits key types to strings and integers.

Validation will fail when *any* of the children does not conform to the content definition.

For example:

```
$childDefinition = new \WildPHP\TypeDefinitions\PrimitiveTypeDefinition('string');
$definition = new \WildPHP\TypeDefinitions\ArrayTypeDefinition($childDefinition);

echo $definition->validate(['this is a valid string', 'another valid string']); // true

echo $definition->validate(1); // false
echo $definition->validate([1]); // false
echo $definition->validate(false); // false
echo $definition->validate([false]); // false
echo $definition->validate(['this is a string', 1]); // false, because 1 does not conform
```

Type definitions using maps and strings
---------------------------------------

[](#type-definitions-using-maps-and-strings)

The library provides a `TypeDefinitionInterpreter` helper class to translate maps and strings into `TypeDefinitionInterface`instances.

This is used [in the models library](https://github.com/wildphp/models) to provide swift model declaration and validation.

`createDefinitionMap` is used to translate the values of key-value based arrays into TypeDefinitionInterface implementations. This method will preserve keys and will not modify the existing array.

For example:

```
$map = [
    'array' => ['string'],
    'string' => 'string',
    'stdClass' => stdClass::class
];

$interpreted = \WildPHP\TypeDefinitions\TypeDefinitionInterpreter::createDefinitionMap($map);

// $interpreted['array'] is now an instance of ArrayTypeDefinition with a content definition of PrimitiveTypeDefinition with type 'string'
// $interpreted['string'] is now an instance of PrimitiveTypeDefinition with type 'string'
// $interpreted['stdClass'] is now an instance of ClassTypeDefinition with class identifier stdClass::class
```

If you do not need to interpret a map but would rather interpret a single value, use the `interpret` function:

```
$definition = 'string';

$interpreted = \WildPHP\TypeDefinitions\TypeDefinitionInterpreter::interpret($definition);

// $interpreted is now an instance of PrimitiveTypeDefinition with type 'string'
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

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

1828d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/64cbefa2b33116fa8d909bbeb6add16b949cd91864b693cd9c4797430ee7eb6d?d=identicon)[Yoshi2889](/maintainers/Yoshi2889)

---

Top Contributors

[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/wildphp-type-definitions/health.svg)

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

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

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