PHP TypeLang Help

Standard Platform

The standard platform is the most general platform, suitable for targets such as DB or API.

If required, you can specify it explicitly, but this is required in cases where you want to customize it.

use TypeLang\Mapper\Mapper; use TypeLang\Mapper\Platform\StandardPlatform; $mapper = new Mapper(new StandardPlatform());

Customization

This platform supports customization of metadata readers and metadata providers (the meta constructor's argument). Additional types (the types argument) and additional type coercers (the coercers argument) can be specified.

Metadata Readers

For example, to customize a metadata reader, you should pass it explicitly to the meta parameter of the constructor.

use TypeLang\Mapper\Mapper; use TypeLang\Mapper\Mapping\Reader\AttributeReader; use TypeLang\Mapper\Mapping\Reader\JsonConfigReader; use TypeLang\Mapper\Mapping\Reader\YamlConfigReader; use TypeLang\Mapper\Platform\StandardPlatform; $attributes = new Mapper(new StandardPlatform( meta: new AttributeReader(), )); $yamlConfigWithAttributes = new Mapper(new StandardPlatform( meta: new AttributeReader( delegate: new YamlConfigReader(...), ), ));

Metadata Providers

To modify metadata provider, it can also be explicitly passed to the constructor in the meta argument.

use TypeLang\Mapper\Mapper; use TypeLang\Mapper\Mapping\Provider\InMemoryProvider; use TypeLang\Mapper\Mapping\Provider\Psr16CacheProvider; use TypeLang\Mapper\Mapping\Reader\AttributeReader; use TypeLang\Mapper\Mapping\Reader\YamlConfigReader; use TypeLang\Mapper\Platform\StandardPlatform; $memoized = new Mapper(new StandardPlatform( meta: new InMemoryProvider(...), )); $cacheAndMemoize = new Mapper(new StandardPlatform( meta: new InMemoryProvider( delegate: new Psr16CacheProvider(...), ), ));

Additional Types

To add custom types, pass a collection of type builders to the types argument of the StandardPlatform constructor.

use TypeLang\Mapper\Mapper; use TypeLang\Mapper\Platform\StandardPlatform; $extended = new Mapper(new StandardPlatform(types: [ new My\Vendor\CustomTypeBuilder(), new My\Vendor\AnotherTypeBuilder(), ]);

Additional Coercers

To add custom type coercers, pass a collection of type coercers to the coercers argument of the StandardPlatform constructor.

use TypeLang\Mapper\Mapper; use TypeLang\Mapper\Platform\StandardPlatform; use TypeLang\Mapper\Type\StringType; $extended = new Mapper(new StandardPlatform(coercers: [ StringType::class => new My\Vendor\CustomStringTypeCoercer(), ]);

Types

The specified platform supports the following list of types.

Definition

Class

Alias To

Bidirectional

array-key

 ArrayKeyType

~

array,
array<K>,
array<K, V>

 ArrayType

~

iterable [10]

~

array

~

iterable<K> [10]

~

array<K>

~

iterable<K, V> [10]

~

array<K, V>

~

Iterator [10]

~

array

~

Iterator<K> [10]

~

array<K>

~

Iterator<K, V> [10]

~

array<K, V>

~

Generator [10]

~

array

~

Generator<K> [10]

~

array<K>

~

Generator<K, V> [10]

~

array<K, V>

~

Traversable [10]

~

array

~

Traversable<K> [10]

~

array<K>

~

Traversable<K, V> [10]

~

array<K, V>

~

IteratorAggregate [10]

~

array

~

IteratorAggregate<K> [10]

~

array<K>

~

IteratorAggregate<K, V>[10]

~

array<K, V>

~

BackedEnum [1]

 BackedEnumFromScalarType

~

D

BackedEnum [1]

 BackedEnumToScalarType

~

N

true,
false

 BoolLiteralType

~

bool

  BoolType

~

boolean [9]

~

bool

~

ClassName [2]

 ClassFromArrayType

~

D

ClassName [2]

 ClassToArrayType

~

N

DateTime,

DateTime<T> 3, 4

 DateTimeFromStringType

~

D

DateTime,
DateTime<T> [3]

 DateTimeToStringType

~

N

float literal value [5]

 FloatLiteralType

~

float

 FloatType

~

double [9]

~

float

~

real [9]

~

float

~

int literal value [6]

 IntLiteralType

~

int<min, max>

 IntRangeType

~

int

 IntType

~

integer [9]

~

int

~

positive-int [10]

~

int

~

non-positive-int [10]

~

int

~

negative-int [10]

~

int

~

non-negative-int [10]

~

int

~

non-zero-int [10]

~

int

~

number [10]

~

int

~

numeric [10]

~

int

~

list,
list<V>

 ListFromArrayType

~

D

list,
list<V>

 ListFromIterableType

~

N

mixed

MixedType

~

?T,
T|null

 NullableType

~

null

 NullType

~

object

 ObjectFromArrayType

~

D

object

 ObjectToArrayType

~

N

stdClass

~

object

~

string literal value [7]

 StringLiteralType

~

string

 StringType

~

Stringable

~

string

~

non-empty-string [10]

~

string

~

lowercase-string [10]

~

string

~

non-empty-lowercase-string [10]

~

string

~

uppercase-string [10]

~

string

~

non-empty-uppercase-string [10]

~

string

~

numeric-string [10]

~

string

~

literal-string [10]

~

string

~

non-empty-literal-string [10]

~

string

~

class-string [10]

~

string

~

interface-string [10]

~

string

~

trait-string [10]

~

string

~

enum-string [10]

~

string

~

callable-string [10]

~

string

~

truthy-string [10]

~

string

~

non-falsy-string [10]

~

string

~

T|U

 UnionType

~

UnitEnum [8]

 UnitEnumFromStringType

~

D

UnitEnum [8]

 UnitEnumToStringType

~

N

Definition

Class

Bidirectional

array-key

 ArrayKeyType

array,
array<K>,
array<K, V>

 ArrayType

BackedEnum [1]

 BackedEnumFromScalarType

D

BackedEnum [1]

 BackedEnumToScalarType

N

true,
false

 BoolLiteralType

bool

  BoolType

ClassName [2]

 ClassFromArrayType

D

ClassName [2]

 ClassToArrayType

N

DateTime,

DateTime<T> 3, 4

 DateTimeFromStringType

D

DateTime,
DateTime<T> [3]

 DateTimeToStringType

N

float literal value [5]

 FloatLiteralType

float

 FloatType

int literal value [6]

 IntLiteralType

int<min, max>

 IntRangeType

int

 IntType

list,
list<V>

 ListFromArrayType

D

list,
list<V>

 ListFromIterableType

N

mixed

MixedType

?T,
T|null

 NullableType

null

 NullType

object

 ObjectFromArrayType

D

object

 ObjectToArrayType

N

string literal value [7]

 StringLiteralType

string

 StringType

T|U

 UnionType

UnitEnum [8]

 UnitEnumFromStringType

D

UnitEnum [8]

 UnitEnumToStringType

N

Definition

Alias To

iterable [10]

array

iterable<K> [10]

array<K>

iterable<K, V> [10]

array<K, V>

Iterator [10]

array

Iterator<K> [10]

array<K>

Iterator<K, V> [10]

array<K, V>

Generator [10]

array

Generator<K> [10]

array<K>

Generator<K, V> [10]

array<K, V>

Traversable [10]

array

Traversable<K> [10]

array<K>

Traversable<K, V> [10]

array<K, V>

IteratorAggregate [10]

array

IteratorAggregate<K> [10]

array<K>

IteratorAggregate<K, V>[10]

array<K, V>

boolean [9]

bool

double [9]

float

real [9]

float

integer [9]

int

positive-int [10]

int

non-positive-int [10]

int

negative-int [10]

int

non-negative-int [10]

int

non-zero-int [10]

int

number [10]

int

numeric [10]

int

stdClass

object

Stringable

string

non-empty-string [10]

string

lowercase-string [10]

string

non-empty-lowercase-string [10]

string

uppercase-string [10]

string

non-empty-uppercase-string [10]

string

numeric-string [10]

string

literal-string [10]

string

non-empty-literal-string [10]

string

class-string [10]

string

interface-string [10]

string

trait-string [10]

string

enum-string [10]

string

callable-string [10]

string

truthy-string [10]

string

non-falsy-string [10]

string

Notes

  1. In case of the backed (int or string) enum is registered:

    • enum BackedEnum: string { ... }

    • enum BackedEnum: int { ... }

  2. In case of the class is registered:

    • class ClassName { ... }

  3. Any implementation of the DateTimeInterface

  4. When specifying the DateTimeInterface as the type, an instance of DateTimeImmutable will be created.

  5. A literal float value is an arbitrary float, such as:

    • 15.16

    • 23.

    • -.42

    • etc.

  6. A literal int value is an arbitrary int, such as:

    • 23

    • 0xDEAD_BEEF

    • etc.

  7. A literal string value is an arbitrary string, such as:

    • "example string"

  8. In case of the unit enum is registered:

    • enum UnitEnum { ... }

  9. Non-canonical alias; Means that such naming is incorrect and recommended to rename the type to original

  10. Temporary alias; Means that at the moment this type is an alias and does not carry any additional logic, but this may change in the future.

Type Coercions

The specified platform supports the following list of types coercers.

Class

Applies To

Used In

ArrayKeyTypeCoercer

array-key

array

BoolTypeCoercer

bool,
true,
false

~

FloatTypeCoercer

float,
float literal value [1]

~

IntTypeCoercer

int,
int<min, max>,
int literal value [2]

array-key,
BackedEnum<int>

StringTypeCoercer

string,
string literal value [3]

array-key,
BackedEnum<string>,
UnitEnum,
DateTime

Notes

  1. A literal float value is an arbitrary float, such as:

    • 15.16

    • 23.

    • -.42

    • etc.

  2. A literal int value is an arbitrary int, such as:

    • 23

    • 0xDEAD_BEEF

  3. A literal string value is an arbitrary string, such as:

    • "example string"

05 November 2025