PatternPropertiesAccessorPostProcessor

$generator = new ModelGenerator();
$generator->addPostProcessor(new PatternPropertiesAccessorPostProcessor());

The PatternPropertiesAccessorPostProcessor adds methods to your model to work with pattern properties on your objects. The methods will only be added if the schema for the object defines pattern properties.

Added methods

{
    "$id": "example",
    "type": "object",
    "properties": {
        "example": {
            "type": "string"
        }
    },
    "patternProperties": {
        "^a": {
            "type": "string"
        },
        "^b": {
            "key": "numbers"
            "type": "integer"
        },
    }
}

Generated interface with the PatternPropertiesAccessorPostProcessor:

public function getRawModelDataInput(): array;

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

public function getPatternProperties(string $key): array;

The added method getPatternProperties can be used to fetch a list of all properties matching the given pattern. As $key you have to provide the pattern you want to fetch. Alternatively you can define a key in your schema and use the key to fetch the properties.

$myObject = new Example('a1' => 'Hello', 'b1' => 100);

// fetches all properties matching the pattern '^a', consequently will return ['a1' => 'Hello']
$myObject->getPatternProperties('^a');

// fetches all properties matching the pattern '^b' (which has a defined key), consequently will return ['b1' => 100]
$myObject->getPatternProperties('numbers');

Note

If you want to modify your object by adding or removing pattern properties after the object instantiation you can use the AdditionalPropertiesAccessorPostProcessor or the PopulatePostProcessor