A key named first-name looked fine in the JSON. Laravel quotes it, and that array syntax is valid. The TypeScript draft and the PHP DTO copy the hyphen into the field name. first-name: string and public string $first-name are what the converter emits. The constructor uses that same hyphen: $this->first-name = $data['first-name'] ?? null. Nothing in the draft renames the key. The blocks below are that object, converted in the browser.
{"first-name":"Ada","role":"engineer"}
[
'first-name' => 'Ada',
'role' => 'engineer',
]
export interface Root {
first-name: string;
role: string;
}
<?php
declare(strict_types=1);
class DataTransferObject
{
public string $first-name;
public string $role;
public function __construct(array $data = [])
{
$this->first-name = $data['first-name'] ?? null;
$this->role = $data['role'] ?? null;
}
}
A list key loses one s
An array of objects drops a single trailing s from the key, then Pascal-cases what remains. items becomes Item, so the field is items: Item[]. status becomes Statu, so the field is status: Statu[]. For {"status":[{"code":"ok"}]} the draft is:
export interface Root {
status: Statu[];
}
export interface Statu {
code: string;
}
The PHP DTO types that property as array. It does not emit a class named Statu.
The class name stays DataTransferObject
The PHP class name does not change. An object value is array, not a child class, so a nested object such as meta is public array $meta. A missing key is still assigned with ?? null, including on a property declared public int $id.
One tip
A trailing comma clears the output. {"a":1,} produces Expected double-quoted property name in JSON at position 7 (line 1 column 8). Repair the text in the JSON formatter, then convert again.
The sample, the CSV cell, and the rest of the questions are in the guide: https://kepttools.com/guides/json-to-typescript-php-dto-laravel
Top comments (0)