|
8 | 8 | use PHPModelGenerator\Filter\TransformingFilterInterface; |
9 | 9 | use PHPModelGenerator\Interfaces\SerializationInterface; |
10 | 10 | use PHPModelGenerator\Model\GeneratorConfiguration; |
| 11 | +use PHPModelGenerator\Model\Property\Property; |
| 12 | +use PHPModelGenerator\Model\Property\PropertyInterface; |
| 13 | +use PHPModelGenerator\Model\Property\PropertyType; |
11 | 14 | use PHPModelGenerator\Model\Schema; |
| 15 | +use PHPModelGenerator\Model\SchemaDefinition\JsonSchema; |
12 | 16 | use PHPModelGenerator\Model\Validator\AdditionalPropertiesValidator; |
13 | 17 | use PHPModelGenerator\Model\Validator\FilterValidator; |
14 | 18 | use PHPModelGenerator\Model\Validator\PatternPropertiesValidator; |
@@ -40,6 +44,7 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu |
40 | 44 |
|
41 | 45 | $this->addSerializeFunctionsForTransformingFilters($schema, $generatorConfiguration); |
42 | 46 | $this->addSerializationHookMethod($schema, $generatorConfiguration); |
| 47 | + $this->addSkipNotProvidedPropertiesMap($schema, $generatorConfiguration); |
43 | 48 |
|
44 | 49 | $this->addPatternPropertiesSerialization($schema, $generatorConfiguration); |
45 | 50 |
|
@@ -229,4 +234,36 @@ public function getCode(): string |
229 | 234 | } |
230 | 235 | ); |
231 | 236 | } |
| 237 | + |
| 238 | + private function addSkipNotProvidedPropertiesMap( |
| 239 | + Schema $schema, |
| 240 | + GeneratorConfiguration $generatorConfiguration |
| 241 | + ): void { |
| 242 | + if ($generatorConfiguration->isImplicitNullAllowed()) { |
| 243 | + return; |
| 244 | + } |
| 245 | + |
| 246 | + $skipNotProvidedValues = array_map( |
| 247 | + static function (PropertyInterface $property): string { |
| 248 | + return $property->getAttribute(true); |
| 249 | + }, |
| 250 | + array_filter( |
| 251 | + $schema->getProperties(), |
| 252 | + static function (PropertyInterface $property): bool { |
| 253 | + return !$property->isRequired() && !$property->getDefaultValue(); |
| 254 | + } |
| 255 | + ) |
| 256 | + ); |
| 257 | + |
| 258 | + $schema->addProperty( |
| 259 | + (new Property( |
| 260 | + 'skipNotProvidedPropertiesMap', |
| 261 | + new PropertyType('array'), |
| 262 | + new JsonSchema(__FILE__, []), |
| 263 | + 'Values which might be skipped for serialization if not provided' |
| 264 | + )) |
| 265 | + ->setDefaultValue($skipNotProvidedValues) |
| 266 | + ->setInternal(true) |
| 267 | + ); |
| 268 | + } |
232 | 269 | } |
0 commit comments