Code Generation
The BradieTilley\Data\Generator namespace can emit PHP Data class source from
a fluent definition or from a decoded JSON/array structure. This is handy for
scaffolding DTOs from an API schema or sample payload.
This generates PHP classes. It is unrelated to TypeScript export, which is not part of this package.
From a fluent definition
use BradieTilley\Data\Generator\DataGeneratorClass;
use BradieTilley\Data\Generator\DataGeneratorProperty;
$class = new DataGeneratorClass(
namespace: 'App\\Http\\Resources',
name: 'ProductResource',
properties: [
new DataGeneratorProperty(
name: 'sku',
types: ['string'],
readonly: true,
description: 'The SKU of a product',
example: 'ABUFEF',
),
],
);
echo $class->generate();
produces:
<?php
namespace App\Http\Resources;
use BradieTilley\Data\Attributes\Description;
use BradieTilley\Data\Attributes\Example;
use BradieTilley\Data\Data;
class ProductResource extends Data
{
public function __construct(
#[Example('ABUFEF')]
#[Description('The SKU of a product')]
public readonly string $sku,
) {
//
}
}
From an array / JSON structure
use BradieTilley\Data\Generator\DataGeneratorClass;
$structure = json_decode($json, true);
$class = DataGeneratorClass::from($structure);
echo $class->generate();
Property options
DataGeneratorProperty supports the full attribute vocabulary, including
sometimes, lazy, literal, arrayOf, template, dateFormat, description,
example, exportName, visibility, default values and readonly-ness — each maps
to the matching attribute on the emitted class.
Marker & documentation attributes
These attributes carry metadata for tooling and the code generator. They do not change the runtime hydration/serialization behaviour of the DTO itself.
| Attribute | Purpose |
|---|---|
Description(...$lines) | Human-readable description of a property |
Example($value, $description = '') | An example value for a property |
ExportName($name) | An alternate name for downstream exporters |
Literal | Marks a property value as a literal for downstream tooling |
Template($name) | Declares a generic template name for downstream tooling |
These marker attributes are primarily consumed by external tooling (e.g. the TypeScript exporter in laravel-suite) and by the code generator when it emits Data classes.