You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -4,7 +4,7 @@ description: 'Generate JSON schemas automatically from PHP class definitions'
4
4
icon: 'code'
5
5
---
6
6
7
-
Generate JSON schemas automatically from PHP classes using reflection and docblock analysis. This feature extracts property types, validation rules, and documentation from your existing PHP classes.
7
+
Generate JSON schemas automatically from PHP classes using reflection and docblock analysis. This feature extracts property types, defaults, documentation, and array element types from your existing PHP classes.
// Generate with Draft 2019-09 for deprecated support
189
192
$schema = Schema::fromClass(
190
193
ModernUser::class,
191
-
version: SchemaVersion::Draft_2019_09
194
+
schemaVersion: SchemaVersion::Draft_2019_09
192
195
);
193
196
194
197
// The generated schema will include "deprecated": true for old_email
195
198
```
196
199
200
+
## Constructor Property Promotion
201
+
202
+
Promoted constructor properties are supported. Descriptions and array element types are read from the constructor's `@param` tags when the property itself has no docblock:
203
+
204
+
```php
205
+
/**
206
+
* User data transfer object
207
+
*
208
+
* @param string $name The user's full name
209
+
* @param int $age The user's age in years
210
+
*/
211
+
class UserDto
212
+
{
213
+
public function __construct(
214
+
public string $name,
215
+
public int $age = 18,
216
+
) {}
217
+
}
218
+
219
+
$schema = Schema::fromClass(UserDto::class);
220
+
// name: required string with @param description
221
+
// age: optional integer with default 18
222
+
```
223
+
224
+
An explicit `@var` tag on a promoted property takes precedence over the matching `@param` description.
225
+
226
+
## Array Item Types
227
+
228
+
When a property is typed as `array`, the converter reads element types from docblock generics and adds an `items` schema. Supported syntax includes:
229
+
230
+
-`string[]` and `(int|string)[]`
231
+
-`array<string>`, `array<int|string>`, and `array<string, int>` (value type is used for key-value maps)
232
+
-`list<bool>`, `non-empty-array<string>`, and similar list/array generics
Array item types are limited to JSON Schema scalar types (`string`, `integer`, `number`, `boolean`, `null`, `object`, `array`) and unions of those scalars. Class names (e.g. `DateTime[]`), `mixed`, and nested generics (e.g. `array<array<int>>`) are skipped silently, leaving a plain `array` without `items`.
// Custom class types (like Address) are treated as generic 'object' type
240
-
// Generic array syntax (e.g., array<OrderItem>) in docblocks is not parsed
241
-
// Arrays are treated as generic arrays without item type information
295
+
// items, notes, and metadata include string item schemas
296
+
// Custom class types (like Address) require ignoreUnknownTypes or separate schema generation
242
297
```
243
298
244
299
<Note>
245
-
Custom class types (non-built-in PHP types) are converted to generic `object` schemas. The converter does not recursively analyze nested classes or parse generic array syntax from docblocks. To create schemas for nested objects, generate them separately and combine them using the fluent API.
300
+
Custom class types (non-built-in PHP types) are not mapped to JSON Schema types by default and will throw an `UnknownTypeException`. Use `ignoreUnknownTypes: true` to skip unmappable properties, or generate nested object schemas separately and combine them using the fluent API.
246
301
</Note>
247
302
303
+
## Handling Unknown Types
304
+
305
+
By default, properties typed as custom classes (e.g. `DateTime`, `Address`) cause conversion to fail. Pass `ignoreUnknownTypes: true` to omit those properties from the generated schema:
306
+
307
+
```php
308
+
class Event
309
+
{
310
+
public string $title;
311
+
312
+
public DateTime $starts_at;
313
+
314
+
public ?DateTime $ends_at = null;
315
+
}
316
+
317
+
// Throws UnknownTypeException for DateTime properties
318
+
$schema = Schema::fromClass(Event::class);
319
+
320
+
// Skips DateTime properties, includes title only
321
+
$schema = Schema::fromClass(
322
+
Event::class,
323
+
ignoreUnknownTypes: true,
324
+
);
325
+
```
326
+
327
+
This option is also available on `Schema::from()` when passing a class or object instance.
328
+
248
329
## Enum Integration
249
330
250
331
Automatically handle backed enums:
@@ -363,9 +444,14 @@ class CompleteUser extends BaseEntity
363
444
}
364
445
365
446
$schema = Schema::fromClass(CompleteUser::class);
366
-
// Schema will include properties from base class and traits
447
+
// Schema includes properties from the base class and traits
448
+
// Static properties are excluded
367
449
```
368
450
451
+
<Note>
452
+
Only **public** properties are included by default. Pass `publicOnly: false` to include protected and private properties. Static properties are always excluded.
453
+
</Note>
454
+
369
455
## Validation and Usage
370
456
371
457
Use the generated schema to validate data:
@@ -418,13 +504,14 @@ if ($userSchema->isValid($userData)) {
418
504
419
505
The schema generator automatically extracts the following from your docblocks:
420
506
421
-
-**Description text** - From the docblock summary and description
422
-
-**Property types** - From `@var` annotations (combined with native PHP types)
423
-
-**Parameter types** - From `@param` annotations
424
-
-**Deprecation status** - Using the `@deprecated` tag
507
+
-**Description text** - From the class docblock summary and `@var` property descriptions
508
+
-**Promoted property descriptions** - From matching constructor `@param` tags when no `@var` is present
509
+
-**Property types** - From native PHP type hints (combined with `@var` where present)
510
+
-**Array item types** - From generic array syntax in `@var` or `@param` tags (scalar element types only)
511
+
-**Deprecation status** - Using the `@deprecated` tag on classes and properties
425
512
426
513
<Note>
427
-
Validation rules (like minLength, pattern, format) are **not** extracted from docblocks. You need to apply them programmatically using the fluent API after generation, or define them in your actual PHP code using native types and enums.
514
+
Validation rules (like minLength, pattern, format) are **not** extracted from docblocks. Apply them programmatically using the fluent API after generation, or rely on native PHP types and backed enums for type validation.
428
515
</Note>
429
516
430
517
## Best Practices
@@ -485,15 +572,15 @@ public string $email;
485
572
*/
486
573
public string $username;
487
574
488
-
// Good: Document complex array types
575
+
// Good: Document array element types with generics
489
576
/**
490
577
* @var array<string> List of user roles
491
578
*/
492
579
public array $roles;
493
580
```
494
581
495
582
<Note>
496
-
Validation rules like minLength, pattern, format are not extracted from docblocks. Generic array syntax (e.g., `array<string>`, `array<OrderItem>`) in docblocks is not parsed - arrays are treated as generic arrays without item type information. Apply validation rules and array item schemas programmatically using the fluent API after schema generation, or use native PHP types and enums for type validation.
583
+
Validation rules like minLength, pattern, and format are not extracted from docblocks. Array item types are inferred from generic syntax for scalar elements only — nested object or array element types (e.g. `array<OrderItem>`) are not resolved recursively. Apply additional validation programmatically using the fluent API after schema generation.
Supported syntax matches class property docblocks: `T[]`, `array<T>`, `list<T>`, `array<K,V>`, and unions of scalar element types. Class names, `mixed`, and nested generics are skipped silently.
209
+
210
+
## Handling Unknown Types
211
+
212
+
Parameters typed as custom classes throw an `UnknownTypeException` by default. Pass `ignoreUnknownTypes: true` to skip unmappable parameters:
213
+
214
+
```php
215
+
/**
216
+
* @param string $title Event title
217
+
* @param DateTime $starts_at Start time
218
+
*/
219
+
$createEvent = function (string $title, DateTime $starts_at): void {};
Use detailed docblock annotations for validation rules:
@@ -261,7 +299,7 @@ Real-world example for API endpoint validation:
261
299
* @param ?string $email Email filter
262
300
* @param ?int $age_min Minimum age filter
263
301
* @param ?int $age_max Maximum age filter
264
-
* @param array $roles Role filter
302
+
* @param array<string> $roles Role filter
265
303
* @param int $page Page number for pagination
266
304
* @param int $per_page Items per page
267
305
* @param string $sort_by Sort field
@@ -319,7 +357,7 @@ $modernApiClosure = function (
319
357
// Generate with Draft 2019-09 for deprecated support
320
358
$modernSchema = Schema::fromClosure(
321
359
$modernApiClosure,
322
-
version: SchemaVersion::Draft_2019_09
360
+
schemaVersion: SchemaVersion::Draft_2019_09
323
361
);
324
362
325
363
// Apply validation rules programmatically after generation
@@ -429,7 +467,7 @@ function processData($name, $age, $tags, $date) {}
429
467
```
430
468
431
469
<Note>
432
-
Validation rules like format, minLength, pattern are not extracted from docblocks. Generic array syntax (e.g., `array<string>`, `array<OrderItem>`) in docblocks is not parsed - arrays are treated as generic arrays without item type information. Only parameter types and descriptions are extracted. Apply validation rules and array item schemas programmatically using the fluent API after schema generation.
470
+
Validation rules like format, minLength, and pattern are not extracted from docblocks. Array item types are inferred from generic syntax for scalar elements only. Apply additional validation programmatically using the fluent API after schema generation.
0 commit comments