|
4 | 4 |
|
5 | 5 | namespace PHPModelGenerator\SchemaProcessor\PostProcessor;
|
6 | 6 |
|
7 |
| -use Exception; |
8 | 7 | use PHPModelGenerator\Exception\Object\MinPropertiesException;
|
9 | 8 | use PHPModelGenerator\Exception\Object\RegularPropertyAsAdditionalPropertyException;
|
10 | 9 | use PHPModelGenerator\Exception\SchemaException;
|
11 |
| -use PHPModelGenerator\Filter\TransformingFilterInterface; |
12 | 10 | use PHPModelGenerator\Model\GeneratorConfiguration;
|
13 | 11 | use PHPModelGenerator\Model\Property\Property;
|
14 | 12 | use PHPModelGenerator\Model\Property\PropertyInterface;
|
15 | 13 | use PHPModelGenerator\Model\Property\PropertyType;
|
16 | 14 | use PHPModelGenerator\Model\Schema;
|
17 |
| -use PHPModelGenerator\Model\SchemaDefinition\JsonSchema; |
18 |
| -use PHPModelGenerator\Model\SerializedValue; |
19 | 15 | use PHPModelGenerator\Model\Validator\AdditionalPropertiesValidator;
|
20 |
| -use PHPModelGenerator\Model\Validator\FilterValidator; |
21 |
| -use PHPModelGenerator\Model\Validator\PropertyTemplateValidator; |
22 | 16 | use PHPModelGenerator\Model\Validator\PropertyValidator;
|
23 | 17 | use PHPModelGenerator\PropertyProcessor\Decorator\TypeHint\ArrayTypeHintDecorator;
|
24 | 18 | use PHPModelGenerator\PropertyProcessor\Decorator\TypeHint\TypeHintDecorator;
|
25 | 19 | use PHPModelGenerator\SchemaProcessor\Hook\SchemaHookResolver;
|
| 20 | +use PHPModelGenerator\SchemaProcessor\PostProcessor\Internal\AdditionalPropertiesPostProcessor; |
| 21 | +use PHPModelGenerator\SchemaProcessor\PostProcessor\Internal\SerializationPostProcessor; |
26 | 22 | use PHPModelGenerator\Utils\RenderHelper;
|
27 | 23 |
|
28 | 24 | /**
|
@@ -70,102 +66,28 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu
|
70 | 66 | $validationProperty = null;
|
71 | 67 | foreach ($schema->getBaseValidators() as $validator) {
|
72 | 68 | if (is_a($validator, AdditionalPropertiesValidator::class)) {
|
73 |
| - $validator->setCollectAdditionalProperties(true); |
74 | 69 | $validationProperty = $validator->getValidationProperty();
|
75 | 70 | }
|
76 | 71 | }
|
77 | 72 |
|
78 |
| - $this->addAdditionalPropertiesCollectionProperty($schema, $validationProperty); |
79 |
| - $this->addGetAdditionalPropertyMethod($schema, $generatorConfiguration, $validationProperty); |
80 |
| - |
81 |
| - if ($generatorConfiguration->hasSerializationEnabled()) { |
82 |
| - $this->addSerializeAdditionalPropertiesMethod($schema, $generatorConfiguration, $validationProperty); |
| 73 | + // check if basic code must be added |
| 74 | + if ($this->addForModelsWithoutAdditionalPropertiesDefinition && !isset($json['additionalProperties'])) { |
| 75 | + (new AdditionalPropertiesPostProcessor())->addAdditionalPropertiesCollectionProperty($schema); |
| 76 | + } |
| 77 | + if ($generatorConfiguration->hasSerializationEnabled() && |
| 78 | + $this->addForModelsWithoutAdditionalPropertiesDefinition && |
| 79 | + !isset($json['additionalProperties']) |
| 80 | + ) { |
| 81 | + (new SerializationPostProcessor())->addAdditionalPropertiesSerialization($schema, $generatorConfiguration); |
83 | 82 | }
|
84 | 83 |
|
| 84 | + $this->addGetAdditionalPropertiesMethod($schema, $generatorConfiguration, $validationProperty); |
| 85 | + $this->addGetAdditionalPropertyMethod($schema, $generatorConfiguration, $validationProperty); |
| 86 | + |
85 | 87 | if (!$generatorConfiguration->isImmutable()) {
|
86 | 88 | $this->addSetAdditionalPropertyMethod($schema, $generatorConfiguration, $validationProperty);
|
87 | 89 | $this->addRemoveAdditionalPropertyMethod($schema, $generatorConfiguration);
|
88 | 90 | }
|
89 |
| - |
90 |
| - if (!isset($json['additionalProperties']) || $json['additionalProperties'] === true) { |
91 |
| - $this->addUpdateAdditionalProperties($schema); |
92 |
| - } |
93 |
| - } |
94 |
| - |
95 |
| - /** |
96 |
| - * Adds an array property to the schema which holds all additional properties |
97 |
| - * |
98 |
| - * @param Schema $schema |
99 |
| - * @param PropertyInterface|null $validationProperty |
100 |
| - * |
101 |
| - * @throws SchemaException |
102 |
| - */ |
103 |
| - private function addAdditionalPropertiesCollectionProperty( |
104 |
| - Schema $schema, |
105 |
| - ?PropertyInterface $validationProperty |
106 |
| - ): void { |
107 |
| - $additionalPropertiesCollectionProperty = (new Property( |
108 |
| - 'additionalProperties', |
109 |
| - new PropertyType('array'), |
110 |
| - new JsonSchema(__FILE__, []), |
111 |
| - 'Collect all additional properties provided to the schema' |
112 |
| - )) |
113 |
| - ->setDefaultValue([]) |
114 |
| - ->setReadOnly(true); |
115 |
| - |
116 |
| - if ($validationProperty) { |
117 |
| - $additionalPropertiesCollectionProperty->addTypeHintDecorator( |
118 |
| - new ArrayTypeHintDecorator($validationProperty) |
119 |
| - ); |
120 |
| - } |
121 |
| - |
122 |
| - $schema->addProperty($additionalPropertiesCollectionProperty); |
123 |
| - } |
124 |
| - |
125 |
| - /** |
126 |
| - * Adds a custom serialization function to the schema to merge all additional properties into the serialization |
127 |
| - * result on serializations |
128 |
| - * |
129 |
| - * @param Schema $schema |
130 |
| - * @param GeneratorConfiguration $generatorConfiguration |
131 |
| - * @param PropertyInterface|null $validationProperty |
132 |
| - */ |
133 |
| - private function addSerializeAdditionalPropertiesMethod( |
134 |
| - Schema $schema, |
135 |
| - GeneratorConfiguration $generatorConfiguration, |
136 |
| - ?PropertyInterface $validationProperty |
137 |
| - ): void { |
138 |
| - $transformingFilterValidator = null; |
139 |
| - |
140 |
| - if ($validationProperty) { |
141 |
| - foreach ($validationProperty->getValidators() as $validator) { |
142 |
| - $validator = $validator->getValidator(); |
143 |
| - |
144 |
| - if ($validator instanceof FilterValidator && |
145 |
| - $validator->getFilter() instanceof TransformingFilterInterface |
146 |
| - ) { |
147 |
| - $transformingFilterValidator = $validator; |
148 |
| - [$serializerClass, $serializerMethod] = $validator->getFilter()->getSerializer(); |
149 |
| - } |
150 |
| - } |
151 |
| - } |
152 |
| - |
153 |
| - $schema->addUsedClass(SerializedValue::class); |
154 |
| - $schema->addMethod( |
155 |
| - 'serializeAdditionalProperties', |
156 |
| - new RenderedMethod( |
157 |
| - $schema, |
158 |
| - $generatorConfiguration, |
159 |
| - 'AdditionalProperties/AdditionalPropertiesSerializer.phptpl', |
160 |
| - [ |
161 |
| - 'serializerClass' => $serializerClass ?? null, |
162 |
| - 'serializerMethod' => $serializerMethod ?? null, |
163 |
| - 'serializerOptions' => $transformingFilterValidator |
164 |
| - ? var_export($transformingFilterValidator->getFilterOptions(), true) |
165 |
| - : [], |
166 |
| - ] |
167 |
| - ) |
168 |
| - ); |
169 | 91 | }
|
170 | 92 |
|
171 | 93 | /**
|
@@ -277,47 +199,36 @@ private function addGetAdditionalPropertyMethod(
|
277 | 199 | );
|
278 | 200 | }
|
279 | 201 |
|
280 |
| - /** |
281 |
| - * Usually the AdditionalPropertiesValidator validates all additional properties against the constraints and updates |
282 |
| - * the internal storage of the additional properties. If no additional property constraints are defined for the |
283 |
| - * schema the provided additional properties must be updated separately as no AdditionalPropertiesValidator is added |
284 |
| - * to the generated class. |
285 |
| - * |
286 |
| - * @param Schema $schema |
287 |
| - */ |
288 |
| - private function addUpdateAdditionalProperties(Schema $schema): void |
289 |
| - { |
290 |
| - $schema->addBaseValidator( |
291 |
| - new class ($schema) extends PropertyTemplateValidator { |
292 |
| - public function __construct(Schema $schema) |
293 |
| - { |
294 |
| - $patternProperties = array_keys($schema->getJsonSchema()->getJson()['patternProperties'] ?? []); |
| 202 | + private function addGetAdditionalPropertiesMethod( |
| 203 | + Schema $schema, |
| 204 | + GeneratorConfiguration $generatorConfiguration, |
| 205 | + ?PropertyInterface $validationProperty |
| 206 | + ): void { |
| 207 | + $validationProperty = $validationProperty |
| 208 | + // type hint always without null as the getter always returns an array |
| 209 | + ? (clone $validationProperty) |
| 210 | + ->setRequired(true) |
| 211 | + ->addTypeHintDecorator(new ArrayTypeHintDecorator($validationProperty)) |
| 212 | + : null; |
| 213 | + |
| 214 | + if ($validationProperty && $validationProperty->getType(true)) { |
| 215 | + $validationProperty->setType( |
| 216 | + $validationProperty->getType(), |
| 217 | + new PropertyType($validationProperty->getType(true)->getName(), false) |
| 218 | + ); |
| 219 | + } |
295 | 220 |
|
296 |
| - parent::__construct( |
297 |
| - new Property($schema->getClassName(), null, $schema->getJsonSchema()), |
298 |
| - join( |
299 |
| - DIRECTORY_SEPARATOR, |
300 |
| - [ |
301 |
| - '..', |
302 |
| - 'SchemaProcessor', |
303 |
| - 'PostProcessor', |
304 |
| - 'Templates', |
305 |
| - 'AdditionalProperties', |
306 |
| - 'UpdateAdditionalProperties.phptpl', |
307 |
| - ] |
308 |
| - ), |
309 |
| - [ |
310 |
| - 'patternProperties' => $patternProperties |
311 |
| - ? RenderHelper::varExportArray($patternProperties) |
312 |
| - : null, |
313 |
| - 'additionalProperties' => RenderHelper::varExportArray( |
314 |
| - array_keys($schema->getJsonSchema()->getJson()['properties'] ?? []) |
315 |
| - ), |
316 |
| - ], |
317 |
| - Exception::class |
318 |
| - ); |
319 |
| - } |
320 |
| - } |
| 221 | + $schema->addMethod( |
| 222 | + 'getAdditionalProperties', |
| 223 | + new RenderedMethod( |
| 224 | + $schema, |
| 225 | + $generatorConfiguration, |
| 226 | + 'AdditionalProperties/GetAdditionalProperties.phptpl', |
| 227 | + [ |
| 228 | + 'validationProperty' => $validationProperty, |
| 229 | + |
| 230 | + ] |
| 231 | + ) |
321 | 232 | );
|
322 | 233 | }
|
323 | 234 | }
|
0 commit comments