Skip to content

Commit

Permalink
Merge cd7fad5 into 632e4d7
Browse files Browse the repository at this point in the history
  • Loading branch information
wol-soft committed Jul 21, 2020
2 parents 632e4d7 + cd7fad5 commit 919031e
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 63 deletions.
16 changes: 5 additions & 11 deletions src/Model/RenderJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,25 @@ class RenderJob
protected $classPath;
/** @var string */
protected $fileName;
/** @var bool */
protected $initialClass;

/**
* Create a new class render job
*
* @param string $fileName The file name
* @param string $classPath The relative path of the class for namespace generation
* @param string $className The class name
* @param Schema $schema The Schema object which holds properties and validators
* @param bool $initialClass Render job for an initial class or render job for a nested class?
* @param string $fileName The file name
* @param string $classPath The relative path of the class for namespace generation
* @param string $className The class name
* @param Schema $schema The Schema object which holds properties and validators
*/
public function __construct(
string $fileName,
string $classPath,
string $className,
Schema $schema,
bool $initialClass
Schema $schema
) {
$this->fileName = $fileName;
$this->classPath = $classPath;
$this->className = $className;
$this->schema = $schema;
$this->initialClass = $initialClass;
}

/**
Expand Down Expand Up @@ -151,7 +146,6 @@ protected function renderClass(GeneratorConfiguration $generatorConfiguration):
'schema' => $this->schema,
'generatorConfiguration' => $generatorConfiguration,
'viewHelper' => new RenderHelper($generatorConfiguration),
'initialClass' => $this->initialClass,
// one hack a day keeps the problems away. Make true literal available for the templating. Easy fix
'true' => true,
]
Expand Down
13 changes: 12 additions & 1 deletion src/Model/Validator/AbstractPropertyValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace PHPModelGenerator\Model\Validator;

use PHPModelGenerator\Model\Property\Property;
use PHPModelGenerator\Model\Property\PropertyInterface;
use PHPModelGenerator\Model\Validator;

Expand All @@ -20,6 +19,18 @@ abstract class AbstractPropertyValidator implements PropertyValidatorInterface
/** @var array */
protected $exceptionParams;

/**
* AbstractPropertyValidator constructor.
*
* @param string $exceptionClass
* @param array $exceptionParams
*/
public function __construct(string $exceptionClass, array $exceptionParams = [])
{
$this->exceptionClass = $exceptionClass;
$this->exceptionParams = $exceptionParams;
}

/**
* @inheritDoc
*/
Expand Down
5 changes: 4 additions & 1 deletion src/Model/Validator/InstanceOfValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ class InstanceOfValidator extends PropertyValidator
public function __construct(PropertyInterface $property)
{
parent::__construct(
sprintf('is_object($value) && !($value instanceof %s)', $property->getType()),
sprintf(
'is_object($value) && !($value instanceof \Exception) && !($value instanceof %s)',
$property->getType()
),
InvalidInstanceOfException::class,
[$property->getName(), $property->getType()]
);
Expand Down
3 changes: 1 addition & 2 deletions src/Model/Validator/PropertyTemplateValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public function __construct(
$this->template = $template;
$this->templateValues = $templateValues;

$this->exceptionClass = $exceptionClass;
$this->exceptionParams = $exceptionParams;
parent::__construct($exceptionClass, $exceptionParams);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Validator/PropertyValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class PropertyValidator extends AbstractPropertyValidator
public function __construct(string $check, string $exceptionClass, array $exceptionParams = [])
{
$this->check = $check;
$this->exceptionClass = $exceptionClass;
$this->exceptionParams = $exceptionParams;

parent::__construct($exceptionClass, $exceptionParams);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPMicroTemplate\Render;
use PHPModelGenerator\Model\GeneratorConfiguration;
use PHPModelGenerator\Model\Property\PropertyInterface;
use PHPModelGenerator\Utils\RenderHelper;

/**
* Class ObjectInstantiationDecorator
Expand Down Expand Up @@ -46,12 +45,15 @@ public function __construct(string $className, GeneratorConfiguration $generator
*/
public function decorate(string $input, PropertyInterface $property): string
{
$template = $this->generatorConfiguration->collectErrors()
? 'ObjectInstantiationDecoratorErrorRegistry.phptpl'
: 'ObjectInstantiationDecoratorDirectException.phptpl';

return static::$renderer->renderTemplate(
DIRECTORY_SEPARATOR . 'Decorator' . DIRECTORY_SEPARATOR . 'ObjectInstantiationDecorator.phptpl',
DIRECTORY_SEPARATOR . 'Decorator' . DIRECTORY_SEPARATOR . $template,
[
'input' => $input,
'className' => $this->className,
'generatorConfiguration' => $this->generatorConfiguration,
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function populate(array $modelData): self
{
{% if generatorConfiguration.collectErrors() %}
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
{% endif%}
{% endif %}
$rollbackValues = [];

{% if schema.getBaseValidators() %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function removeAdditionalProperty(string $property): bool
{% if minPropertyValidator %}
{% if generatorConfiguration.collectErrors() %}
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
{% endif%}
{% endif %}

if ({{ minPropertyValidator.getCheck() }}) {
{{ viewHelper.validationError(minPropertyValidator) }}
Expand Down
8 changes: 3 additions & 5 deletions src/SchemaProcessor/SchemaProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected function generateModel(
$jsonSchema->withJson($json)
);

$this->generateClassFile($classPath, $className, $schema, $initialClass);
$this->generateClassFile($classPath, $className, $schema);

return $schema;
}
Expand All @@ -173,20 +173,18 @@ protected function generateModel(
* @param string $classPath
* @param string $className
* @param Schema $schema
* @param bool $initialClass
*/
public function generateClassFile(
string $classPath,
string $className,
Schema $schema,
bool $initialClass = false
Schema $schema
): void {
$fileName = join(
DIRECTORY_SEPARATOR,
[$this->destination, str_replace('\\', DIRECTORY_SEPARATOR, $classPath), $className]
) . '.php';

$this->renderQueue->addRenderJob(new RenderJob($fileName, $classPath, $className, $schema, $initialClass));
$this->renderQueue->addRenderJob(new RenderJob($fileName, $classPath, $className, $schema));

if ($this->generatorConfiguration->isOutputEnabled()) {
echo "Generated class {$this->generatorConfiguration->getNamespacePrefix()}\\$classPath\\$className\n";
Expand Down
7 changes: 0 additions & 7 deletions src/Templates/Decorator/ObjectInstantiationDecorator.phptpl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(is_array($param = {{ input }}) ? new {{ className }}($param) : $param)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(function ($value) {
try {
return is_array($value) ? new {{ className }}($value) : $value;
} catch (\Exception $instantiationException) {
foreach($instantiationException->getErrors() as $nestedValidationError) {
$this->errorRegistry->addError($nestedValidationError);
}

return $instantiationException;
}
})({{ input }})
31 changes: 9 additions & 22 deletions src/Templates/Model.phptpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,14 @@ class {{ class }} {% if schema.getInterfaces() %}implements {{ viewHelper.joinCl
* {{ class }} constructor.
*
* @param array $modelData
{% if generatorConfiguration.collectErrors() %}{% if not initialClass %}* @param {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }} $errorRegistry{% endif %}{% endif %}
*
* @throws {% if generatorConfiguration.collectErrors() %}{{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}{% else %}ValidationException{% endif %}
*/
public function __construct(
array $modelData = []
public function __construct(array $modelData = [])
{
{% if generatorConfiguration.collectErrors() %}
{% if not initialClass %}
, {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }} $errorRegistry = null
{% endif %}
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
{% endif %}
) {
{% if generatorConfiguration.collectErrors() %}
{% if initialClass %}
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
{% else %}
$this->errorRegistry = $errorRegistry;
{% endif %}
{% endif%}

{% if schema.getBaseValidators() %}
$this->executeBaseValidators($modelData);
Expand All @@ -65,12 +54,10 @@ class {{ class }} {% if schema.getInterfaces() %}implements {{ viewHelper.joinCl
{% endforeach %}

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

$this->rawModelDataInput = $modelData;
}
Expand Down Expand Up @@ -130,15 +117,15 @@ class {{ class }} {% if schema.getInterfaces() %}implements {{ viewHelper.joinCl

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

$value = $this->validate{{ viewHelper.ucfirst(property.getAttribute()) }}($value, $modelData);

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

$this->{{ property.getAttribute() }} = $value;

Expand Down
6 changes: 3 additions & 3 deletions src/Templates/Validator/ArrayContains.phptpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ is_array($value) && (function (&$items) {

{% if generatorConfiguration.collectErrors() %}
$originalErrorRegistry = $this->errorRegistry;
{% endif%}
{% endif %}

foreach ($items as &$value) {
try {
Expand All @@ -28,7 +28,7 @@ is_array($value) && (function (&$items) {
}

$this->errorRegistry = $originalErrorRegistry;
{% endif%}
{% endif %}

// one matched item is enough to pass the contains check
return false;
Expand All @@ -39,7 +39,7 @@ is_array($value) && (function (&$items) {

{% if generatorConfiguration.collectErrors() %}
$this->errorRegistry = $originalErrorRegistry;
{% endif%}
{% endif %}

return true;
})($value)
4 changes: 2 additions & 2 deletions src/Templates/Validator/ArrayItem.phptpl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
is_array($value) && (function (&$items) use (&$invalidItems{{ suffix }}) {
{% if generatorConfiguration.collectErrors() %}
$originalErrorRegistry = $this->errorRegistry;
{% endif%}
{% endif %}

foreach ($items as $index => &$value) {
{% if generatorConfiguration.collectErrors() %}
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
{% endif%}
{% endif %}

try {
{{ viewHelper.resolvePropertyDecorator(nestedProperty) }}
Expand Down
2 changes: 1 addition & 1 deletion src/Templates/Validator/ArrayTuple.phptpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
is_array($value) && (function (&$items) use (&$invalidTuples) {
{% if generatorConfiguration.collectErrors() %}
$originalErrorRegistry = $this->errorRegistry;
{% endif%}
{% endif %}

$index = 0;
{% foreach tupleProperties as tuple %}
Expand Down
2 changes: 1 addition & 1 deletion src/Templates/Validator/SchemaDependency.phptpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ array_key_exists('{{ propertyName }}', $modelData) && (function () use ($modelDa
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
{% else %}
try {
{% endif%}
{% endif %}

$value = $modelData;
{{ viewHelper.resolvePropertyDecorator(nestedProperty) }}
Expand Down

0 comments on commit 919031e

Please sign in to comment.