Skip to content

Commit aa83784

Browse files
committed
* Pattern properties must not occur in the additional properties
* Throw an exception for an invalid string pattern validator
1 parent 1553c9c commit aa83784

18 files changed

+131
-53
lines changed

src/Model/Validator/AdditionalPropertiesValidator.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,17 @@ public function __construct(
6161
$propertiesStructure->withJson($propertiesStructure->getJson()[static::ADDITIONAL_PROPERTIES_KEY])
6262
);
6363

64+
$patternProperties = array_keys($schema->getJsonSchema()->getJson()['patternProperties'] ?? []);
65+
6466
parent::__construct(
6567
new Property($propertyName ?? $schema->getClassName(), null, $propertiesStructure),
6668
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'AdditionalProperties.phptpl',
6769
[
6870
'validationProperty' => $this->validationProperty,
69-
'additionalProperties' => preg_replace(
70-
'(\d+\s=>)',
71-
'',
72-
var_export(array_keys($propertiesStructure->getJson()[static::PROPERTIES_KEY] ?? []), true)
71+
'additionalProperties' => RenderHelper::varExportArray(
72+
array_keys($propertiesStructure->getJson()[static::PROPERTIES_KEY] ?? [])
7373
),
74+
'patternProperties' => $patternProperties ? RenderHelper::varExportArray($patternProperties) : null,
7475
'generatorConfiguration' => $schemaProcessor->getGeneratorConfiguration(),
7576
'viewHelper' => new RenderHelper($schemaProcessor->getGeneratorConfiguration()),
7677
// by default don't collect additional property data

src/Model/Validator/EnumValidator.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPModelGenerator\Exception\Generic\EnumException;
88
use PHPModelGenerator\Model\Property\PropertyInterface;
9+
use PHPModelGenerator\Utils\RenderHelper;
910

1011
/**
1112
* Class EnumValidator
@@ -25,9 +26,7 @@ public function __construct(PropertyInterface $property, array $allowedValues)
2526

2627
parent::__construct(
2728
$property,
28-
'!in_array($value, ' .
29-
preg_replace('(\d+\s=>)', '', var_export($allowedValues, true)) .
30-
', true)',
29+
'!in_array($value, ' . RenderHelper::varExportArray($allowedValues) . ', true)',
3130
EnumException::class,
3231
[$allowedValues]
3332
);

src/Model/Validator/PropertyDependencyValidator.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPModelGenerator\Exception\Dependency\InvalidPropertyDependencyException;
88
use PHPModelGenerator\Model\Property\PropertyInterface;
9+
use PHPModelGenerator\Utils\RenderHelper;
910

1011
/**
1112
* Class PropertyDependencyValidator
@@ -26,11 +27,7 @@ public function __construct(PropertyInterface $property, array $dependencies)
2627
$property,
2728
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'PropertyDependency.phptpl',
2829
[
29-
'dependencies' => preg_replace(
30-
'(\d+\s=>)',
31-
'',
32-
var_export(array_values($dependencies), true)
33-
),
30+
'dependencies' => RenderHelper::varExportArray(array_values($dependencies)),
3431
],
3532
InvalidPropertyDependencyException::class,
3633
['&$missingAttributes']

src/PropertyProcessor/Property/BaseProcessor.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use PHPModelGenerator\PropertyProcessor\PropertyMetaDataCollection;
3030
use PHPModelGenerator\PropertyProcessor\PropertyFactory;
3131
use PHPModelGenerator\PropertyProcessor\PropertyProcessorFactory;
32+
use PHPModelGenerator\Utils\RenderHelper;
3233

3334
/**
3435
* Class BaseObjectProcessor
@@ -151,7 +152,7 @@ protected function addAdditionalPropertiesValidator(JsonSchema $propertySchema):
151152
new Property($this->schema->getClassName(), null, $propertySchema),
152153
sprintf(
153154
'$additionalProperties = array_diff(array_keys($modelData), %s)',
154-
preg_replace('(\d+\s=>)', '', var_export(array_keys($json['properties'] ?? []), true))
155+
RenderHelper::varExportArray(array_keys($json['properties'] ?? []))
155156
),
156157
AdditionalPropertiesException::class,
157158
['&$additionalProperties']
@@ -173,9 +174,9 @@ protected function addPatternPropertiesValidator(JsonSchema $propertySchema): vo
173174
}
174175

175176
foreach ($json['patternProperties'] as $pattern => $schema) {
176-
if (preg_match("/$pattern/", '') === false) {
177+
if (@preg_match("/$pattern/", '') === false) {
177178
throw new SchemaException(
178-
"Invalid pattern $pattern for pattern property in file {$propertySchema->getFile()}"
179+
"Invalid pattern '$pattern' for pattern property in file {$propertySchema->getFile()}"
179180
);
180181
}
181182

src/PropertyProcessor/Property/StringProcessor.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ protected function generateValidators(PropertyInterface $property, JsonSchema $p
4747
*
4848
* @param PropertyInterface $property
4949
* @param JsonSchema $propertySchema
50+
*
51+
* @throws SchemaException
5052
*/
5153
protected function addPatternValidator(PropertyInterface $property, JsonSchema $propertySchema): void
5254
{
@@ -56,6 +58,17 @@ protected function addPatternValidator(PropertyInterface $property, JsonSchema $
5658
return;
5759
}
5860

61+
if (@preg_match("/{$json[static::JSON_FIELD_PATTERN]}/", '') === false) {
62+
throw new SchemaException(
63+
sprintf(
64+
"Invalid pattern '%s' for property '%s' in file %s",
65+
$json[static::JSON_FIELD_PATTERN],
66+
$property->getName(),
67+
$propertySchema->getFile()
68+
)
69+
);
70+
}
71+
5972
$json[static::JSON_FIELD_PATTERN] = addcslashes($json[static::JSON_FIELD_PATTERN], "'");
6073

6174
$property->addValidator(

src/SchemaProcessor/PostProcessor/AdditionalPropertiesAccessorPostProcessor.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PHPModelGenerator\PropertyProcessor\Decorator\TypeHint\ArrayTypeHintDecorator;
2424
use PHPModelGenerator\PropertyProcessor\Decorator\TypeHint\TypeHintDecorator;
2525
use PHPModelGenerator\SchemaProcessor\Hook\SchemaHookResolver;
26+
use PHPModelGenerator\Utils\RenderHelper;
2627

2728
/**
2829
* Class AdditionalPropertiesAccessorPostProcessor
@@ -179,15 +180,10 @@ private function addSetAdditionalPropertyMethod(
179180
GeneratorConfiguration $generatorConfiguration,
180181
?PropertyInterface $validationProperty
181182
): void {
182-
$objectProperties = preg_replace(
183-
'(\d+\s=>)',
184-
'',
185-
var_export(
186-
array_map(function (PropertyInterface $property): string {
187-
return $property->getName();
188-
}, $schema->getProperties()),
189-
true
190-
)
183+
$objectProperties = RenderHelper::varExportArray(
184+
array_map(function (PropertyInterface $property): string {
185+
return $property->getName();
186+
}, $schema->getProperties())
191187
);
192188

193189
$schema->addUsedClass(RegularPropertyAsAdditionalPropertyException::class);
@@ -295,6 +291,8 @@ private function addUpdateAdditionalProperties(Schema $schema): void
295291
new class ($schema) extends PropertyTemplateValidator {
296292
public function __construct(Schema $schema)
297293
{
294+
$patternProperties = array_keys($schema->getJsonSchema()->getJson()['patternProperties'] ?? []);
295+
298296
parent::__construct(
299297
new Property($schema->getClassName(), null, $schema->getJsonSchema()),
300298
join(
@@ -309,13 +307,11 @@ public function __construct(Schema $schema)
309307
]
310308
),
311309
[
312-
'additionalProperties' => preg_replace(
313-
'(\d+\s=>)',
314-
'',
315-
var_export(
316-
array_keys($schema->getJsonSchema()->getJson()['properties'] ?? []),
317-
true
318-
)
310+
'patternProperties' => $patternProperties
311+
? RenderHelper::varExportArray($patternProperties)
312+
: null,
313+
'additionalProperties' => RenderHelper::varExportArray(
314+
array_keys($schema->getJsonSchema()->getJson()['properties'] ?? [])
319315
),
320316
],
321317
Exception::class

src/SchemaProcessor/PostProcessor/PatternPropertiesAccessorPostProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu
4949
$key = $json['patternProperties'][$validator->getPattern()]['key'] ?? $validator->getPattern();
5050
$hash = md5($key);
5151

52-
if (in_array($hash, $patternHashes)) {
52+
if (array_key_exists($hash, $patternHashes)) {
5353
throw new SchemaException(
54-
"Duplicate pattern property key '$key' in file {$schema->getJsonSchema()->getFile()}"
54+
"Duplicate pattern property access key '$key' in file {$schema->getJsonSchema()->getFile()}"
5555
);
5656
}
5757

src/SchemaProcessor/PostProcessor/Templates/AdditionalProperties/RemoveAdditionalProperty.phptpl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
*/
1111
public function removeAdditionalProperty(string $property): bool
1212
{
13+
if (isset($this->_patternProperties)) {
14+
foreach ($this->_patternProperties as $patternHash => $_) {
15+
if (isset($this->_patternProperties[$patternHash][$property])) {
16+
unset($this->_patternProperties[$patternHash][$property]);
17+
unset($this->_rawModelDataInput[$property]);
18+
}
19+
}
20+
}
21+
1322
if (!array_key_exists($property, $this->additionalProperties)) {
1423
return false;
1524
}
@@ -33,11 +42,5 @@ public function removeAdditionalProperty(string $property): bool
3342
unset($this->_rawModelDataInput[$property]);
3443
unset($this->additionalProperties[$property]);
3544

36-
if (isset($this->_patternProperties)) {
37-
foreach ($this->_patternProperties as $patternHash => $_) {
38-
unset($this->_patternProperties[$patternHash][$property]);
39-
}
40-
}
41-
4245
return true;
4346
}

src/SchemaProcessor/PostProcessor/Templates/AdditionalProperties/UpdateAdditionalProperties.phptpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
(function () use ($value) {
22
foreach (array_diff(array_keys($value), {{ additionalProperties }}) as $propertyKey) {
3+
{% if patternProperties %}
4+
foreach ({{ patternProperties }} as $pattern) {
5+
if (preg_match("/$pattern/", $propertyKey)) {
6+
continue 2;
7+
}
8+
}
9+
{% endif %}
10+
311
$this->additionalProperties[$propertyKey] = $value[$propertyKey];
412
}
513

src/Templates/Validator/AdditionalProperties.phptpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
{% endif %}
88

99
foreach (array_diff(array_keys($properties), {{ additionalProperties }}) as $propertyKey) {
10+
{% if patternProperties %}
11+
foreach ({{ patternProperties }} as $pattern) {
12+
if (preg_match("/$pattern/", $property)) {
13+
continue 2;
14+
}
15+
}
16+
{% endif %}
17+
1018
try {
1119
$value = $properties[$propertyKey];
1220

0 commit comments

Comments
 (0)