Skip to content

Commit f734f10

Browse files
committed
feat: add a hook for doctrine entity or document to resource transformation
1 parent e50afcc commit f734f10

14 files changed

+128
-49
lines changed

features/doctrine/transform_entity.feature

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Feature: Use an entity or document transformer to return the correct ressource
2+
3+
@createSchema
4+
@!mongodb
5+
Scenario: Get collection from entities
6+
Given there is a TransformedDummy for date '2025-01-01'
7+
When I send a "GET" request to "/transformed_dummy_entity_ressources"
8+
Then the response status code should be 200
9+
And the response should be in JSON
10+
And the JSON node "hydra:totalItems" should be equal to 1
11+
12+
@!mongodb
13+
Scenario: Get item from entity
14+
Given there is a TransformedDummy for date '2025-01-01'
15+
When I send a "GET" request to "/transformed_dummy_entity_ressources/1"
16+
Then the response status code should be 200
17+
And the response should be in JSON
18+
And the JSON node "year" should exist
19+
And the JSON node year should be equal to "2025"
20+
21+
@createSchema
22+
@mongodb
23+
Scenario: Get collection from documents
24+
Given there is a TransformedDummy for date '2025-01-01'
25+
When I send a "GET" request to "/transformed_dummy_document_ressources"
26+
Then the response status code should be 200
27+
And the response should be in JSON
28+
And the JSON node "hydra:totalItems" should be equal to 1
29+
30+
@mongodb
31+
Scenario: Get item from document
32+
Given there is a TransformedDummy for date '2025-01-01'
33+
When I send a "GET" request to "/transformed_dummy_document_ressources/1"
34+
Then the response status code should be 200
35+
And the response should be in JSON
36+
And the JSON node "year" should exist
37+
And the JSON node year should be equal to "2025"

src/Doctrine/Common/State/EntityTransformerLocatorTrait.php renamed to src/Doctrine/Common/State/ModelTransformerLocatorTrait.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* Maybe merge this and LinksHandlerLocatorTrait into a OptionsHooksLocatorTrait or something similar?
1010
*/
11-
trait EntityTransformerLocatorTrait
11+
trait ModelTransformerLocatorTrait
1212
{
1313
private ?ContainerInterface $transformEntityLocator;
1414

@@ -18,13 +18,13 @@ protected function getEntityTransformer(Operation $operation): ?callable
1818
return null;
1919
}
2020

21-
$transformEntity = $options->getTransformEntity();
22-
if (\is_callable($transformEntity)) {
23-
return $transformEntity;
21+
$transformModel = $options->getTransformModel();
22+
if (\is_callable($transformModel)) {
23+
return $transformModel;
2424
}
2525

26-
if ($this->transformEntityLocator && \is_string($transformEntity) && $this->transformEntityLocator->has($transformEntity)) {
27-
return [$this->transformEntityLocator->get($transformEntity), 'transformEntity'];
26+
if ($this->transformEntityLocator && \is_string($transformModel) && $this->transformEntityLocator->has($transformModel)) {
27+
return [$this->transformEntityLocator->get($transformModel), 'transformModel'];
2828
}
2929

3030
return null;

src/Doctrine/Common/State/Options.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ class Options implements OptionsInterface
1919
{
2020
/**
2121
* @param mixed $handleLinks experimental callable, typed mixed as we may want a service name in the future
22-
* @param mixed $transformEntity experimental callable, typed mixed as we may want a service name in the future
2322
*/
2423
public function __construct(
2524
protected mixed $handleLinks = null,
26-
protected mixed $transformEntity = null,
25+
protected mixed $transformModel = null
2726
) {
2827
}
2928

@@ -40,15 +39,15 @@ public function withHandleLinks(mixed $handleLinks): self
4039
return $self;
4140
}
4241

43-
public function getTransformEntity(): mixed
42+
public function getTransformModel(): mixed
4443
{
45-
return $this->transformEntity;
44+
return $this->transformModel;
4645
}
4746

48-
public function withTransformEntity(mixed $transformEntity): self
47+
public function withTransformModel(mixed $transformModel): self
4948
{
5049
$self = clone $this;
51-
$self->transformEntity = $transformEntity;
50+
$self->transformModel = $transformModel;
5251

5352
return $self;
5453
}

src/Doctrine/Odm/State/CollectionProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace ApiPlatform\Doctrine\Odm\State;
1515

16-
use ApiPlatform\Doctrine\Common\State\EntityTransformerLocatorTrait;
16+
use ApiPlatform\Doctrine\Common\State\ModelTransformerLocatorTrait;
1717
use ApiPlatform\Doctrine\Common\State\LinksHandlerLocatorTrait;
1818
use ApiPlatform\Doctrine\Odm\Extension\AggregationCollectionExtensionInterface;
1919
use ApiPlatform\Doctrine\Odm\Extension\AggregationResultCollectionExtensionInterface;
@@ -34,7 +34,7 @@ final class CollectionProvider implements ProviderInterface
3434
{
3535
use LinksHandlerLocatorTrait;
3636
use LinksHandlerTrait;
37-
use EntityTransformerLocatorTrait;
37+
use ModelTransformerLocatorTrait;
3838
use StateOptionsTrait;
3939

4040
/**

src/Doctrine/Odm/State/ItemProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace ApiPlatform\Doctrine\Odm\State;
1515

16-
use ApiPlatform\Doctrine\Common\State\EntityTransformerLocatorTrait;
16+
use ApiPlatform\Doctrine\Common\State\ModelTransformerLocatorTrait;
1717
use ApiPlatform\Doctrine\Common\State\LinksHandlerLocatorTrait;
1818
use ApiPlatform\Doctrine\Odm\Extension\AggregationItemExtensionInterface;
1919
use ApiPlatform\Doctrine\Odm\Extension\AggregationResultItemExtensionInterface;
@@ -38,7 +38,7 @@ final class ItemProvider implements ProviderInterface
3838
use LinksHandlerLocatorTrait;
3939
use LinksHandlerTrait;
4040
use StateOptionsTrait;
41-
use EntityTransformerLocatorTrait;
41+
use ModelTransformerLocatorTrait;
4242

4343
/**
4444
* @param AggregationItemExtensionInterface[] $itemExtensions

src/Doctrine/Odm/State/Options.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ class Options extends CommonOptions implements OptionsInterface
2020
{
2121
/**
2222
* @param mixed $handleLinks experimental callable, typed mixed as we may want a service name in the future
23+
* @param mixed $transformDocument experimental callable, typed mixed as we may want a service name in the future
2324
*
2425
* @see LinksHandlerInterface
2526
*/
2627
public function __construct(
2728
protected ?string $documentClass = null,
2829
mixed $handleLinks = null,
30+
mixed $transformDocument = null,
2931
) {
30-
parent::__construct(handleLinks: $handleLinks);
32+
parent::__construct(handleLinks: $handleLinks, transformModel: $transformDocument);
3133
}
3234

3335
public function getDocumentClass(): ?string
@@ -42,4 +44,14 @@ public function withDocumentClass(?string $documentClass): self
4244

4345
return $self;
4446
}
47+
48+
public function getTransformDocument(): mixed
49+
{
50+
return $this->getTransformModel();
51+
}
52+
53+
public function withTransformDocument(mixed $transformDocument): self
54+
{
55+
return $this->withTransformModel($transformDocument);
56+
}
4557
}

src/Doctrine/Orm/State/CollectionProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace ApiPlatform\Doctrine\Orm\State;
1515

16-
use ApiPlatform\Doctrine\Common\State\EntityTransformerLocatorTrait;
16+
use ApiPlatform\Doctrine\Common\State\ModelTransformerLocatorTrait;
1717
use ApiPlatform\Doctrine\Common\State\LinksHandlerLocatorTrait;
1818
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
1919
use ApiPlatform\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
@@ -37,8 +37,8 @@ final class CollectionProvider implements ProviderInterface
3737
{
3838
use LinksHandlerLocatorTrait;
3939
use LinksHandlerTrait;
40+
use ModelTransformerLocatorTrait;
4041
use StateOptionsTrait;
41-
use EntityTransformerLocatorTrait;
4242

4343
/**
4444
* @param QueryCollectionExtensionInterface[] $collectionExtensions

src/Doctrine/Orm/State/ItemProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace ApiPlatform\Doctrine\Orm\State;
1515

16-
use ApiPlatform\Doctrine\Common\State\EntityTransformerLocatorTrait;
16+
use ApiPlatform\Doctrine\Common\State\ModelTransformerLocatorTrait;
1717
use ApiPlatform\Doctrine\Common\State\LinksHandlerLocatorTrait;
1818
use ApiPlatform\Doctrine\Orm\Extension\QueryItemExtensionInterface;
1919
use ApiPlatform\Doctrine\Orm\Extension\QueryResultItemExtensionInterface;
@@ -38,7 +38,7 @@ final class ItemProvider implements ProviderInterface
3838
use LinksHandlerLocatorTrait;
3939
use LinksHandlerTrait;
4040
use StateOptionsTrait;
41-
use EntityTransformerLocatorTrait;
41+
use ModelTransformerLocatorTrait;
4242

4343
/**
4444
* @param QueryItemExtensionInterface[] $itemExtensions

src/Doctrine/Orm/State/Options.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(
2929
mixed $handleLinks = null,
3030
mixed $transformEntity = null
3131
) {
32-
parent::__construct(handleLinks: $handleLinks, transformEntity: $transformEntity);
32+
parent::__construct(handleLinks: $handleLinks, transformModel: $transformEntity);
3333
}
3434

3535
public function getEntityClass(): ?string
@@ -44,4 +44,14 @@ public function withEntityClass(?string $entityClass): self
4444

4545
return $self;
4646
}
47+
48+
public function getTransformDocument(): mixed
49+
{
50+
return $this->getTransformModel();
51+
}
52+
53+
public function withTransformDocument(mixed $transformEntity): self
54+
{
55+
return $this->withTransformModel($transformEntity);
56+
}
4757
}

0 commit comments

Comments
 (0)