Skip to content

Commit f85d42e

Browse files
committed
* Added PatternPropertiesAccessorPostProcessor
* moved SerializationPostProcessor to internal post processors * Basic implementation of interaction between overlapping properties * object properties and pattern properties * additional properties and pattern properties
1 parent b1e994d commit f85d42e

19 files changed

+397
-27
lines changed

docs/source/gettingStarted.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The recommended way to install php-json-model-generator is through `Composer <ht
1111
composer require --dev wol-soft/php-json-schema-model-generator
1212
composer require wol-soft/php-json-schema-model-generator-production
1313
14-
To avoid adding all dependencies of the php-json-model-generator to your production dependencies it's recommended to add the library as a dev-dependency and include the php-json-model-generator-exception library. The exception library provides all classes to run the generated code. Generating the classes should either be a step done in the development environment or as a build step of your application (which is the recommended workflow).
14+
To avoid adding all dependencies of the php-json-model-generator to your production dependencies it's recommended to add the library as a dev-dependency and include the php-json-model-generator-production library. The production library provides all classes to run the generated code. Generating the classes should either be a step done in the development environment or as a build step of your application (which is the recommended workflow).
1515

1616
Generating classes
1717
------------------

src/Model/GeneratorConfiguration.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPModelGenerator\Exception\InvalidFilterException;
88
use PHPModelGenerator\Filter\FilterInterface;
99
use PHPModelGenerator\Filter\TransformingFilterInterface;
10-
use PHPModelGenerator\Format\FormatValidatorFromRegEx;
1110
use PHPModelGenerator\Format\FormatValidatorInterface;
1211
use PHPModelGenerator\PropertyProcessor\Filter\DateTimeFilter;
1312
use PHPModelGenerator\PropertyProcessor\Filter\NotEmptyFilter;

src/Model/Validator/PatternPropertiesValidator.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class PatternPropertiesValidator extends PropertyTemplateValidator
2525
{
2626
/** @var PropertyInterface */
2727
private $validationProperty;
28+
/** @var string */
29+
private $pattern;
30+
/** @var bool */
31+
private $collectPatternProperties = false;
2832

2933
/**
3034
* PatternPropertiesValidator constructor.
@@ -42,6 +46,7 @@ public function __construct(
4246
string $pattern,
4347
JsonSchema $propertyStructure
4448
) {
49+
$this->pattern = $pattern;
4550
$propertyFactory = new PropertyFactory(new PropertyProcessorFactory());
4651

4752
$this->validationProperty = $propertyFactory->create(
@@ -56,16 +61,27 @@ public function __construct(
5661
new Property($schema->getClassName(), null, $propertyStructure),
5762
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'PatternProperties.phptpl',
5863
[
59-
'pattern' => "/$pattern/",
64+
'patternHash' => md5($propertyStructure->getJson()['key'] ?? $this->pattern),
65+
'pattern' => "/{$this->pattern}/",
6066
'validationProperty' => $this->validationProperty,
6167
'generatorConfiguration' => $schemaProcessor->getGeneratorConfiguration(),
6268
'viewHelper' => new RenderHelper($schemaProcessor->getGeneratorConfiguration()),
69+
'collectPatternProperties' => &$this->collectPatternProperties,
70+
'schemaProperties' => $schema->getProperties(),
6371
],
6472
InvalidPatternPropertiesException::class,
65-
[$pattern, '&$invalidProperties']
73+
[$this->pattern, '&$invalidProperties']
6674
);
6775
}
6876

77+
/**
78+
* @param bool $collectPatternProperties
79+
*/
80+
public function setCollectPatternProperties(bool $collectPatternProperties): void
81+
{
82+
$this->collectPatternProperties = $collectPatternProperties;
83+
}
84+
6985
/**
7086
* @inheritDoc
7187
*/
@@ -76,6 +92,14 @@ public function getCheck(): string
7692
return parent::getCheck();
7793
}
7894

95+
/**
96+
* @return string
97+
*/
98+
public function getPattern(): string
99+
{
100+
return $this->pattern;
101+
}
102+
79103
/**
80104
* Initialize all variables which are required to execute a property names validator
81105
*

src/ModelGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use PHPModelGenerator\Exception\SchemaException;
1111
use PHPModelGenerator\Model\GeneratorConfiguration;
1212
use PHPModelGenerator\SchemaProcessor\PostProcessor\Internal\CompositionValidationPostProcessor;
13+
use PHPModelGenerator\SchemaProcessor\PostProcessor\Internal\SerializationPostProcessor;
1314
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
14-
use PHPModelGenerator\SchemaProcessor\PostProcessor\SerializationPostProcessor;
1515
use PHPModelGenerator\SchemaProcessor\RenderQueue;
1616
use PHPModelGenerator\SchemaProcessor\SchemaProcessor;
1717
use PHPModelGenerator\SchemaProvider\SchemaProviderInterface;
@@ -43,7 +43,7 @@ public function __construct(GeneratorConfiguration $generatorConfiguration = nul
4343
$this->addPostProcessor(new SerializationPostProcessor());
4444
}
4545

46-
// add internal post processors
46+
// add internal post processors which must always be executed
4747
$this->addPostProcessor(new CompositionValidationPostProcessor());
4848
}
4949

src/PropertyProcessor/Property/BaseProcessor.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ public function process(string $propertyName, JsonSchema $propertySchema): Prope
7272
$property = new BaseProperty($propertyName, new PropertyType(static::TYPE), $propertySchema);
7373
$this->generateValidators($property, $propertySchema);
7474

75+
$this->addPropertiesToSchema($propertySchema);
76+
$this->transferComposedPropertiesToSchema($property);
77+
7578
$this->addPropertyNamesValidator($propertySchema);
7679
$this->addPatternPropertiesValidator($propertySchema);
7780
$this->addAdditionalPropertiesValidator($propertySchema);
7881

7982
$this->addMinPropertiesValidator($propertyName, $propertySchema);
8083
$this->addMaxPropertiesValidator($propertyName, $propertySchema);
8184

82-
$this->addPropertiesToSchema($propertySchema);
83-
$this->transferComposedPropertiesToSchema($property);
84-
8585
return $property;
8686
}
8787

@@ -179,14 +179,14 @@ protected function addPatternPropertiesValidator(JsonSchema $propertySchema): vo
179179
);
180180
}
181181

182-
$this->schema->addBaseValidator(
183-
new PatternPropertiesValidator(
184-
$this->schemaProcessor,
185-
$this->schema,
186-
$pattern,
187-
$propertySchema->withJson($schema)
188-
)
182+
$validator = new PatternPropertiesValidator(
183+
$this->schemaProcessor,
184+
$this->schema,
185+
$pattern,
186+
$propertySchema->withJson($schema)
189187
);
188+
189+
$this->schema->addBaseValidator($validator);
190190
}
191191
}
192192

src/SchemaProcessor/PostProcessor/AdditionalPropertiesAccessorPostProcessor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private function addSerializeAdditionalPropertiesMethod(
144144
new RenderedMethod(
145145
$schema,
146146
$generatorConfiguration,
147-
'AdditionalPropertiesSerializer.phptpl',
147+
'AdditionalProperties/AdditionalPropertiesSerializer.phptpl',
148148
[
149149
'serializerClass' => $serializerClass ?? null,
150150
'serializerMethod' => $serializerMethod ?? null,
@@ -185,7 +185,7 @@ private function addSetAdditionalPropertyMethod(
185185
new RenderedMethod(
186186
$schema,
187187
$generatorConfiguration,
188-
'SetAdditionalProperty.phptpl',
188+
'AdditionalProperties/SetAdditionalProperty.phptpl',
189189
[
190190
'validationProperty' => $validationProperty,
191191
'objectProperties' => $objectProperties,
@@ -227,7 +227,7 @@ private function addRemoveAdditionalPropertyMethod(
227227
new RenderedMethod(
228228
$schema,
229229
$generatorConfiguration,
230-
'RemoveAdditionalProperty.phptpl',
230+
'AdditionalProperties/RemoveAdditionalProperty.phptpl',
231231
['minPropertyValidator' => $minPropertyValidator]
232232
)
233233
);
@@ -258,7 +258,7 @@ private function addGetAdditionalPropertyMethod(
258258
new RenderedMethod(
259259
$schema,
260260
$generatorConfiguration,
261-
'GetAdditionalProperty.phptpl',
261+
'AdditionalProperties/GetAdditionalProperty.phptpl',
262262
[
263263
'validationProperty' => $validationProperty
264264
// type hint always with null as a non existent property may be requested (casually covered by

src/SchemaProcessor/PostProcessor/SerializationPostProcessor.php renamed to src/SchemaProcessor/PostProcessor/Internal/SerializationPostProcessor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
declare(strict_types = 1);
44

5-
namespace PHPModelGenerator\SchemaProcessor\PostProcessor;
5+
namespace PHPModelGenerator\SchemaProcessor\PostProcessor\Internal;
66

77
use JsonSerializable;
88
use PHPModelGenerator\Filter\TransformingFilterInterface;
99
use PHPModelGenerator\Interfaces\SerializationInterface;
1010
use PHPModelGenerator\Model\GeneratorConfiguration;
1111
use PHPModelGenerator\Model\Schema;
1212
use PHPModelGenerator\Model\Validator\FilterValidator;
13+
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
14+
use PHPModelGenerator\SchemaProcessor\PostProcessor\RenderedMethod;
1315
use PHPModelGenerator\Traits\SerializableTrait;
1416

1517
/**

0 commit comments

Comments
 (0)