wol-soft/php-json-schema-model-generator

View on GitHub
src/SchemaProcessor/PostProcessor/Templates/AdditionalProperties/SetAdditionalProperty.phptpl

Summary

Maintainability
Test Coverage
/**
 * Adds or overwrites an additional property on the object
 *
 * @param string $property The property key of the additional property
 * @param {% if validationProperty %}{{ viewHelper.getTypeHintAnnotation(validationProperty) }}{% else %}mixed{% endif %} $value The new value of the additional property
 *
 * @return self
 *
 * {% if schema.getBaseValidators() %}@throws {% if generatorConfiguration.collectErrors() %}{{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}{% else %}ValidationException{% endif %}{% endif %}
 * @throws RegularPropertyAsAdditionalPropertyException
 */
public function setAdditionalProperty(
    string $property,
    {% if validationProperty %}{{ viewHelper.getType(validationProperty) }}{% endif %} $value
): self {
    if (in_array($property, {{ objectProperties }})) {
        throw new RegularPropertyAsAdditionalPropertyException($value, $property, self::class);
    }

    if (isset($this->_additionalProperties[$property]) && $this->_additionalProperties[$property] === $value) {
        return $this;
    }

    {% if validationProperty %}{{ schemaHookResolver.resolveSetterBeforeValidationHook(validationProperty) }}{% endif %}

    {% if schema.getBaseValidators() %}
        {% if generatorConfiguration.collectErrors() %}
            $this->_errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
        {% endif %}

        $addedProperty = [$property => $value];
        $this->executeBaseValidators($addedProperty);

        {% if generatorConfiguration.collectErrors() %}
            if (count($this->_errorRegistry->getErrors())) {
                throw $this->_errorRegistry;
            }
        {% endif %}
    {% else %}
        $this->_additionalProperties[$property] = $value;
    {% endif %}

    $this->_rawModelDataInput[$property] = $value;

    {% if validationProperty %}{{ schemaHookResolver.resolveSetterAfterValidationHook(validationProperty) }}{% endif %}

    return $this;
}