Skip to content

Commit c622865

Browse files
committed
Extract schema definition dictionary
1 parent c0dc589 commit c622865

File tree

12 files changed

+147
-117
lines changed

12 files changed

+147
-117
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"require": {
1414
"php": ">=7.2",
1515
"wol-soft/php-micro-template": "^1.3",
16-
"symplify/easy-coding-standard": "^4.1"
16+
"symplify/easy-coding-standard": "^4.1",
17+
"ext-json": "*"
1718
},
1819
"require-dev": {
1920
"phpunit/phpunit": "^8.0"

src/Model/Property/CompositionPropertyDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace PHPModelGenerator\Model\Property;
66

7-
use PHPModelGenerator\Model\ResolvedDefinitionsCollection;
7+
use PHPModelGenerator\Model\SchemaDefinition\ResolvedDefinitionsCollection;
88

99
/**
1010
* Class CompositionPropertyDecorator

src/Model/Property/PropertyProxy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace PHPModelGenerator\Model\Property;
66

7-
use PHPModelGenerator\Model\ResolvedDefinitionsCollection;
7+
use PHPModelGenerator\Model\SchemaDefinition\ResolvedDefinitionsCollection;
88
use PHPModelGenerator\Model\Schema;
99
use PHPModelGenerator\Model\Validator\PropertyValidatorInterface;
1010
use PHPModelGenerator\PropertyProcessor\Decorator\PropertyDecoratorInterface;

src/Model/Schema.php

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPModelGenerator\Exception\SchemaException;
99
use PHPModelGenerator\Model\Property\Property;
1010
use PHPModelGenerator\Model\Property\PropertyInterface;
11+
use PHPModelGenerator\Model\SchemaDefinition\SchemaDefinitionDictionary;
1112
use PHPModelGenerator\Model\Validator\PropertyValidator;
1213
use PHPModelGenerator\Model\Validator\PropertyValidatorInterface;
1314

@@ -24,12 +25,17 @@ class Schema
2425
* before adding properties to the model
2526
*/
2627
protected $baseValidators = [];
27-
/** @var SchemaDefinition[] Hold all definitions of the schema */
28-
protected $definitions;
28+
/** @var SchemaDefinitionDictionary */
29+
protected $schemaDefinitionDictionary;
2930

30-
public function __construct(array $parentDefinitions = [])
31+
/**
32+
* Schema constructor.
33+
*
34+
* @param SchemaDefinitionDictionary $dictionary
35+
*/
36+
public function __construct(SchemaDefinitionDictionary $dictionary)
3137
{
32-
$this->definitions = $parentDefinitions;
38+
$this->schemaDefinitionDictionary = $dictionary;
3339
}
3440

3541
/**
@@ -111,39 +117,10 @@ public function getUseList(bool $skipGlobalNamespace): array
111117
}
112118

113119
/**
114-
* Add a partial schema definition to the schema
115-
*
116-
* @param string $key
117-
* @param SchemaDefinition $definition
118-
*
119-
* @return Schema
120-
*/
121-
public function addDefinition(string $key, SchemaDefinition $definition): self
122-
{
123-
if (isset($this->definitions[$key])) {
124-
return $this;
125-
}
126-
127-
$this->definitions[$key] = $definition;
128-
129-
return $this;
130-
}
131-
132-
/**
133-
* @param string $key
134-
*
135-
* @return SchemaDefinition
136-
*/
137-
public function getDefinition(string $key): ?SchemaDefinition
138-
{
139-
return $this->definitions[$key] ?? null;
140-
}
141-
142-
/**
143-
* @return array
120+
* @return SchemaDefinitionDictionary
144121
*/
145-
public function getDefinitions(): array
122+
public function getSchemaDictionary(): SchemaDefinitionDictionary
146123
{
147-
return $this->definitions;
124+
return $this->schemaDefinitionDictionary;
148125
}
149126
}

src/Model/ResolvedDefinitionsCollection.php renamed to src/Model/SchemaDefinition/ResolvedDefinitionsCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types = 1);
44

5-
namespace PHPModelGenerator\Model;
5+
namespace PHPModelGenerator\Model\SchemaDefinition;
66

77
use ArrayObject;
88

src/Model/SchemaDefinition.php renamed to src/Model/SchemaDefinition/SchemaDefinition.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
declare(strict_types = 1);
44

5-
namespace PHPModelGenerator\Model;
5+
namespace PHPModelGenerator\Model\SchemaDefinition;
66

77
use PHPModelGenerator\Exception\PHPModelGeneratorException;
88
use PHPModelGenerator\Exception\SchemaException;
99
use PHPModelGenerator\Model\Property\PropertyInterface;
1010
use PHPModelGenerator\Model\Property\PropertyProxy;
11+
use PHPModelGenerator\Model\Schema;
1112
use PHPModelGenerator\PropertyProcessor\PropertyCollectionProcessor;
1213
use PHPModelGenerator\PropertyProcessor\PropertyFactory;
1314
use PHPModelGenerator\PropertyProcessor\PropertyProcessorFactory;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Model\SchemaDefinition;
6+
7+
use ArrayObject;
8+
use PHPModelGenerator\Model\Schema;
9+
use PHPModelGenerator\SchemaProcessor\SchemaProcessor;
10+
11+
/**
12+
* Class SchemaDefinitionDictionary
13+
*
14+
* @package PHPModelGenerator\Model\SchemaDefinition
15+
*/
16+
class SchemaDefinitionDictionary extends ArrayObject
17+
{
18+
/**
19+
* Set up the definition directory for the schema
20+
*
21+
* @param array $propertyData
22+
* @param SchemaProcessor $schemaProcessor
23+
* @param Schema $schema
24+
*/
25+
public function setUpDefinitionDictionary(array $propertyData, SchemaProcessor $schemaProcessor, Schema $schema)
26+
{
27+
foreach ($propertyData as $key => $propertyEntry) {
28+
if (!is_array($propertyEntry)) {
29+
continue;
30+
}
31+
32+
// add the root nodes of the schema to resolve path references
33+
$this->addDefinition($key, new SchemaDefinition($propertyEntry, $schemaProcessor, $schema));
34+
}
35+
36+
$this->fetchDefinitionsById($propertyData, $schemaProcessor, $schema);
37+
}
38+
39+
/**
40+
* Fetch all schema definitions with an ID for direct references
41+
*
42+
* @param array $propertyData
43+
* @param SchemaProcessor $schemaProcessor
44+
* @param Schema $schema
45+
*/
46+
protected function fetchDefinitionsById(array $propertyData, SchemaProcessor $schemaProcessor, Schema $schema)
47+
{
48+
if (isset($propertyData['$id'])) {
49+
$this->addDefinition($propertyData['$id'], new SchemaDefinition($propertyData, $schemaProcessor, $schema));
50+
}
51+
52+
foreach ($propertyData as $item) {
53+
if (!is_array($item)) {
54+
continue;
55+
}
56+
57+
$this->fetchDefinitionsById($item, $schemaProcessor, $schema);
58+
}
59+
}
60+
61+
/**
62+
* Add a partial schema definition to the schema
63+
*
64+
* @param string $key
65+
* @param SchemaDefinition $definition
66+
*
67+
* @return $this
68+
*/
69+
public function addDefinition(string $key, SchemaDefinition $definition): self
70+
{
71+
if (isset($this[$key])) {
72+
return $this;
73+
}
74+
75+
$this[$key] = $definition;
76+
77+
return $this;
78+
}
79+
80+
/**
81+
* @param string $key
82+
*
83+
* @return SchemaDefinition
84+
*/
85+
public function getDefinition(string $key): ?SchemaDefinition
86+
{
87+
return $this[$key] ?? null;
88+
}
89+
}

src/PropertyProcessor/Property/BaseProcessor.php

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PHPModelGenerator\Exception\SchemaException;
99
use PHPModelGenerator\Model\Property\Property;
1010
use PHPModelGenerator\Model\Property\PropertyInterface;
11-
use PHPModelGenerator\Model\SchemaDefinition;
1211
use PHPModelGenerator\Model\Validator;
1312
use PHPModelGenerator\Model\Validator\ComposedPropertyValidator;
1413
use PHPModelGenerator\Model\Validator\PropertyValidator;
@@ -30,10 +29,14 @@ class BaseProcessor extends AbstractPropertyProcessor
3029

3130
/**
3231
* @inheritdoc
32+
*
33+
* @throws SchemaException
3334
*/
3435
public function process(string $propertyName, array $propertyData): PropertyInterface
3536
{
36-
$this->setUpDefinitionDictionary($propertyData);
37+
$this->schema
38+
->getSchemaDictionary()
39+
->setUpDefinitionDictionary($propertyData, $this->schemaProcessor, $this->schema);
3740

3841
// create a property which is used to gather composed properties validators.
3942
$property = new Property($propertyName, static::TYPE);
@@ -112,59 +115,6 @@ protected function addMinPropertiesValidator(array $propertyData): void
112115
);
113116
}
114117

115-
/**
116-
* Set up the definition directory for the schema
117-
*
118-
* @param array $propertyData
119-
*/
120-
protected function setUpDefinitionDictionary(array $propertyData)
121-
{
122-
foreach ($propertyData as $key => $propertyEntry) {
123-
if (!is_array($propertyEntry)) {
124-
continue;
125-
}
126-
127-
// add the root nodes of the schema to resolve path references
128-
$this->schema->addDefinition(
129-
$key,
130-
new SchemaDefinition(
131-
$propertyEntry,
132-
$this->schemaProcessor,
133-
$this->schema
134-
)
135-
);
136-
}
137-
138-
$this->fetchDefinitionsById($propertyData);
139-
}
140-
141-
/**
142-
* Fetch all schema definitions with an ID for direct references
143-
*
144-
* @param array $schema
145-
*/
146-
protected function fetchDefinitionsById(array $schema)
147-
{
148-
if (isset($schema['$id'])) {
149-
$this->schema->addDefinition(
150-
$schema['$id'],
151-
new SchemaDefinition(
152-
$schema,
153-
$this->schemaProcessor,
154-
$this->schema
155-
)
156-
);
157-
}
158-
159-
foreach ($schema as $item) {
160-
if (!is_array($item)) {
161-
continue;
162-
}
163-
164-
$this->fetchDefinitionsById($item);
165-
}
166-
}
167-
168118
/**
169119
* Add the properties defined in the JSON schema to the current schema model
170120
*

src/PropertyProcessor/Property/ObjectProcessor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PHPModelGenerator\PropertyProcessor\Property;
66

7+
use PHPModelGenerator\Exception\SchemaException;
78
use PHPModelGenerator\Model\Property\PropertyInterface;
89
use PHPModelGenerator\PropertyProcessor\Decorator\ObjectInstantiationDecorator;
910

@@ -18,6 +19,8 @@ class ObjectProcessor extends AbstractTypedValueProcessor
1819

1920
/**
2021
* @inheritdoc
22+
*
23+
* @throws SchemaException
2124
*/
2225
public function process(string $propertyName, array $propertyData): PropertyInterface
2326
{
@@ -31,7 +34,7 @@ public function process(string $propertyName, array $propertyData): PropertyInte
3134
$propertyData,
3235
$this->schemaProcessor->getCurrentClassPath(),
3336
$className,
34-
$this->schema->getDefinitions()
37+
$this->schema->getSchemaDictionary()
3538
);
3639

3740
$property

src/PropertyProcessor/Property/ReferenceProcessor.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Exception;
88
use PHPModelGenerator\Exception\SchemaException;
99
use PHPModelGenerator\Model\Property\PropertyInterface;
10-
use PHPModelGenerator\Model\SchemaDefinition;
10+
use PHPModelGenerator\Model\SchemaDefinition\SchemaDefinition;
1111

1212
/**
1313
* Class ConstProcessor
@@ -24,9 +24,10 @@ class ReferenceProcessor extends AbstractTypedValueProcessor
2424
public function process(string $propertyName, array $propertyData): PropertyInterface
2525
{
2626
$reference = $propertyData['$ref'];
27+
$dictionary = $this->schema->getSchemaDictionary();
2728

28-
if ($this->schema->getDefinition($reference)) {
29-
return $this->resolveDefinition($propertyName, $this->schema->getDefinition($reference), $reference);
29+
if ($dictionary->getDefinition($reference)) {
30+
return $this->resolveDefinition($propertyName, $dictionary->getDefinition($reference), $reference);
3031
}
3132

3233
if (strpos($reference, '#') === 0 && strpos($reference, '/')) {
@@ -35,7 +36,7 @@ public function process(string $propertyName, array $propertyData): PropertyInte
3536

3637
return $this->resolveDefinition(
3738
$propertyName,
38-
$this->schema->getDefinition(array_shift($path)),
39+
$dictionary->getDefinition(array_shift($path)),
3940
$reference,
4041
$path
4142
);

0 commit comments

Comments
 (0)