PHP TypeLang Help

Type Builders

The Type Builder is a factory class that acts as a layer between the type declaration (like non-empty-string) and the type instance (like NonEmptyStringType).

To register types in the Mapper, a platform is used containing a set of type builders that analyze the type description (and AST) and create a set of type instances used directly at runtime for checking and casting values.

To better understand the tasks of the builder, it makes sense to look at the graph illustrating the process of compiling a type when calling $mapper->denormalize(42) that should return int(42) using built-in TypeInterface<int> type

cast value 42 using TypeInterface

start compilation

for int(42) value the string("int") was inferred

definition string("int") was parsed to AST object(TypeStatement)

configure builders list<TypeBuilderInterface>

TypeInterface<int>

int(42)

Mapper::denormalize(42)

TypeInterface<int>::cast(42)

TypeExtractor::extract(42)

TypeParser::parse("int")

TypeRepository::findType(TypeStatement)

Platform

int(42)

The diagram is quite complex, and it's okay if you don't quite understand the process. The main thing is that it makes sense:

That is, the code of type builder of int<0, 10> looks something like this:

class IntTypeBuilder interface TypeBuilderInterface { public function build(TypeStatement $stmt): TypeInterface { // [[[Expects named type|basic-types.html]]] assert($stmt instanceof NamedTypeNode); // [[[Expects 2 template arguments|generic-types.html]]] assert(count($stmt->arguments) === 2); // [[[Expects no shape fields|shape-types.html]]] assert($stmt->fields === null); return new IntType( name: $stmt->name->toString(), // string("int"); min: $stmt->arguments[0]->getValue(), // int(0) max: $stmt->arguments[1]->getValue(), // int(10) ); } }
06 November 2025