Custom Type Builder
To create a custom type, it may make sense to create a factory (i.e., a type builder). This ensures that the type object is created correctly and with the required constructor arguments.
The builder object compiles the type based on configuration rules and external parameters, so don't be afraid to move all type assembly logic into this class.
For a fully custom implementation of the type builder, one should implement the interface TypeLang\Mapper\Type\Builder\TypeBuilderInterface, which requires the implementation of two methods:
isSupported()- Must returnbool(true)if the specified type builder can build theTypeInterfacebuild()- Creates aTypeInterfaceinstance
Additionally, don't forget that you can pass additional external parameters to the type builder's constructor. For example, a JSON Schema Validator, if you want to create a type that validates a JSON inside a string, like: json<"schema.json">
Matching
First, for the builder to work and construct the type, a checking method isSupported() must be implemented.
For example, if we want to check that the type is called json, the code will look like this:
Each named type is implemented using the NamedTypeNode instance.
Building
Next, we need to implement the type creation method. If the type doesn't have any external dependencies, then creating the type will be quite simple.
However, in this case the same JsonType instance will be returned, no matter how you write the type definition:
jsonjson<T>json{key: val}json{key: val, ...<T>}etc.
You can add appropriate checks to exclude template arguments and shape fields.