Multi Type

By providing an array with types for a property multiple types can be allowed.

{
    "$id": "example",
    "type": "object",
    "properties": {
        "example": {
            "type": ["number", "string"]
        }
    }
}

Generated interface:

public function setExample(float | string $example): static;
// the property isn't required, so null is included in the return type
public function getExample(): float | string | null;

Possible exceptions:

  • Invalid type for property. Requires [float, string], got __TYPE__

The thrown exception will be a PHPModelGenerator\Exception\Generic\InvalidTypeException which provides the following methods to get further error details:

// returns a string if the property expects exactly one type, an array if the property accepts multiple types
public function getExpectedType()
// get the name of the property which failed
public function getPropertyName(): string
// get the value provided to the property
public function getProvidedValue()

Additional validators

For each type given in the allowed types array additional validators may be added to the property:

{
    "$id": "example",
    "type": "object",
    "properties": {
        "example": {
            "type": ["number", "string", "array"],
            "minimum": 10,
            "minLength": 4,
            "items": {
                "type": "string"
            },
            "minItems": 2
        }
    }
}

The property example will be type hinted with float | string | string[]. The validators are applied if the given input matches the corresponding type. For example if an array [“Hello”, 123, “Goodbye”] is given the validation will fail as numbers aren’t allowed in arrays:

Invalid items in array example:
  - invalid item #1
    * Invalid type for item of array example. Requires string, got integer