Skip to content

Commit 40c6f3d

Browse files
committed
Add additional documentation for recursive objects and references
1 parent d7d27ce commit 40c6f3d

File tree

4 files changed

+124
-3
lines changed

4 files changed

+124
-3
lines changed

docs/source/complexTypes/object.rst

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Generated interface:
3737
public function setCar(?Car $name): self;
3838
public function getCar(): ?Car;
3939
40-
// class Car
40+
// class Person_Car
4141
public function setModel(?string $name): self;
4242
public function getModel(): ?string;
4343
public function setPs(?int $name): self;
@@ -55,8 +55,23 @@ Namespaces
5555
If a nested class is generated the nested class will be located in the same namespace as the parent class.
5656
If the nested class occurs somewhere else and has already been generated a class from another namespace may be used (compare `namespaces <../generic/namespaces.html>`__ for additional information concerning class re-usage).
5757

58+
Naming
59+
------
60+
61+
Naming of classes
62+
^^^^^^^^^^^^^^^^^
63+
64+
If the given main object in a JSON-Schema file contains a `$id` the id will be used as class name. Otherwise the name of the file will be used.
65+
66+
Naming of nested classes
67+
^^^^^^^^^^^^^^^^^^^^^^^^
68+
69+
Nested classes are prefixed with the parent class. If an object `Person` has a nested object `car` the class for car will be named **Person_Car**.
70+
71+
For the class name of a nested class the `$id` property of the nested object is used. If the id property isn't present the property key combined with a uniqid will be used.
72+
5873
Property Name Normalization
59-
---------------------------
74+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
6075

6176
Property names are normalized to provide valid and readable PHP code. All non alpha numeric characters will be removed.
6277

@@ -183,6 +198,57 @@ If invalid additional properties are provided a detailed exception will be throw
183198
- invalid additional property 'additional2'
184199
* Invalid type for age. Requires int, got string
185200
201+
Recursive Objects
202+
-----------------
203+
204+
If objects are defined recursive the recursion will be resolved into a single class.
205+
206+
.. code-block:: json
207+
208+
{
209+
"definitions": {
210+
"person": {
211+
"$id": "person",
212+
"type": "object",
213+
"properties": {
214+
"name": {
215+
"type": "string"
216+
},
217+
"children": {
218+
"type": "array",
219+
"items": {
220+
"$ref": "#/definitions/person"
221+
}
222+
}
223+
}
224+
}
225+
},
226+
"$id": "family",
227+
"type": "object",
228+
"properties": {
229+
"members": {
230+
"type": "array",
231+
"items": {
232+
"$ref": "#/definitions/person"
233+
}
234+
}
235+
}
236+
}
237+
238+
Generated interface:
239+
240+
.. code-block:: php
241+
242+
// class Family, arrays typehinted in docblocks with Family_Person[]
243+
public function setMembers(?array $members): self;
244+
public function getMembers(): ?array;
245+
246+
// class Person, arrays typehinted in docblocks with Family_Person[]
247+
public function setName(?string $name): self;
248+
public function getName(): ?string;
249+
public function setChildren(?array $name): self;
250+
public function getChildren(): ?array;
251+
186252
Property Names
187253
--------------
188254

docs/source/generic/references.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
.. |br| raw:: html
2+
3+
<br>
4+
5+
References
6+
==========
7+
8+
References can be used to re-use parts/objects of JSON-Schema definitions.
9+
10+
Supported reference types
11+
-------------------------
12+
13+
* internal (in a single file) reference by id (example: `"$ref": "IdOfMyObject"`)
14+
* internal (in a single file) reference by path (example: `"$ref": "#/definitions/myObject"`)
15+
* relative reference based on the location on the file system to a complete file (example: `"$ref": "./../modules/myObject.json"`)
16+
* relative reference based on the location on the file system to an object by id (example: `"$ref": "./../modules/myObject.json#IdOfMyObject"`)
17+
* relative reference based on the location on the file system to an object by path (example: `"$ref": "./../modules/myObject.json#/definitions/myObject"`)
18+
* network reference to a complete file (example: `"$ref": "https://my.domain.com/schema/modules/myObject.json"`)
19+
* network reference to an object by id (example: `"$ref": "https://my.domain.com/schema/modules/myObject.json#IdOfMyObject"`)
20+
* network reference to an object by path (example: `"$ref": "https://my.domain.com/schema/modules/myObject.json#/definitions/myObject"`)
21+
22+
Object reference
23+
----------------
24+
25+
An example for properties referring to a definition inside the same schema:
26+
27+
.. code-block:: json
28+
29+
{
30+
"definitions": {
31+
"person": {
32+
"$id": "person",
33+
"type": "object",
34+
"properties": {
35+
"name": {
36+
"type": "string"
37+
}
38+
}
39+
}
40+
},
41+
"$id": "team",
42+
"type": "object",
43+
"properties": {
44+
"leader": {
45+
"$ref": "#/definitions/person"
46+
}
47+
"members": {
48+
"type": "array",
49+
"items": {
50+
"$ref": "#/definitions/person"
51+
}
52+
}
53+
}
54+
}

docs/source/toc-generic.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
generic/required
66
generic/default
77
generic/namespaces
8+
generic/references
89
generic/meta
910
generic/combinedException

src/Utils/ClassNameGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function getClassName(
2323
$className = sprintf(
2424
$isMergeClass ? '%s_Merged_%s' : '%s_%s',
2525
$currentClassName,
26-
$schema['$id'] ?? ($propertyName . ($currentClassName ? uniqid() : ''))
26+
ucfirst($schema['$id'] ?? ($propertyName . ($currentClassName ? uniqid() : '')))
2727
);
2828

2929
return ucfirst(preg_replace('/\W/', '', trim($className, '_')));

0 commit comments

Comments
 (0)