Skip to content

Commit 8d32091

Browse files
committed
BaseProcessor
1 parent fb3340e commit 8d32091

File tree

5 files changed

+148
-16
lines changed

5 files changed

+148
-16
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\PropertyProcessor\Property;
6+
7+
use PHPModelGenerator\Exception\InvalidArgumentException;
8+
use PHPModelGenerator\Model\Property\Property;
9+
use PHPModelGenerator\Model\Property\PropertyInterface;
10+
use PHPModelGenerator\Model\Validator\PropertyValidator;
11+
use PHPModelGenerator\PropertyProcessor\PropertyCollectionProcessor;
12+
use PHPModelGenerator\PropertyProcessor\PropertyFactory;
13+
use PHPModelGenerator\PropertyProcessor\PropertyProcessorFactory;
14+
15+
/**
16+
* Class BaseObjectProcessor
17+
*
18+
* @package PHPModelGenerator\PropertyProcessor\Property
19+
*/
20+
class BaseProcessor extends AbstractPropertyProcessor
21+
{
22+
protected const TYPE = 'object';
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public function process(string $propertyName, array $propertyData): PropertyInterface
28+
{
29+
$property = new Property($propertyName, static::TYPE);
30+
31+
$this->generateValidators($property, $propertyData);
32+
33+
$this->addAdditionalPropertiesValidator($propertyData);
34+
$this->addMinPropertiesValidator($propertyData);
35+
$this->addMaxPropertiesValidator($propertyData);
36+
37+
$propertyFactory = new PropertyFactory(new PropertyProcessorFactory());
38+
$propertyCollectionProcessor = new PropertyCollectionProcessor($propertyData['required'] ?? []);
39+
40+
foreach ($propertyData['properties'] as $propertyName => $propertyStructure) {
41+
$this->schema->addProperty(
42+
$propertyFactory->create(
43+
$propertyCollectionProcessor,
44+
$this->schemaProcessor,
45+
$this->schema,
46+
$propertyName,
47+
$propertyStructure
48+
)
49+
);
50+
}
51+
52+
return $property;
53+
}
54+
55+
public function addAdditionalPropertiesValidator(array $propertyData): void
56+
{
57+
if (!isset($propertyData['additionalProperties']) || $propertyData['additionalProperties'] === true) {
58+
return;
59+
}
60+
61+
$this->schema->addBaseValidator(
62+
new PropertyValidator(
63+
sprintf(
64+
'array_diff(array_keys($modelData), %s)',
65+
preg_replace('(\d+\s=>)', '', var_export(array_keys($structure['properties'] ?? []), true))
66+
),
67+
InvalidArgumentException::class,
68+
'Provided JSON contains not allowed additional properties'
69+
)
70+
);
71+
}
72+
73+
public function addMaxPropertiesValidator(array $propertyData): void
74+
{
75+
if (!isset($propertyData['maxProperties'])) {
76+
return;
77+
}
78+
79+
$this->schema->addBaseValidator(
80+
new PropertyValidator(
81+
sprintf('count($modelData) > %d', $propertyData['maxProperties']),
82+
InvalidArgumentException::class,
83+
"Provided object must not contain more than {$propertyData['maxProperties']} properties"
84+
)
85+
);
86+
}
87+
88+
public function addMinPropertiesValidator(array $propertyData): void
89+
{
90+
if (!isset($propertyData['minProperties'])) {
91+
return;
92+
}
93+
94+
$this->schema->addBaseValidator(
95+
new PropertyValidator(
96+
sprintf('count($modelData) < %d', $propertyData['minProperties']),
97+
InvalidArgumentException::class,
98+
"Provided object must not contain less than {$propertyData['minProperties']} properties"
99+
)
100+
);
101+
}
102+
}

src/SchemaProcessor/SchemaProcessor.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use PHPModelGenerator\Model\GeneratorConfiguration;
99
use PHPModelGenerator\Model\RenderJob;
1010
use PHPModelGenerator\Model\Schema;
11+
use PHPModelGenerator\PropertyProcessor\PropertyCollectionProcessor;
12+
use PHPModelGenerator\PropertyProcessor\PropertyFactory;
13+
use PHPModelGenerator\PropertyProcessor\PropertyProcessorFactory;
1114

1215
/**
1316
* Class SchemaProcessor
@@ -113,14 +116,24 @@ protected function generateModel(
113116
array $parentDefinitions = []
114117
): void {
115118
$schema = new Schema($parentDefinitions);
116-
$schemaPropertyProcessorFactory = new SchemaPropertyProcessorFactory();
119+
//$schemaPropertyProcessorFactory = new SchemaPropertyProcessorFactory();
117120

121+
$structure['type'] = 'base';
122+
123+
(new PropertyFactory(new PropertyProcessorFactory()))->create(
124+
new PropertyCollectionProcessor($structure['required'] ?? []),
125+
$this,
126+
$schema,
127+
'SchemaBaseObject',
128+
$structure
129+
);
130+
/*
118131
foreach (array_keys($structure) as $schemaProperty) {
119132
$schemaPropertyProcessorFactory
120133
->getSchemaPropertyProcessor($schemaProperty)
121134
->process($this, $schema, $structure);
122135
}
123-
136+
*/
124137
$fileName = join(
125138
DIRECTORY_SEPARATOR,
126139
[$this->destination, str_replace('\\', DIRECTORY_SEPARATOR, $classPath), $className]

src/SchemaProcessor/SchemaProperty/PropertiesProcessor.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@ class PropertiesProcessor implements SchemaPropertyProcessorInterface
2323
*/
2424
public function process(SchemaProcessor $schemaProcessor, Schema $schema, array $structure): void
2525
{
26-
$propertyFactory = new PropertyFactory(new PropertyProcessorFactory());
27-
$propertyCollectionProcessor = new PropertyCollectionProcessor($structure['required'] ?? []);
2826

29-
foreach ($structure['properties'] as $propertyName => $propertyStructure) {
30-
$schema->addProperty(
31-
$propertyFactory->create(
32-
$propertyCollectionProcessor,
33-
$schemaProcessor,
34-
$schema,
35-
$propertyName,
36-
$propertyStructure
37-
)
38-
);
39-
}
4027
}
4128
}

tests/ComposedValue/ComposedNotTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function testNotMatchingObjectPropertyWithReferencedSchemaIsValid($proper
188188
$className = $this->generateObjectFromFile('ReferencedObjectSchema.json');
189189

190190
$object = new $className(['person' => $propertyValue]);
191-
$this->assertSame(['person' => $propertyValue], $object->getRawModelDataInput());
191+
$this->assertSame($propertyValue, $object->getPerson());
192192
}
193193

194194
public function objectPropertyWithReferencedSchemaDataProvider(): array
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"definitions": {
3+
"person": {
4+
"type": "object",
5+
"properties": {
6+
"name": {
7+
"type": "string",
8+
"minLength": 2
9+
},
10+
"age": {
11+
"type": "int"
12+
}
13+
},
14+
"required": [
15+
"name",
16+
"age"
17+
],
18+
"additionalProperties": false
19+
}
20+
},
21+
"type": "object",
22+
"allOf":
23+
"properties": {
24+
"person": {
25+
"not": {
26+
"$ref": "#/definitions/person"
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)