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 setExample(float $example): static;
public function getExample(): float;

public function patternProperties(): PatternPropertiesAccessor;

The patternProperties() method returns an accessor object with the following interface:

public function get(string $key): array;

get: Returns all properties matching the given pattern as a key-value array. As $key provide the pattern you want to fetch. Alternatively you can define a key in your schema and use that key to fetch the properties.

When at least one pattern type is not the bare mixed/untyped case, a typed companion class {ModelName}PatternProperties is generated that narrows the return annotation of get().

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

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

// fetches all properties matching the pattern '^b' (which has a defined key), consequently will return ['b1' => 100]
$myObject->patternProperties()->get('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