|
1 |
| -<?php |
2 |
| - |
3 |
| -declare(strict_types = 1); |
4 |
| - |
5 |
| -namespace PHPModelGenerator\Model\SchemaDefinition; |
6 |
| - |
7 |
| -use PHPModelGenerator\Exception\PHPModelGeneratorException; |
8 |
| -use PHPModelGenerator\Exception\SchemaException; |
9 |
| -use PHPModelGenerator\Model\Property\PropertyInterface; |
10 |
| -use PHPModelGenerator\Model\Property\PropertyProxy; |
11 |
| -use PHPModelGenerator\Model\Schema; |
12 |
| -use PHPModelGenerator\PropertyProcessor\PropertyCollectionProcessor; |
13 |
| -use PHPModelGenerator\PropertyProcessor\PropertyFactory; |
14 |
| -use PHPModelGenerator\PropertyProcessor\PropertyProcessorFactory; |
15 |
| -use PHPModelGenerator\SchemaProcessor\SchemaProcessor; |
16 |
| - |
17 |
| -/** |
18 |
| - * Class SchemaDefinition |
19 |
| - * |
20 |
| - * Hold a definition from a schema |
21 |
| - * |
22 |
| - * @package PHPModelGenerator\Model |
23 |
| - */ |
24 |
| -class SchemaDefinition |
25 |
| -{ |
26 |
| - /** @var array */ |
27 |
| - protected $structure; |
28 |
| - /** @var SchemaProcessor */ |
29 |
| - protected $schemaProcessor; |
30 |
| - /** @var Schema */ |
31 |
| - protected $schema; |
32 |
| - /** @var ResolvedDefinitionsCollection */ |
33 |
| - protected $resolvedPaths; |
34 |
| - |
35 |
| - /** |
36 |
| - * SchemaDefinition constructor. |
37 |
| - * |
38 |
| - * @param array $structure |
39 |
| - * @param SchemaProcessor $schemaProcessor |
40 |
| - * @param Schema $schema |
41 |
| - */ |
42 |
| - public function __construct(array $structure, SchemaProcessor $schemaProcessor, Schema $schema) |
43 |
| - { |
44 |
| - $this->structure = $structure; |
45 |
| - $this->schemaProcessor = $schemaProcessor; |
46 |
| - $this->schema = $schema; |
47 |
| - |
48 |
| - $this->resolvedPaths = new ResolvedDefinitionsCollection(); |
49 |
| - } |
50 |
| - |
51 |
| - /** |
52 |
| - * Resolve a reference |
53 |
| - * |
54 |
| - * @param string $propertyName |
55 |
| - * @param array $path |
56 |
| - * @param PropertyCollectionProcessor $propertyCollectionProcessor |
57 |
| - * |
58 |
| - * @return PropertyInterface |
59 |
| - * |
60 |
| - * @throws PHPModelGeneratorException |
61 |
| - * @throws SchemaException |
62 |
| - */ |
63 |
| - public function resolveReference( |
64 |
| - string $propertyName, |
65 |
| - array $path, |
66 |
| - PropertyCollectionProcessor $propertyCollectionProcessor |
67 |
| - ): PropertyInterface { |
68 |
| - $structure = $this->structure; |
69 |
| - $originalPath = $path; |
70 |
| - |
71 |
| - while ($segment = array_shift($path)) { |
72 |
| - if (!isset($structure[$segment])) { |
73 |
| - throw new SchemaException("Unresolved path segment: $segment"); |
74 |
| - } |
75 |
| - |
76 |
| - $structure = $structure[$segment]; |
77 |
| - } |
78 |
| - |
79 |
| - $key = implode('-', $originalPath); |
80 |
| - |
81 |
| - if (!$this->resolvedPaths->offsetExists($key)) { |
82 |
| - // create a dummy entry for the path first. If the path is used recursive the recursive usages will point |
83 |
| - // to the currently created property |
84 |
| - $this->resolvedPaths->offsetSet($key, true); |
85 |
| - try { |
86 |
| - $this->resolvedPaths->offsetSet($key, (new PropertyFactory(new PropertyProcessorFactory())) |
87 |
| - ->create( |
88 |
| - $propertyCollectionProcessor, |
89 |
| - $this->schemaProcessor, |
90 |
| - $this->schema, |
91 |
| - $propertyName, |
92 |
| - $structure |
93 |
| - ) |
94 |
| - ); |
95 |
| - } catch (PHPModelGeneratorException $exception) { |
96 |
| - $this->resolvedPaths->offsetUnset($key); |
97 |
| - throw $exception; |
98 |
| - } |
99 |
| - } |
100 |
| - |
101 |
| - return new PropertyProxy($this->resolvedPaths, $key); |
102 |
| - } |
103 |
| -} |
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace PHPModelGenerator\Model\SchemaDefinition; |
| 6 | + |
| 7 | +use PHPModelGenerator\Exception\PHPModelGeneratorException; |
| 8 | +use PHPModelGenerator\Exception\SchemaException; |
| 9 | +use PHPModelGenerator\Model\Property\PropertyInterface; |
| 10 | +use PHPModelGenerator\Model\Property\PropertyProxy; |
| 11 | +use PHPModelGenerator\Model\Schema; |
| 12 | +use PHPModelGenerator\PropertyProcessor\PropertyCollectionProcessor; |
| 13 | +use PHPModelGenerator\PropertyProcessor\PropertyFactory; |
| 14 | +use PHPModelGenerator\PropertyProcessor\PropertyProcessorFactory; |
| 15 | +use PHPModelGenerator\SchemaProcessor\SchemaProcessor; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class SchemaDefinition |
| 19 | + * |
| 20 | + * Hold a definition from a schema |
| 21 | + * |
| 22 | + * @package PHPModelGenerator\Model |
| 23 | + */ |
| 24 | +class SchemaDefinition |
| 25 | +{ |
| 26 | + /** @var array */ |
| 27 | + protected $structure; |
| 28 | + /** @var SchemaProcessor */ |
| 29 | + protected $schemaProcessor; |
| 30 | + /** @var Schema */ |
| 31 | + protected $schema; |
| 32 | + /** @var ResolvedDefinitionsCollection */ |
| 33 | + protected $resolvedPaths; |
| 34 | + |
| 35 | + /** |
| 36 | + * SchemaDefinition constructor. |
| 37 | + * |
| 38 | + * @param array $structure |
| 39 | + * @param SchemaProcessor $schemaProcessor |
| 40 | + * @param Schema $schema |
| 41 | + */ |
| 42 | + public function __construct(array $structure, SchemaProcessor $schemaProcessor, Schema $schema) |
| 43 | + { |
| 44 | + $this->structure = $structure; |
| 45 | + $this->schemaProcessor = $schemaProcessor; |
| 46 | + $this->schema = $schema; |
| 47 | + |
| 48 | + $this->resolvedPaths = new ResolvedDefinitionsCollection(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @return Schema |
| 53 | + */ |
| 54 | + public function getSchema(): Schema |
| 55 | + { |
| 56 | + return $this->schema; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Resolve a reference |
| 61 | + * |
| 62 | + * @param string $propertyName |
| 63 | + * @param array $path |
| 64 | + * @param PropertyCollectionProcessor $propertyCollectionProcessor |
| 65 | + * |
| 66 | + * @return PropertyInterface |
| 67 | + * |
| 68 | + * @throws PHPModelGeneratorException |
| 69 | + * @throws SchemaException |
| 70 | + */ |
| 71 | + public function resolveReference( |
| 72 | + string $propertyName, |
| 73 | + array $path, |
| 74 | + PropertyCollectionProcessor $propertyCollectionProcessor |
| 75 | + ): PropertyInterface { |
| 76 | + $structure = $this->structure; |
| 77 | + $originalPath = $path; |
| 78 | + |
| 79 | + while ($segment = array_shift($path)) { |
| 80 | + if (!isset($structure[$segment])) { |
| 81 | + throw new SchemaException("Unresolved path segment: $segment"); |
| 82 | + } |
| 83 | + |
| 84 | + $structure = $structure[$segment]; |
| 85 | + } |
| 86 | + |
| 87 | + $key = implode('-', $originalPath); |
| 88 | + |
| 89 | + if (!$this->resolvedPaths->offsetExists($key)) { |
| 90 | + // create a dummy entry for the path first. If the path is used recursive the recursive usages will point |
| 91 | + // to the currently created property |
| 92 | + $this->resolvedPaths->offsetSet($key, true); |
| 93 | + try { |
| 94 | + $this->resolvedPaths->offsetSet($key, (new PropertyFactory(new PropertyProcessorFactory())) |
| 95 | + ->create( |
| 96 | + $propertyCollectionProcessor, |
| 97 | + $this->schemaProcessor, |
| 98 | + $this->schema, |
| 99 | + $propertyName, |
| 100 | + $structure |
| 101 | + ) |
| 102 | + ); |
| 103 | + } catch (PHPModelGeneratorException $exception) { |
| 104 | + $this->resolvedPaths->offsetUnset($key); |
| 105 | + throw $exception; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return new PropertyProxy($this->resolvedPaths, $key); |
| 110 | + } |
| 111 | +} |
0 commit comments