Transformers are the most important part of TypeScript transformer, they take a PHP class and try to transform it to a
TypeScript type. A transformer implements the Transformer interface:
interface Transformer
{
public function transform(PhpClassNode $phpClassNode, TransformationContext $context): Transformed|Untransformable;
}
The TransformationContext contains all the information you need to transform a class:
class TransformationContext
{
public function __construct(
public string $name,
public array $nameSpaceSegments,
) {
}
}
Within the method a Transformed data object should be created and returned which looks like this:
use Spatie\TypeScriptTransformer\References\ClassStringReference;
new Transformed(
typeScriptNode: $typeScriptNode,
reference: new ClassStringReference($reflectionClass->getName()),
location: $context->nameSpaceSegments,
export: true,
);
If a class cannot be transformed, the Untransformable object should be returned:
use Spatie\TypeScriptTransformer\Untransformable;
Untransformable::create();
When a class cannot be transformed, the next transformer in the list will be executed.