Skip to content

Commit d88f8b9

Browse files
committed
Instead of passing arrays with the currently processed schema around wrap the schema into a JsonSchema object to provide additional meta data.
The JsonSchema object is additionally associated with Schema and Property objects to determine the scope of the generated code later (eg. in Post-Processors or to attach debug information to exceptions).
1 parent e7218a7 commit d88f8b9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+553
-326
lines changed

src/Model/Property/Property.php

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

77
use PHPModelGenerator\Exception\SchemaException;
88
use PHPModelGenerator\Model\Schema;
9+
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
10+
use PHPModelGenerator\Model\SchemaDefinition\JsonSchemaTrait;
911
use PHPModelGenerator\Model\Validator;
1012
use PHPModelGenerator\Model\Validator\PropertyValidatorInterface;
1113
use PHPModelGenerator\PropertyProcessor\Decorator\Property\PropertyDecoratorInterface;
@@ -18,6 +20,8 @@
1820
*/
1921
class Property implements PropertyInterface
2022
{
23+
use JsonSchemaTrait;
24+
2125
/** @var string */
2226
protected $name = '';
2327
/** @var string */
@@ -49,15 +53,17 @@ class Property implements PropertyInterface
4953
*
5054
* @param string $name
5155
* @param string $type
56+
* @param JsonSchema $jsonSchema
5257
* @param string $description
5358
*
5459
* @throws SchemaException
5560
*/
56-
public function __construct(string $name, string $type, string $description = '')
61+
public function __construct(string $name, string $type, JsonSchema $jsonSchema, string $description = '')
5762
{
5863
$this->attribute = $this->processAttributeName($name);
5964
$this->name = $name;
6065
$this->type = $type;
66+
$this->jsonSchema = $jsonSchema;
6167
$this->description = $description;
6268
}
6369

src/Model/Property/PropertyInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PHPModelGenerator\Model\Property;
66

77
use PHPModelGenerator\Model\Schema;
8+
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
89
use PHPModelGenerator\Model\Validator;
910
use PHPModelGenerator\Model\Validator\PropertyValidatorInterface;
1011
use PHPModelGenerator\PropertyProcessor\Decorator\Property\PropertyDecoratorInterface;
@@ -179,4 +180,11 @@ public function setNestedSchema(Schema $schema);
179180
* @return null|Schema
180181
*/
181182
public function getNestedSchema(): ?Schema;
183+
184+
/**
185+
* Get the JSON schema used to set up the property
186+
*
187+
* @return JsonSchema
188+
*/
189+
public function getJsonSchema(): JsonSchema;
182190
}

src/Model/Property/PropertyProxy.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PHPModelGenerator\Model\Property;
66

7+
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
78
use PHPModelGenerator\Model\SchemaDefinition\ResolvedDefinitionsCollection;
89
use PHPModelGenerator\Model\Schema;
910
use PHPModelGenerator\Model\Validator\PropertyValidatorInterface;
@@ -219,4 +220,12 @@ public function getNestedSchema(): ?Schema
219220
{
220221
return $this->getProperty()->getNestedSchema();
221222
}
223+
224+
/**
225+
* @inheritdoc
226+
*/
227+
public function getJsonSchema(): JsonSchema
228+
{
229+
return $this->getProperty()->getJsonSchema();
230+
}
222231
}

src/Model/Schema.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PHPModelGenerator\Interfaces\JSONModelInterface;
88
use PHPModelGenerator\Model\Property\PropertyInterface;
9+
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
10+
use PHPModelGenerator\Model\SchemaDefinition\JsonSchemaTrait;
911
use PHPModelGenerator\Model\SchemaDefinition\SchemaDefinitionDictionary;
1012
use PHPModelGenerator\Model\Validator\PropertyValidatorInterface;
1113
use PHPModelGenerator\Model\Validator\SchemaDependencyValidator;
@@ -18,6 +20,8 @@
1820
*/
1921
class Schema
2022
{
23+
use JsonSchemaTrait;
24+
2125
/** @var string */
2226
protected $className;
2327
/** @var string */
@@ -47,14 +51,20 @@ class Schema
4751
/**
4852
* Schema constructor.
4953
*
50-
* @param string $classPath
51-
* @param string $className
54+
* @param string $classPath
55+
* @param string $className
56+
* @param JsonSchema|null $schema
5257
* @param SchemaDefinitionDictionary|null $dictionary
5358
*/
54-
public function __construct(string $classPath, string $className, SchemaDefinitionDictionary $dictionary = null)
55-
{
59+
public function __construct(
60+
string $classPath,
61+
string $className,
62+
JsonSchema $schema = null,
63+
SchemaDefinitionDictionary $dictionary = null
64+
) {
5665
$this->className = $className;
5766
$this->classPath = $classPath;
67+
$this->jsonSchema = $schema;
5868
$this->schemaDefinitionDictionary = $dictionary ?? new SchemaDefinitionDictionary('');
5969

6070
$this->addInterface(JSONModelInterface::class);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Model\SchemaDefinition;
6+
7+
/**
8+
* Class JsonSchema
9+
*
10+
* @package PHPModelGenerator\Model\SchemaDefinition
11+
*/
12+
class JsonSchema
13+
{
14+
/** @var array */
15+
protected $json;
16+
/** @var string */
17+
private $file;
18+
19+
/**
20+
* JsonSchema constructor.
21+
*
22+
* @param string $file the source file for the schema
23+
* @param array $json Decoded json schema
24+
*/
25+
public function __construct(string $file, array $json)
26+
{
27+
$this->json = $json;
28+
$this->file = $file;
29+
}
30+
31+
/**
32+
* @return array
33+
*/
34+
public function getJson(): array
35+
{
36+
return $this->json;
37+
}
38+
39+
/**
40+
* @param array $json
41+
*
42+
* @return JsonSchema
43+
*/
44+
public function withJson(array $json): JsonSchema
45+
{
46+
$jsonSchema = clone $this;
47+
$jsonSchema->json = $json;
48+
49+
return $jsonSchema;
50+
}
51+
52+
/**
53+
* @return string
54+
*/
55+
public function getFile(): string
56+
{
57+
return $this->file;
58+
}
59+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Model\SchemaDefinition;
6+
7+
/**
8+
* Trait JsonSchemaTrait
9+
*
10+
* @package PHPModelGenerator\Model\SchemaDefinition
11+
*/
12+
trait JsonSchemaTrait
13+
{
14+
/** @var JsonSchema */
15+
protected $jsonSchema;
16+
17+
/**
18+
* Get the JSON schema structure
19+
*
20+
* @return JsonSchema
21+
*/
22+
public function getJsonSchema(): JsonSchema
23+
{
24+
return $this->jsonSchema;
25+
}
26+
}

src/Model/SchemaDefinition/SchemaDefinition.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
*/
2424
class SchemaDefinition
2525
{
26-
/** @var array */
27-
protected $structure;
26+
/** @var JsonSchema */
27+
protected $source;
2828
/** @var SchemaProcessor */
2929
protected $schemaProcessor;
3030
/** @var Schema */
@@ -35,13 +35,13 @@ class SchemaDefinition
3535
/**
3636
* SchemaDefinition constructor.
3737
*
38-
* @param array $structure
38+
* @param JsonSchema $jsonSchema
3939
* @param SchemaProcessor $schemaProcessor
40-
* @param Schema $schema
40+
* @param Schema $schema
4141
*/
42-
public function __construct(array $structure, SchemaProcessor $schemaProcessor, Schema $schema)
42+
public function __construct(JsonSchema $jsonSchema, SchemaProcessor $schemaProcessor, Schema $schema)
4343
{
44-
$this->structure = $structure;
44+
$this->source = $jsonSchema;
4545
$this->schemaProcessor = $schemaProcessor;
4646
$this->schema = $schema;
4747

@@ -73,15 +73,15 @@ public function resolveReference(
7373
array $path,
7474
PropertyMetaDataCollection $propertyMetaDataCollection
7575
): PropertyInterface {
76-
$structure = $this->structure;
76+
$jsonSchema = $this->source->getJson();
7777
$originalPath = $path;
7878

7979
while ($segment = array_shift($path)) {
80-
if (!isset($structure[$segment])) {
80+
if (!isset($jsonSchema[$segment])) {
8181
throw new SchemaException("Unresolved path segment: $segment");
8282
}
8383

84-
$structure = $structure[$segment];
84+
$jsonSchema = $jsonSchema[$segment];
8585
}
8686

8787
$key = implode('-', $originalPath);
@@ -98,7 +98,7 @@ public function resolveReference(
9898
$this->schemaProcessor,
9999
$this->schema,
100100
$propertyName,
101-
$structure
101+
$this->source->withJson($jsonSchema)
102102
)
103103
);
104104
} catch (PHPModelGeneratorException $exception) {

src/Model/SchemaDefinition/SchemaDefinitionDictionary.php

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,52 +36,55 @@ public function __construct(string $sourceDirectory)
3636
/**
3737
* Set up the definition directory for the schema
3838
*
39-
* @param array $propertyData
4039
* @param SchemaProcessor $schemaProcessor
41-
* @param Schema $schema
40+
* @param Schema $schema
4241
*/
43-
public function setUpDefinitionDictionary(
44-
array $propertyData,
45-
SchemaProcessor $schemaProcessor,
46-
Schema $schema
47-
): void {
42+
public function setUpDefinitionDictionary(SchemaProcessor $schemaProcessor, Schema $schema): void {
4843
// attach the root node to the definition dictionary
49-
$this->addDefinition('#', new SchemaDefinition($propertyData, $schemaProcessor, $schema));
44+
$this->addDefinition('#', new SchemaDefinition($schema->getJsonSchema(), $schemaProcessor, $schema));
5045

51-
foreach ($propertyData as $key => $propertyEntry) {
46+
foreach ($schema->getJsonSchema()->getJson() as $key => $propertyEntry) {
5247
if (!is_array($propertyEntry)) {
5348
continue;
5449
}
5550

5651
// add the root nodes of the schema to resolve path references
57-
$this->addDefinition($key, new SchemaDefinition($propertyEntry, $schemaProcessor, $schema));
52+
$this->addDefinition(
53+
$key,
54+
new SchemaDefinition($schema->getJsonSchema()->withJson($propertyEntry), $schemaProcessor, $schema)
55+
);
5856
}
5957

60-
$this->fetchDefinitionsById($propertyData, $schemaProcessor, $schema);
58+
$this->fetchDefinitionsById($schema->getJsonSchema(), $schemaProcessor, $schema);
6159
}
6260

6361
/**
6462
* Fetch all schema definitions with an ID for direct references
6563
*
66-
* @param array $propertyData
64+
* @param JsonSchema $jsonSchema
6765
* @param SchemaProcessor $schemaProcessor
6866
* @param Schema $schema
6967
*/
70-
protected function fetchDefinitionsById(array $propertyData, SchemaProcessor $schemaProcessor, Schema $schema): void
71-
{
72-
if (isset($propertyData['$id'])) {
68+
protected function fetchDefinitionsById(
69+
JsonSchema $jsonSchema,
70+
SchemaProcessor $schemaProcessor,
71+
Schema $schema
72+
): void {
73+
$json = $jsonSchema->getJson();
74+
75+
if (isset($json['$id'])) {
7376
$this->addDefinition(
74-
strpos($propertyData['$id'], '#') === 0 ? $propertyData['$id'] : "#{$propertyData['$id']}",
75-
new SchemaDefinition($propertyData, $schemaProcessor, $schema)
77+
strpos($json['$id'], '#') === 0 ? $json['$id'] : "#{$json['$id']}",
78+
new SchemaDefinition($jsonSchema, $schemaProcessor, $schema)
7679
);
7780
}
7881

79-
foreach ($propertyData as $item) {
82+
foreach ($json as $item) {
8083
if (!is_array($item)) {
8184
continue;
8285
}
8386

84-
$this->fetchDefinitionsById($item, $schemaProcessor, $schema);
87+
$this->fetchDefinitionsById($jsonSchema->withJson($item), $schemaProcessor, $schema);
8588
}
8689
}
8790

@@ -176,8 +179,14 @@ protected function parseExternalFile(
176179
}
177180

178181
// set up a dummy schema to fetch the definitions from the external file
179-
$schema = new Schema('ExternalSchema_' . uniqid(), '', new self(dirname($jsonSchemaFilePath)));
180-
$schema->getSchemaDictionary()->setUpDefinitionDictionary($decodedJsonSchema, $schemaProcessor, $schema);
182+
$schema = new Schema(
183+
'ExternalSchema_' . uniqid(),
184+
'',
185+
new JsonSchema($jsonSchemaFilePath, $decodedJsonSchema),
186+
new self(dirname($jsonSchemaFilePath))
187+
);
188+
189+
$schema->getSchemaDictionary()->setUpDefinitionDictionary($schemaProcessor, $schema);
181190
$this->parsedExternalFileSchemas[$jsonSchemaFile] = $schema;
182191

183192
return $schema->getSchemaDictionary()->getDefinition($externalKey, $schemaProcessor, $path);

src/Model/Validator/AdditionalItemsValidator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPModelGenerator\Exception\Arrays\InvalidAdditionalTupleItemsException;
88
use PHPModelGenerator\Exception\SchemaException;
99
use PHPModelGenerator\Model\Schema;
10+
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
1011
use PHPModelGenerator\SchemaProcessor\SchemaProcessor;
1112

1213
/**
@@ -28,15 +29,15 @@ class AdditionalItemsValidator extends AdditionalPropertiesValidator
2829
*
2930
* @param SchemaProcessor $schemaProcessor
3031
* @param Schema $schema
31-
* @param array $propertiesStructure
32+
* @param JsonSchema $propertiesStructure
3233
* @param string $propertyName
3334
*
3435
* @throws SchemaException
3536
*/
3637
public function __construct(
3738
SchemaProcessor $schemaProcessor,
3839
Schema $schema,
39-
array $propertiesStructure,
40+
JsonSchema $propertiesStructure,
4041
string $propertyName
4142
) {
4243
parent::__construct($schemaProcessor, $schema, $propertiesStructure, $propertyName);

0 commit comments

Comments
 (0)