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

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

Summary

Maintainability
Test Coverage
/**
 * Removes an additional property from the object. Returns true if the property has been removed,
 * false if the requested additional property doesn't exist
 *
 * @param string $property The property key of the additional property
 *
 * @return bool
 *
 * {% if minPropertyValidator %}@throws {% if generatorConfiguration.collectErrors() %}{{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}{% else %}ValidationException{% endif %}{% endif %}
 */
public function removeAdditionalProperty(string $property): bool
{
    if (isset($this->_patternProperties)) {
        foreach ($this->_patternProperties as $patternHash => $_) {
            if (isset($this->_patternProperties[$patternHash][$property])) {
                unset($this->_patternProperties[$patternHash][$property]);
                unset($this->_rawModelDataInput[$property]);
            }
        }
    }

    if (!array_key_exists($property, $this->_additionalProperties)) {
        return false;
    }

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

        if ({{ minPropertyValidator.getCheck() }}) {
            {{ viewHelper.validationError(minPropertyValidator) }}
        }

        {% if generatorConfiguration.collectErrors() %}
            if (count($this->_errorRegistry->getErrors())) {
                throw $this->_errorRegistry;
            }
        {% endif %}
    {% endif %}

    unset($this->_rawModelDataInput[$property]);
    unset($this->_additionalProperties[$property]);

    return true;
}