Skip to content

Commit c0dc589

Browse files
committed
Implement schema intern references via ID
1 parent 67a951f commit c0dc589

15 files changed

+254
-74
lines changed

src/Model/SchemaDefinition.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ public function resolveReference(
9797
}
9898
}
9999

100-
101100
return new PropertyProxy($this->resolvedPaths, $key);
102101
}
103102
}

src/PropertyProcessor/ComposedValue/AbstractComposedValueProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,5 @@ protected function generateValidators(PropertyInterface $property, array $proper
7676
*
7777
* @return string
7878
*/
79-
abstract function getComposedValueValidation(int $composedElements): string;
79+
abstract protected function getComposedValueValidation(int $composedElements): string;
8080
}

src/PropertyProcessor/ComposedValue/AllOfProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AllOfProcessor extends AbstractComposedPropertiesProcessor
1212
/**
1313
* @inheritdoc
1414
*/
15-
function getComposedValueValidation(int $composedElements): string
15+
protected function getComposedValueValidation(int $composedElements): string
1616
{
1717
return "\$succeededCompositionElements === $composedElements";
1818
}

src/PropertyProcessor/ComposedValue/AnyOfProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AnyOfProcessor extends AbstractComposedPropertiesProcessor
1212
/**
1313
* @inheritdoc
1414
*/
15-
function getComposedValueValidation(int $composedElements): string
15+
protected function getComposedValueValidation(int $composedElements): string
1616
{
1717
return "\$succeededCompositionElements > 0";
1818
}

src/PropertyProcessor/ComposedValue/NotProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected function generateValidators(PropertyInterface $property, array $proper
2626
/**
2727
* @inheritdoc
2828
*/
29-
function getComposedValueValidation(int $composedElements): string
29+
protected function getComposedValueValidation(int $composedElements): string
3030
{
3131
return '$succeededCompositionElements === 0';
3232
}

src/PropertyProcessor/ComposedValue/OneOfProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class OneOfProcessor extends AbstractComposedPropertiesProcessor
1212
/**
1313
* @inheritdoc
1414
*/
15-
function getComposedValueValidation(int $composedElements): string
15+
protected function getComposedValueValidation(int $composedElements): string
1616
{
1717
return '$succeededCompositionElements === 1';
1818
}

src/PropertyProcessor/Property/ArrayProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private function addUniqueItemsValidation(PropertyInterface $property, array $pr
100100
* Add the validator to check if the items inside an array are unique
101101
*
102102
* @param PropertyInterface $property
103-
* @param array $propertyData
103+
* @param array $propertyData
104104
*
105105
* @throws SchemaException
106106
*/

src/PropertyProcessor/Property/BaseProcessor.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ protected function setUpDefinitionDictionary(array $propertyData)
124124
continue;
125125
}
126126

127+
// add the root nodes of the schema to resolve path references
127128
$this->schema->addDefinition(
128129
$key,
129130
new SchemaDefinition(
@@ -133,6 +134,35 @@ protected function setUpDefinitionDictionary(array $propertyData)
133134
)
134135
);
135136
}
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+
}
136166
}
137167

138168
/**

src/PropertyProcessor/Property/ReferenceProcessor.php

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

1112
/**
1213
* Class ConstProcessor
@@ -17,26 +18,58 @@ class ReferenceProcessor extends AbstractTypedValueProcessor
1718
{
1819
/**
1920
* @inheritdoc
21+
*
22+
* @throws SchemaException
2023
*/
2124
public function process(string $propertyName, array $propertyData): PropertyInterface
2225
{
23-
if (strpos($propertyData['$ref'], '#') === 0) {
24-
$path = explode('/', $propertyData['$ref']);
26+
$reference = $propertyData['$ref'];
27+
28+
if ($this->schema->getDefinition($reference)) {
29+
return $this->resolveDefinition($propertyName, $this->schema->getDefinition($reference), $reference);
30+
}
31+
32+
if (strpos($reference, '#') === 0 && strpos($reference, '/')) {
33+
$path = explode('/', $reference);
2534
array_shift($path);
2635

27-
$definition = $this->schema->getDefinition(array_shift($path));
36+
return $this->resolveDefinition(
37+
$propertyName,
38+
$this->schema->getDefinition(array_shift($path)),
39+
$reference,
40+
$path
41+
);
42+
}
2843

29-
if ($definition) {
30-
try {
31-
return $definition->resolveReference($propertyName, $path, $this->propertyCollectionProcessor);
32-
} catch (Exception $exception) {
33-
throw new SchemaException("Unresolved Reference: {$propertyData['$ref']}", 0, $exception);
34-
}
35-
}
44+
throw new SchemaException("Unresolved Reference: $reference");
45+
}
3646

37-
throw new SchemaException("Unresolved Reference: {$propertyData['$ref']}");
47+
/**
48+
* Resolve a given definition into a Property
49+
*
50+
* @param string $propertyName
51+
* @param SchemaDefinition|null $definition
52+
* @param string $reference
53+
* @param array $path
54+
*
55+
* @return PropertyInterface
56+
*
57+
* @throws SchemaException
58+
*/
59+
protected function resolveDefinition(
60+
string $propertyName,
61+
?SchemaDefinition $definition,
62+
string $reference,
63+
array $path = []
64+
): PropertyInterface {
65+
if ($definition) {
66+
try {
67+
return $definition->resolveReference($propertyName, $path, $this->propertyCollectionProcessor);
68+
} catch (Exception $exception) {
69+
throw new SchemaException("Unresolved Reference: $reference", 0, $exception);
70+
}
3871
}
3972

40-
throw new SchemaException("Unsupported Reference: {$propertyData['$ref']}");
73+
throw new SchemaException("Unresolved Reference: $reference");
4174
}
4275
}

tests/AbstractPHPModelGeneratorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ public function generateObjectFromFile(string $file, GeneratorConfiguration $gen
104104
* @param bool $escape
105105
*
106106
* @return string
107+
*
108+
* @throws FileSystemException
109+
* @throws RenderException
110+
* @throws SchemaException
107111
*/
108112
public function generateObjectFromFileTemplate(
109113
string $file,

0 commit comments

Comments
 (0)