1414namespace ApiPlatform \Doctrine \Odm \State ;
1515
1616use ApiPlatform \Doctrine \Common \State \LinksHandlerLocatorTrait ;
17+ use ApiPlatform \Doctrine \Common \State \ResourceTransformerLocatorTrait ;
1718use ApiPlatform \Doctrine \Odm \Extension \AggregationCollectionExtensionInterface ;
1819use ApiPlatform \Doctrine \Odm \Extension \AggregationResultCollectionExtensionInterface ;
1920use ApiPlatform \Metadata \Exception \RuntimeException ;
@@ -33,15 +34,20 @@ final class CollectionProvider implements ProviderInterface
3334{
3435 use LinksHandlerLocatorTrait;
3536 use LinksHandlerTrait;
37+ use ResourceTransformerLocatorTrait;
3638 use StateOptionsTrait;
3739
3840 /**
3941 * @param AggregationCollectionExtensionInterface[] $collectionExtensions
4042 */
41- public function __construct (ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory , ManagerRegistry $ managerRegistry , private readonly iterable $ collectionExtensions = [], ?ContainerInterface $ handleLinksLocator = null )
42- {
43+ public function __construct (ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory , ManagerRegistry $ managerRegistry ,
44+ private readonly iterable $ collectionExtensions = [],
45+ ?ContainerInterface $ handleLinksLocator = null ,
46+ ?ContainerInterface $ resourceTransformerLocator = null ,
47+ ) {
4348 $ this ->resourceMetadataCollectionFactory = $ resourceMetadataCollectionFactory ;
4449 $ this ->handleLinksLocator = $ handleLinksLocator ;
50+ $ this ->resourceTransformerLocator = $ resourceTransformerLocator ;
4551 $ this ->managerRegistry = $ managerRegistry ;
4652 }
4753
@@ -69,13 +75,19 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
6975 $ extension ->applyToCollection ($ aggregationBuilder , $ documentClass , $ operation , $ context );
7076
7177 if ($ extension instanceof AggregationResultCollectionExtensionInterface && $ extension ->supportsResult ($ documentClass , $ operation , $ context )) {
72- return $ extension ->getResult ($ aggregationBuilder , $ documentClass , $ operation , $ context );
78+ $ result = $ extension ->getResult ($ aggregationBuilder , $ documentClass , $ operation , $ context );
79+ break ;
7380 }
7481 }
7582
7683 $ attribute = $ operation ->getExtraProperties ()['doctrine_mongodb ' ] ?? [];
7784 $ executeOptions = $ attribute ['execute_options ' ] ?? [];
7885
79- return $ aggregationBuilder ->hydrate ($ documentClass )->getAggregation ($ executeOptions )->getIterator ();
86+ $ result = $ result ?? $ aggregationBuilder ->hydrate ($ documentClass )->getAggregation ($ executeOptions )->getIterator ();
87+
88+ return match ($ transformer = $ this ->getToResourceTransformer ($ operation )) {
89+ null => $ result ,
90+ default => array_map ($ transformer , iterator_to_array ($ result )),
91+ };
8092 }
8193}
0 commit comments