|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ApiClients\Tools\OpenApiClientGenerator\Gatherer; |
| 6 | + |
| 7 | +use ApiClients\Tools\OpenApiClientGenerator\ClassString; |
| 8 | +use ApiClients\Tools\OpenApiClientGenerator\Configuration\Namespace_; |
| 9 | +use ApiClients\Tools\OpenApiClientGenerator\Registry\CompositSchema as CompositSchemaRegistry; |
| 10 | +use ApiClients\Tools\OpenApiClientGenerator\Registry\Contract as ContractRegistry; |
| 11 | +use ApiClients\Tools\OpenApiClientGenerator\Registry\Schema as SchemaRegistry; |
| 12 | +use ApiClients\Tools\OpenApiClientGenerator\Representation\Contract; |
| 13 | +use ApiClients\Tools\OpenApiClientGenerator\Representation\Schema; |
| 14 | +use ApiClients\Tools\OpenApiClientGenerator\Utils; |
| 15 | +use cebe\openapi\spec\Schema as baseSchema; |
| 16 | + |
| 17 | +use function array_key_exists; |
| 18 | +use function in_array; |
| 19 | +use function is_array; |
| 20 | +use function property_exists; |
| 21 | + |
| 22 | +final class IntersectionSchema |
| 23 | +{ |
| 24 | + public static function gather( |
| 25 | + Namespace_ $baseNamespace, |
| 26 | + string $className, |
| 27 | + baseSchema $baseProperty, |
| 28 | + SchemaRegistry $schemaRegistry, |
| 29 | + ContractRegistry $contractRegistry, |
| 30 | + CompositSchemaRegistry $compositSchemaRegistry, |
| 31 | + ): Schema { |
| 32 | + $className = Utils::className($className); |
| 33 | + $contracts = []; |
| 34 | + $properties = []; |
| 35 | + $example = []; |
| 36 | + |
| 37 | + foreach ($baseProperty->allOf as $schema) { |
| 38 | + $gatheredProperties = []; |
| 39 | + foreach ($schema->properties as $propertyName => $property) { |
| 40 | + $gatheredProperty = $gatheredProperties[(string) $propertyName] = Property::gather( |
| 41 | + $baseNamespace, |
| 42 | + $className, |
| 43 | + (string) $propertyName, |
| 44 | + in_array( |
| 45 | + (string) $propertyName, |
| 46 | + $schema->required ?? [], |
| 47 | + false, |
| 48 | + ), |
| 49 | + $property, |
| 50 | + $schemaRegistry, |
| 51 | + $contractRegistry, |
| 52 | + $compositSchemaRegistry, |
| 53 | + ); |
| 54 | + |
| 55 | + $example[$gatheredProperty->sourceName] = $gatheredProperty->example->raw; |
| 56 | + |
| 57 | + foreach (['examples', 'example'] as $examplePropertyName) { |
| 58 | + if (array_key_exists($gatheredProperty->sourceName, $example)) { |
| 59 | + break; |
| 60 | + } |
| 61 | + |
| 62 | + if (! property_exists($schema, $examplePropertyName) || ! is_array($schema->$examplePropertyName) || ! array_key_exists($gatheredProperty->sourceName, $schema->$examplePropertyName)) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + $example[$gatheredProperty->sourceName] = $schema->$examplePropertyName[$gatheredProperty->sourceName]; |
| 67 | + } |
| 68 | + |
| 69 | + foreach ($property->enum ?? [] as $value) { |
| 70 | + $example[$gatheredProperty->sourceName] = $value; |
| 71 | + break; |
| 72 | + } |
| 73 | + |
| 74 | + if ($example[$gatheredProperty->sourceName] !== null || $property->required || $baseProperty->required) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + unset($example[$gatheredProperty->sourceName]); |
| 79 | + } |
| 80 | + |
| 81 | + $contracts[] = new Contract( |
| 82 | + ClassString::factory( |
| 83 | + $baseNamespace, |
| 84 | + $contractRegistry->get($schema, 'Contract\\' . $className . '\\' . $schema->title), |
| 85 | + ), |
| 86 | + $gatheredProperties, |
| 87 | + ); |
| 88 | + |
| 89 | + $properties = [...$properties, ...$gatheredProperties]; |
| 90 | + } |
| 91 | + |
| 92 | + return new Schema( |
| 93 | + ClassString::factory($baseNamespace, 'Schema\\' . $className), |
| 94 | + $contracts, |
| 95 | + ClassString::factory($baseNamespace, 'Error\\' . $className), |
| 96 | + ClassString::factory($baseNamespace, 'ErrorSchemas\\' . $className), |
| 97 | + $baseProperty->title ?? '', |
| 98 | + $baseProperty->description ?? '', |
| 99 | + $example, |
| 100 | + $properties, |
| 101 | + $baseProperty, |
| 102 | + false, |
| 103 | + ($baseProperty->type === null ? ['object'] : (is_array($baseProperty->type) ? $baseProperty->type : [$baseProperty->type])), |
| 104 | + ); |
| 105 | + } |
| 106 | +} |
0 commit comments