src/Templates/Model.phptpl
<?php
// @codingStandardsIgnoreFile
// @codeCoverageIgnoreStart
declare(strict_types = 1);
{% if namespace %}
namespace {{ namespace }};
{% endif %}
{% foreach use as import %}
use {{ import }};
{% endforeach %}
/**
* Class {{ class }}
{% if namespace %} * @package {{ namespace }} {% endif %}
*
{% if schema.getDescription() %} * {{ schema.getDescription() }}
*{% endif %}
* This is an auto-implemented class implemented by the php-json-schema-model-generator.
* If you need to implement something in this class use inheritance. Else you will lose your changes if the classes
* are re-generated.
*/
class {{ class }} {% if schema.getInterfaces() %}implements {{ viewHelper.joinClassNames(schema.getInterfaces()) }}{% endif %}
{
{% if schema.getTraits() %}use {{ viewHelper.joinClassNames(schema.getTraits()) }};{% endif %}
{% foreach schema.getProperties() as property %}
/**{% if viewHelper.getTypeHintAnnotation(property, true) %} @var {{ viewHelper.getTypeHintAnnotation(property, true) }}{% endif %}{% if property.getDescription() %} {{ property.getDescription() }}{% endif %} */
{% if property.isInternal() %}private{% else %}protected{% endif %} ${{ property.getAttribute(true) }}{% if not viewHelper.isNull(property.getDefaultValue()) %} = {{ property.getDefaultValue() }}{% endif %};
{% endforeach %}
/** @var array */
protected $_rawModelDataInput = [];
{% if generatorConfiguration.collectErrors() %}
/** @var {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }} Collect all validation errors */
protected $_errorRegistry;
{% endif %}
/**
* {{ class }} constructor.
*
* @param array $rawModelDataInput
*
* @throws {% if generatorConfiguration.collectErrors() %}{{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}{% else %}ValidationException{% endif %}
*/
public function __construct(array $rawModelDataInput = [])
{
{% if generatorConfiguration.collectErrors() %}
$this->_errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
{% endif %}
{{ schemaHookResolver.resolveConstructorBeforeValidationHook() }}
{% if schema.getBaseValidators() %}
$this->executeBaseValidators($rawModelDataInput);
{% endif %}
{% foreach schema.getProperties() as property %}
{% if not property.isInternal() %}
$this->process{{ viewHelper.ucfirst(property.getAttribute()) }}($rawModelDataInput);
{% endif %}
{% endforeach %}
{% if generatorConfiguration.collectErrors() %}
if (count($this->_errorRegistry->getErrors())) {
throw $this->_errorRegistry;
}
{% endif %}
$this->_rawModelDataInput = $rawModelDataInput;
{{ schemaHookResolver.resolveConstructorAfterValidationHook() }}
}
{% if schema.getBaseValidators() %}
protected function executeBaseValidators(array &$modelData): void
{
$value = &$modelData;
{% foreach baseValidatorsWithoutCompositions as validator %}
{{ viewHelper.renderValidator(validator, schema) }}
{% endforeach %}
{% foreach schema.getCompositionValidatorKeys() as compositionValidatorKey %}
$this->validateComposition_{{ compositionValidatorKey }}($modelData);
{% endforeach %}
}
{% endif %}
/**
* Get the raw input used to set up the model
*
* @return array
*/
public function getRawModelDataInput(): array
{
return $this->_rawModelDataInput;
}
{% foreach schema.getProperties() as property %}
{% if not property.isInternal() %}
/**
* Get the value of {{ property.getName() }}.
*
* {% if property.getDescription() %}{{ property.getDescription() }}{% endif %}
*
* {% if viewHelper.getTypeHintAnnotation(property, true) %}@return {{ viewHelper.getTypeHintAnnotation(property, true) }}{% endif %}
*/
public function get{{ viewHelper.ucfirst(property.getAttribute()) }}()
{% if property.getType(true) %}: {{ viewHelper.getType(property, true) }}{% endif %}
{
{{ schemaHookResolver.resolveGetterHook(property) }}
return $this->{{ property.getAttribute(true) }};
}
{% if not property.isReadOnly() %}
/**
* Set the value of {{ property.getName() }}.
*
* @param {{ viewHelper.getTypeHintAnnotation(property) }} ${{ property.getAttribute(true) }}{% if property.getDescription() %} {{ property.getDescription() }}{% endif %}
*
* {% if property.getValidators() %}@throws {% if generatorConfiguration.collectErrors() %}{{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}{% else %}ValidationException{% endif %}{% endif %}
*
* @return self
*/
public function set{{ viewHelper.ucfirst(property.getAttribute()) }}(
{{ viewHelper.getType(property) }} ${{ property.getAttribute(true) }}
): self {
if ($this->{{ property.getAttribute(true) }} === ${{ property.getAttribute(true) }}) {
return $this;
}
$value = $modelData['{{ property.getName() }}'] = ${{ property.getAttribute(true) }};
{% if generatorConfiguration.collectErrors() %}
$this->_errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
{% endif %}
{{ schemaHookResolver.resolveSetterBeforeValidationHook(property) }}
$value = $this->validate{{ viewHelper.ucfirst(property.getAttribute()) }}($value, $modelData);
{% if generatorConfiguration.collectErrors() %}
if ($this->_errorRegistry->getErrors()) {
throw $this->_errorRegistry;
}
{% endif %}
$this->{{ property.getAttribute(true) }} = $value;
$this->_rawModelDataInput['{{ property.getName() }}'] = ${{ property.getAttribute(true) }};
{{ schemaHookResolver.resolveSetterAfterValidationHook(property) }}
return $this;
}
{% endif %}
/**
* Extract the value, perform validations and set the property {{ property.getAttribute() }}
*
* @param array $modelData
*
* {% if property.getValidators() %}@throws {% if generatorConfiguration.collectErrors() %}{{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}{% else %}ValidationException{% endif %}{% endif %}
*/
protected function process{{ viewHelper.ucfirst(property.getAttribute()) }}(array $modelData): void
{
{% if not generatorConfiguration.isImplicitNullAllowed() %}
{% if not property.isRequired() %}
if (!array_key_exists('{{ property.getName() }}', $modelData) && $this->{{ property.getAttribute(true) }} === null) {
return;
}
{% endif %}
{% endif %}
$value = array_key_exists('{{ property.getName() }}', $modelData) ? $modelData['{{ property.getName() }}'] : $this->{{ property.getAttribute(true) }};
{{ viewHelper.resolvePropertyDecorator(property, true) }}
$this->{{ property.getAttribute(true) }} = $this->validate{{ viewHelper.ucfirst(property.getAttribute()) }}($value, $modelData);
}
/**
* Execute all validators for the property {{ property.getAttribute() }}
*/
protected function validate{{ viewHelper.ucfirst(property.getAttribute()) }}($value, array $modelData)
{
{% foreach property.getOrderedValidators() as validator %}
{{ viewHelper.renderValidator(validator, schema) }}
{% endforeach %}
return $value;
}
{% endif %}
{% endforeach %}
{{ viewHelper.renderMethods(schema) }}
}
// @codeCoverageIgnoreEnd