|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Netgen\ApiPlatformExtras\ApiPlatform\Hydra\JsonSchema; |
| 6 | + |
| 7 | +use ApiPlatform\JsonSchema\Schema; |
| 8 | +use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface; |
| 9 | +use ApiPlatform\JsonSchema\SchemaFactoryInterface; |
| 10 | +use ApiPlatform\Metadata\Operation; |
| 11 | + |
| 12 | +use function is_array; |
| 13 | + |
| 14 | +final class SchemaFactoryDecorator implements SchemaFactoryInterface, SchemaFactoryAwareInterface |
| 15 | +{ |
| 16 | + private const string HYDRA_COLLECTION_BASE_SCHEMA_NAME = 'HydraCollectionBaseSchema'; |
| 17 | + |
| 18 | + private const array HYDRA_VIEW_KEYS = ['hydra:view', 'view']; |
| 19 | + |
| 20 | + private const array PAGINATION_PROPERTIES = [ |
| 21 | + 'firstPage' => [ |
| 22 | + 'type' => 'integer', |
| 23 | + 'minimum' => 0, |
| 24 | + ], |
| 25 | + 'lastPage' => [ |
| 26 | + 'type' => 'integer', |
| 27 | + 'minimum' => 0, |
| 28 | + ], |
| 29 | + 'currentPage' => [ |
| 30 | + 'type' => 'integer', |
| 31 | + 'minimum' => 0, |
| 32 | + ], |
| 33 | + 'previousPage' => [ |
| 34 | + 'type' => 'integer', |
| 35 | + 'minimum' => 0, |
| 36 | + ], |
| 37 | + 'nextPage' => [ |
| 38 | + 'type' => 'integer', |
| 39 | + 'minimum' => 0, |
| 40 | + ], |
| 41 | + 'itemsPerPage' => [ |
| 42 | + 'type' => 'integer', |
| 43 | + 'minimum' => 0, |
| 44 | + ], |
| 45 | + ]; |
| 46 | + |
| 47 | + private const array PAGINATION_EXAMPLE_VALUES = [ |
| 48 | + 'firstPage' => 1, |
| 49 | + 'lastPage' => 10, |
| 50 | + 'currentPage' => 1, |
| 51 | + 'previousPage' => 1, |
| 52 | + 'nextPage' => 2, |
| 53 | + 'itemsPerPage' => 30, |
| 54 | + ]; |
| 55 | + |
| 56 | + public function __construct( |
| 57 | + private readonly SchemaFactoryInterface $decorated, |
| 58 | + ) {} |
| 59 | + |
| 60 | + public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void |
| 61 | + { |
| 62 | + if ($this->decorated instanceof SchemaFactoryAwareInterface) { |
| 63 | + $this->decorated->setSchemaFactory($schemaFactory); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** @param array<string, mixed>|null $serializerContext */ |
| 68 | + public function buildSchema(string $className, string $format = 'json', string $type = Schema::TYPE_OUTPUT, ?Operation $operation = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema |
| 69 | + { |
| 70 | + $schema = $this->decorated->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection); |
| 71 | + |
| 72 | + if ('jsonld' !== $format) { |
| 73 | + return $schema; |
| 74 | + } |
| 75 | + |
| 76 | + $definitions = $schema->getDefinitions(); |
| 77 | + $collectionBaseSchema = $definitions[self::HYDRA_COLLECTION_BASE_SCHEMA_NAME] ?? null; |
| 78 | + |
| 79 | + if (!is_array($collectionBaseSchema)) { |
| 80 | + return $schema; |
| 81 | + } |
| 82 | + |
| 83 | + $allOf = $collectionBaseSchema['allOf'] ?? null; |
| 84 | + if (!is_array($allOf) || !isset($allOf[1]) || !is_array($allOf[1])) { |
| 85 | + return $schema; |
| 86 | + } |
| 87 | + |
| 88 | + $properties = $allOf[1]['properties'] ?? null; |
| 89 | + if (!is_array($properties)) { |
| 90 | + return $schema; |
| 91 | + } |
| 92 | + |
| 93 | + if ( |
| 94 | + isset($properties['view']['properties']['firstPage']) |
| 95 | + || isset($properties['hydra:view']['properties']['firstPage']) |
| 96 | + ) { |
| 97 | + return $schema; |
| 98 | + } |
| 99 | + |
| 100 | + foreach (self::HYDRA_VIEW_KEYS as $viewKey) { |
| 101 | + $viewSchema = $properties[$viewKey] ?? null; |
| 102 | + if (!is_array($viewSchema)) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + $viewProperties = $viewSchema['properties'] ?? []; |
| 107 | + if (!is_array($viewProperties)) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + foreach (self::PAGINATION_PROPERTIES as $propertyName => $propertySchema) { |
| 112 | + $viewProperties[$propertyName] ??= $propertySchema; |
| 113 | + } |
| 114 | + |
| 115 | + $viewSchema['properties'] = $viewProperties; |
| 116 | + $viewExample = $viewSchema['example'] ?? []; |
| 117 | + if (is_array($viewExample)) { |
| 118 | + foreach (self::PAGINATION_EXAMPLE_VALUES as $propertyName => $propertyValue) { |
| 119 | + $viewExample[$propertyName] ??= $propertyValue; |
| 120 | + } |
| 121 | + |
| 122 | + $viewSchema['example'] = $viewExample; |
| 123 | + } |
| 124 | + |
| 125 | + $properties[$viewKey] = $viewSchema; |
| 126 | + } |
| 127 | + |
| 128 | + $allOf[1]['properties'] = $properties; |
| 129 | + $collectionBaseSchema['allOf'] = $allOf; |
| 130 | + $definitions[self::HYDRA_COLLECTION_BASE_SCHEMA_NAME] = $collectionBaseSchema; |
| 131 | + |
| 132 | + return $schema; |
| 133 | + } |
| 134 | +} |
0 commit comments