Skip to content

Commit fe3167e

Browse files
committed
Docs
Disable pretty print by default Fix null in tuples Fix typehint for constant properties Fix multibyte strings Fix not provided value for required enum
1 parent 82a7602 commit fe3167e

30 files changed

+1079
-49
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[![Build Status](https://travis-ci.org/wol-soft/php-micro-template.svg?branch=master)](https://travis-ci.org/wol-soft/php-json-schema-model-generator)
55
[![Coverage Status](https://coveralls.io/repos/github/wol-soft/php-json-schema-model-generator/badge.svg?branch=master)](https://coveralls.io/github/wol-soft/php-json-schema-model-generator?branch=master)
66
[![MIT License](https://img.shields.io/packagist/l/wol-soft/php-micro-template.svg)](https://github.com/wol-soft/php-json-schema-model-generator/blob/master/LICENSE)
7+
[![Documentation Status](https://readthedocs.org/projects/php-json-schema-model-generator/badge/?version=latest)](https://php-json-schema-model-generator.readthedocs.io/en/latest/?badge=latest)
78

89
# php-json-schema-model-generator
910
Generates PHP model classes from JSON-Schema files including validation and providing a fluent auto completion for the generated classes.
@@ -26,6 +27,7 @@ Simple example from a PHP application: you define and document an API with swagg
2627
## Requirements ##
2728

2829
- Requires at least PHP 7.2
30+
- Requires the PHP extensions ext-json and ext-mbstring
2931

3032
## Installation ##
3133

@@ -38,6 +40,8 @@ To avoid adding all dependencies of the php-json-model-generator to your product
3840

3941
## Basic usage ##
4042

43+
Check out the [docs](https://php-json-schema-model-generator.readthedocs.io/en/latest/) for more details.
44+
4145
The base object for generating models is the *Generator*. After you have created a Generator you can use the object to generate your model classes without any further configuration:
4246

4347
```php
@@ -70,11 +74,11 @@ Method | Configuration | Default
7074
``` setNamespacePrefix(string $prefix) ``` <br><br>Example:<br> ``` setNamespacePrefix('\MyApp\Model') ``` | Configures a namespace prefix for all generated classes. The namespaces will be extended with the directory structure of the source directory. | Empty string so no namespace prefix will be used
7175
``` setImmutable(bool $immutable) ``` <br><br>Example:<br> ``` setImmutable(false) ``` | If set to true the generated model classes will be delivered without setter methods for the object properties. | true
7276
``` setCollectErrors(bool $collectErrors) ``` <br><br>Example:<br> ``` setCollectErrors(false) ``` | By default the complete input is validated and in case of failing validations all error messages will be thrown in a single exception. If set to false the first failing validation will throw an exception. | true
73-
``` setPrettyPrint(bool $prettyPrint) ``` <br><br>Example:<br> ``` setPrettyPrint(false) ``` | If set to false, the generated model classes won't follow coding gudelines (but the generation is faster). If enabled the package [Symplify/EasyCodingStandard](https://github.com/Symplify/EasyCodingStandard) will be used to clean up the generated code. | true
77+
``` setPrettyPrint(bool $prettyPrint) ``` <br><br>Example:<br> ``` setPrettyPrint(true) ``` | If set to false, the generated model classes won't follow coding guidelines (but the generation is faster). If enabled the package [Symplify/EasyCodingStandard](https://github.com/Symplify/EasyCodingStandard) will be used to clean up the generated code. By default pretty printing is disabled. | false
7478
``` setSerialization(bool $serialization) ``` <br><br>Example:<br> ``` setSerialization(true) ``` | If set to true the serialization methods `toArray` and `toJSON` will be added to the public interface of the generated classes. | false
75-
``` setOutputEnabled(bool $prettyPrint) ``` <br><br>Example:<br> ``` setOutputEnabled(false) ``` | Enable or disable output of the generation process to STDOUT | true
76-
``` setErrorRegistryClass(string $exceptionClass) ``` <br><br>Example:<br> ``` setErrorRegistryClass(CustomException::class) ``` | Define a custom exception implementing the ErrorRegistryExceptionInterface to decouple the generated code from the library (if you want to declare the library as a dev-dependency). The exception will be thrown if a validation fails error collection is **enabled** | ErrorRegistryException::class
77-
``` setExceptionClass(bool $prettyPrint) ``` <br><br>Example:<br> ``` setExceptionClass(CustomException::class) ``` | Define a custom exception to decouple the generated code from the library (if you want to declare the library as a dev-dependency). The exception will be thrown if a validation fails error collection is **disabled** | ValidationException::class
79+
``` setOutputEnabled(bool $outputEnabled) ``` <br><br>Example:<br> ``` setOutputEnabled(false) ``` | Enable or disable output of the generation process to STDOUT | true
80+
``` setErrorRegistryClass(string $exceptionClass) ``` <br><br>Example:<br> ``` setErrorRegistryClass(CustomException::class) ``` | Define a custom exception implementing the ErrorRegistryExceptionInterface to be used. The exception will be thrown if a validation fails and error collection is **enabled** | ErrorRegistryException::class
81+
``` setExceptionClass(string $exceptionClass) ``` <br><br>Example:<br> ``` setExceptionClass(CustomException::class) ``` | Define a custom exception to be used. The exception will be thrown if a validation fails and error collection is **disabled** | ValidationException::class
7882

7983
## Examples ##
8084

@@ -135,6 +139,7 @@ $person = new Person(['name' => 'Albert', 'age' => -1]);
135139
$person = new Person(['name' => 'Albert']);
136140
$person->getName(); // returns 'Albert'
137141
$person->getAge(); // returns NULL
142+
$person->getRawModelDataInput(); // returns ['name' => 'Albert']
138143

139144
// If setters are generated the setters also perform validations.
140145
// Exception: 'Value for age must not be smaller than 0'

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"wol-soft/php-json-schema-model-generator-exception": "^0.1.0",
1616
"wol-soft/php-micro-template": "^1.3.1",
1717
"php": ">=7.2",
18-
"ext-json": "*"
18+
"ext-json": "*",
19+
"ext-mbstring": "*"
1920
},
2021
"require-dev": {
2122
"phpunit/phpunit": "^8.4"

docs/source/complexTypes/array.rst

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
Array
2+
=====
3+
4+
There are two types of arrays in JSON-Schema definitions: lists and tuples. Both types are supported by the model generator.
5+
6+
Lists
7+
-----
8+
9+
A simple array without further restrictions can be defined using `array`.
10+
11+
.. code-block:: json
12+
13+
{
14+
"id": "example",
15+
"type": "object",
16+
"properties": {
17+
"example": {
18+
"type": "array"
19+
}
20+
}
21+
}
22+
23+
Generated interface:
24+
25+
.. code-block:: php
26+
27+
public function setExample(?array $example): self;
28+
public function getExample(): ?array;
29+
30+
Possible exceptions:
31+
32+
* Invalid type for example. Requires array, got __TYPE__
33+
34+
Items
35+
^^^^^
36+
37+
The items of a list can be restricted with a nested schema. All items of the schema must match the defined schema.
38+
39+
.. code-block:: json
40+
41+
{
42+
"id": "example",
43+
"type": "object",
44+
"properties": {
45+
"example": {
46+
"type": "array",
47+
"items": {
48+
"type": "string",
49+
"minLength": 2
50+
}
51+
}
52+
}
53+
}
54+
55+
With a schema like this all items must contain a string with at least two characters. Possible exceptions:
56+
57+
* Invalid item in array example
58+
59+
A more complex array may contain a nested object.
60+
61+
.. code-block:: json
62+
63+
{
64+
"id": "example",
65+
"type": "family",
66+
"properties": {
67+
"members": {
68+
"type": "array",
69+
"items": {
70+
"type": "object",
71+
"id": "member",
72+
"properties": {
73+
"name": {
74+
"type": "string"
75+
},
76+
"age": {
77+
"type": "integer",
78+
"minimum": 0
79+
}
80+
},
81+
"required": [
82+
"name"
83+
]
84+
}
85+
}
86+
}
87+
}
88+
89+
In this case the model generator will generate two classes: **Family** and **Member**. Generated interfaces:
90+
91+
.. code-block:: php
92+
93+
// class Family
94+
public function setMembers(?array $members): self;
95+
public function getMembers(): ?array;
96+
97+
// class Member
98+
public function setName(string $name): self;
99+
public function getName(): string;
100+
101+
public function setAge(?int $age): self;
102+
public function getAge(): ?int;
103+
104+
The *getMembers* function of the class *Family* is typehinted with *@returns Member[]*. Consequently auto completion is available when developing something like:
105+
106+
.. code-block:: php
107+
108+
$family = new Family($inputArray);
109+
110+
foreach ($family->getMembers() as $member) {
111+
// auto completion with available methods on $member
112+
$member->getName();
113+
}
114+
115+
Tuples
116+
------
117+
118+
TODO: documentation
119+
120+
Contains
121+
--------
122+
123+
The contains check uses a schema which must match at least one of the items provided in the input data to pass the validation. Simple checks like 'must contain a string' are possible as well as checks like 'must contain an object with a specific structure'.
124+
125+
.. code-block:: json
126+
127+
{
128+
"id": "example",
129+
"type": "object",
130+
"properties": {
131+
"example": {
132+
"type": "array",
133+
"contains": {
134+
"type": "string"
135+
}
136+
}
137+
}
138+
}
139+
140+
Possible exceptions:
141+
142+
* No item in array example matches contains constraint
143+
144+
Size validation
145+
---------------
146+
147+
To limit the size of an array use the `minItems` and `maxItems` keywords.
148+
149+
.. code-block:: json
150+
151+
{
152+
"id": "example",
153+
"type": "object",
154+
"properties": {
155+
"example": {
156+
"type": "array",
157+
"minItems": 2,
158+
"maxItems": 5
159+
}
160+
}
161+
}
162+
163+
Possible exceptions:
164+
165+
* Array example must not contain less than 2 items
166+
* Array example must not contain more than 5 items
167+
168+
Uniqueness
169+
----------
170+
171+
The items of an array can be forced to be unique with the `uniqueItems` keyword.
172+
173+
.. code-block:: json
174+
175+
{
176+
"id": "example",
177+
"type": "object",
178+
"properties": {
179+
"example": {
180+
"type": "array",
181+
"uniqueItems": true
182+
}
183+
}
184+
}
185+
186+
Possible exceptions:
187+
188+
* Items of array example are not unique

docs/source/complexTypes/enum.rst

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Enum
2+
====
3+
4+
Enums can be used to define a set of constant values a property must accept.
5+
6+
.. code-block:: json
7+
8+
{
9+
"id": "example",
10+
"type": "object",
11+
"properties": {
12+
"example": {
13+
"type": "string",
14+
"enum": ["ABC", "DEF"]
15+
}
16+
}
17+
}
18+
19+
Generated interface:
20+
21+
.. code-block:: php
22+
23+
public function setExample(?string $example): self;
24+
public function getExample(): ?string;
25+
26+
Possible exceptions:
27+
28+
* Invalid type for example. Requires string, got __TYPE__
29+
* Invalid value for example declined by enum constraint
30+
31+
Untyped Enum
32+
------------
33+
34+
An enum can also be defined without a specific type.
35+
36+
.. code-block:: json
37+
38+
{
39+
"id": "example",
40+
"type": "object",
41+
"properties": {
42+
"example": {
43+
"enum": ["ABC", 10, true, null]
44+
}
45+
}
46+
}
47+
48+
Generated interface (no typehints are generated as it's an untyped enum):
49+
50+
.. code-block:: php
51+
52+
public function setExample($example): self;
53+
public function getExample();
54+
55+
Possible exceptions:
56+
57+
* Invalid value for example declined by enum constraint

docs/source/conf.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
# The theme to use for HTML and HTML Help pages. See the documentation for
7575
# a list of builtin themes.
7676
#
77-
html_theme = 'alabaster'
77+
html_theme = 'sphinx_rtd_theme'
7878

7979
# Theme options are theme-specific and customize the look and feel of a theme
8080
# further. For a list of options available for each theme, see the
@@ -171,3 +171,11 @@
171171

172172
# A list of files that should not be packed into the epub file.
173173
epub_exclude_files = ['search.html']
174+
175+
# load PhpLexer
176+
from sphinx.highlighting import lexers
177+
from pygments.lexers.web import PhpLexer
178+
179+
# enable highlighting for PHP code not between <?php ... ?> by default
180+
lexers['php'] = PhpLexer(startinline=True)
181+
lexers['php-annotations'] = PhpLexer(startinline=True)

docs/source/generic/default.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Default values
2+
==============
3+
4+
Default values are set inside the model if a property which is not required isn't provided in the input data.
5+
6+
.. code-block:: json
7+
8+
{
9+
"id": "example",
10+
"type": "object",
11+
"properties": {
12+
"example": {
13+
"type": "string",
14+
"default": "Not provided"
15+
}
16+
}
17+
}
18+
19+
Behaviour with different inputs:
20+
21+
.. code-block:: php
22+
23+
// property example not provided --> fallback to the default value
24+
$example = new Example([]);
25+
$example->getExample(); // returns "Not provided"
26+
27+
// property example explicitly set to null (allowed as the property isn't required)
28+
$example = new Example(['example' => null]);
29+
$example->getExample(); // returns NULL
30+
31+
// property example set to a custom value
32+
$example = new Example(['example' => 'My Input']);
33+
$example->getExample(); // returns "My Input"

0 commit comments

Comments
 (0)