Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ The directory `./tests/manual` contains some easy examples which show the usage.
Let's have a look into an easy example. We create a simple model for a person with a name and an optional age. Our resulting JSON-Schema:
```json
{
"$id": "Person",
"title": "Person",
"type": "object",
"properties": {
"name": {
Expand Down Expand Up @@ -112,22 +112,22 @@ public function getName(): string;
public function getAge(): ?int;

// setters to change the values of the model after instantiation (only generated if immutability is disabled)
public function setName(string $name): Person;
public function setAge(?int $age): Person;
public function setName(string $name): static;
public function setAge(?int $age): static;
```

Now let's have a look at the behaviour of the generated model:
```php
// Throws an exception as the required name isn't provided.
// Exception: 'Missing required value for name'
// Exception: "Missing required value for 'name'"
$person = new Person([]);

// Throws an exception as the name provides an invalid value.
// Exception: 'Invalid type for name. Requires string, got int'
// Exception: "Invalid type for 'name': requires 'string', got 'integer'"
$person = new Person(['name' => 12]);

// Throws an exception as the age contains an invalid value due to the minimum definition.
// Exception: 'Value for age must not be smaller than 0'
// Exception: "Value for 'age' must not be smaller than 0"
$person = new Person(['name' => 'Albert', 'age' => -1]);

// A valid example as the age isn't required
Expand All @@ -137,20 +137,20 @@ $person->getAge(); // returns NULL
$person->meta()->rawInput(); // returns ['name' => 'Albert']

// If setters are generated the setters also perform validations.
// Exception: 'Value for age must not be smaller than 0'
// Exception: "Value for 'age' must not be smaller than 0"
$person->setAge(-10);
```

More complex exception messages eg. from a [allOf](https://json-schema.org/understanding-json-schema/reference/combining.html#allof) composition may look like:
```
Invalid value for Animal declined by composition constraint.
Requires to match 3 composition elements but matched 1 element.
Invalid value for 'Animal' declined by composition constraint
Requires to match all composition elements but matched 1 element
- Composition element #1: Failed
* Value for age must not be smaller than 0
* Value for 'age' must not be smaller than 0
- Composition element #2: Valid
- Composition element #3: Failed
* Value for legs must not be smaller than 2
* Value for legs must be a multiple of 2
* Value for 'legs' must not be smaller than 2
* Value for 'legs' must be a multiple of 2
```

## How the heck does this work? ##
Expand Down
4 changes: 2 additions & 2 deletions docs/source/combinedSchemas/allOf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The `allOf` keyword can be used to combine multiple subschemas. The provided val
.. code-block:: json

{
"$id": "example",
"title": "Example",
"type": "object",
"properties": {
"example": {
Expand All @@ -31,7 +31,7 @@ Generated interface:
.. code-block:: php

public function setExample(float $example): static;
public function getExample(): float;
public function getExample(): ?float;


Possible exception (eg. if a string is provided):
Expand Down
6 changes: 3 additions & 3 deletions docs/source/combinedSchemas/anyOf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The `anyOf` keyword can be used to combine multiple subschemas. The provided val
.. code-block:: json

{
"$id": "example",
"title": "Example",
"type": "object",
"properties": {
"example": {
Expand All @@ -31,7 +31,7 @@ Generated interface:
.. code-block:: php

public function setExample(float $example): static;
public function getExample(): float;
public function getExample(): ?float;


Possible exception (if a string is provided):
Expand Down Expand Up @@ -98,7 +98,7 @@ The thrown exception will be a *PHPModelGenerator\\Exception\\ComposedValue\\Any
construction time by which branches the provided data satisfies. When multiple matching branches
define a default for the same property, those defaults must agree; the generator throws a
``SchemaException`` at generation time if they differ. Branch defaults are **not** included in
``getRawModelDataInput()``.
``meta()->rawInput()``.

See `Default values <../generic/default.html#branch-defaults-in-compositions>`__ for the full
explanation.
6 changes: 3 additions & 3 deletions docs/source/combinedSchemas/crossTypedComposition.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ property type to the union of all branch types.
.. code-block:: json

{
"$id": "example",
"title": "Example",
"type": "object",
"anyOf": [
{
Expand Down Expand Up @@ -74,7 +74,7 @@ For example, with a two-branch ``oneOf`` where ``age`` is required in both branc
.. code-block:: json

{
"$id": "example",
"title": "Example",
"type": "object",
"oneOf": [
{
Expand Down Expand Up @@ -147,4 +147,4 @@ widen the property type.

The same intersection behaviour also applies to properties defined via
``patternProperties`` when their names match declared properties. See
`Pattern properties <../object/patternProperties.html>`__ for details.
`Pattern properties <../complexTypes/object.html#pattern-properties>`__ for details.
10 changes: 5 additions & 5 deletions docs/source/combinedSchemas/if.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The keywords `if`, `then` and `else` can be used to conditionally combine multip
.. code-block:: json

{
"$id": "example",
"title": "Example",
"type": "object",
"properties": {
"example": {
Expand Down Expand Up @@ -77,7 +77,7 @@ An object level composition will result in an object which contains all properti
.. code-block:: json

{
"$id": "customer",
"title": "Customer",
"type": "object",
"properties": {
"country": {
Expand Down Expand Up @@ -117,15 +117,15 @@ Generated interface:
public function setCountry(string $country): static;
public function getCountry(): ?string;

public function setPostalCode(string $country): static;
public function setPostalCode(string $postalCode): static;
public function getPostalCode(): ?string;

When the ``then`` and ``else`` branches define the same property with **different types**, the generator produces a union type hint — consistent with the behaviour of ``anyOf``/``oneOf``:

.. code-block:: json

{
"$id": "example",
"title": "Example",
"type": "object",
"if": {
"properties": {
Expand Down Expand Up @@ -203,7 +203,7 @@ When only a ``then`` block is present (no ``else``), the branch may not apply at
generator applies the branch default only when the relevant branch is active — the ``then``
default applies when the ``if`` condition is satisfied, and the ``else`` default applies when it
is not. A user-supplied value always overrides the branch default. Branch defaults are **not**
included in ``getRawModelDataInput()``.
included in ``meta()->rawInput()``.

When a ``then`` or ``else`` branch default conflicts with a root ``properties`` default or a
``patternProperties`` default for the same property, the generator throws a ``SchemaException``
Expand Down
10 changes: 5 additions & 5 deletions docs/source/combinedSchemas/mergedProperty.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ For example we combine two objects with `allOf` for an object property:
.. code-block:: json

{
"$id": "company",
"title": "Company",
"type": "object",
"properties": {
"ceo": {
"$id": "CEO",
"title": "CEO",
"allOf": [
{
"type": "object",
Expand Down Expand Up @@ -45,15 +45,15 @@ As the subschemas don't contain IDs they will be named with uniqIds (compare the
* Company_Ceo5e4a82e39fe37.php
* Company_Merged_CEO.php

If the allOf doesn't contain an $id field the merged class will also contain an uniqId. So if you want to use the class with a reproducible class name you must set the $id field.
If the property holding the allOf doesn't carry a ``title`` (or ``$id``) the merged class will also contain a uniqId. So if you want to use the class with a reproducible class name you must set ``title`` (or ``$id``) on the property.
The classes Company_Ceo5e4a82e39edc3 and Company_Ceo5e4a82e39fe37 are only used for internal validation and can't be accessed via the generated interface of Company.

Generated interface:

.. code-block:: php

# class Company
public function setCeo(Company_Merged_CEO $example): static;
public function setCeo(Company_Merged_CEO $ceo): static;
public function getCeo(): ?Company_Merged_CEO;

# class Company_Merged_CEO
Expand All @@ -67,7 +67,7 @@ If your composition is defined on object level the object will gain access to al
.. code-block:: json

{
"$id": "CEO",
"title": "CEO",
"type": "object",
"allOf": [
{
Expand Down
2 changes: 1 addition & 1 deletion docs/source/combinedSchemas/not.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Used to validate a provided schema or property doesn't match the given schema. I
.. code-block:: json

{
"$id": "example",
"title": "Example",
"type": "object",
"properties": {
"example": {
Expand Down
6 changes: 3 additions & 3 deletions docs/source/combinedSchemas/oneOf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The `oneOf` keyword can be used to combine multiple subschemas. The provided val
.. code-block:: json

{
"$id": "example",
"title": "Example",
"type": "object",
"properties": {
"example": {
Expand All @@ -31,7 +31,7 @@ Generated interface:
.. code-block:: php

public function setExample(float $example): static;
public function getExample(): float;
public function getExample(): ?float;


Possible exception (if a string is provided):
Expand Down Expand Up @@ -106,7 +106,7 @@ The thrown exception will be a *PHPModelGenerator\\Exception\\ComposedValue\\One
Properties in object-level ``oneOf`` branches may carry a ``"default"`` value. The generator
applies the branch default only when that branch is the active one — determined at construction
time by which branch the provided data satisfies. A user-supplied value always overrides the
branch default. Branch defaults are **not** included in ``getRawModelDataInput()``.
branch default. Branch defaults are **not** included in ``meta()->rawInput()``.

When two ``oneOf`` branches define a default for the same property, or when a branch default
conflicts with a root ``properties`` default or a ``patternProperties`` default, the generator
Expand Down
Loading
Loading