-
-
Notifications
You must be signed in to change notification settings - Fork 896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(doctrine): new hook for entity/document transformation #6919
Draft
mrossard
wants to merge
7
commits into
api-platform:main
Choose a base branch
from
mrossard:entityclass-transform
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e7f6781
wip: ajout d'un hook entityTransformer
mrossard d69f17a
feat: add a hook for entity to resource transformation
mrossard ba90b5d
Merge remote-tracking branch 'mrossard/entityclass-transform' into en…
mrossard fbc5e1b
feat: add a hook for doctrine entity or document to resource transfor…
mrossard c7f7694
fix: silly mistake
mrossard 3b26b70
fix: run cs fixer
mrossard 9f4cb1a
feat: add a second hook for resource to entity or document transforma…
mrossard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
Feature: Use an entity or document transformer to return the correct ressource | ||
|
||
@createSchema | ||
@!mongodb | ||
Scenario: Get collection from entities | ||
Given there is a TransformedDummy for date '2025-01-01' | ||
When I send a "GET" request to "/transformed_dummy_entity_ressources" | ||
Then the response status code should be 200 | ||
And the response should be in JSON | ||
And the JSON node "hydra:totalItems" should be equal to 1 | ||
|
||
@!mongodb | ||
Scenario: Get item from entity | ||
Given there is a TransformedDummy for date '2025-01-01' | ||
When I send a "GET" request to "/transformed_dummy_entity_ressources/1" | ||
Then the response status code should be 200 | ||
And the response should be in JSON | ||
And the JSON node "year" should exist | ||
And the JSON node year should be equal to "2025" | ||
|
||
@createSchema | ||
@mongodb | ||
Scenario: Get collection from documents | ||
Given there is a TransformedDummy for date '2025-01-01' | ||
When I send a "GET" request to "/transformed_dummy_document_ressources" | ||
Then the response status code should be 200 | ||
And the response should be in JSON | ||
And the JSON node "hydra:totalItems" should be equal to 1 | ||
|
||
@mongodb | ||
Scenario: Get item from document | ||
Given there is a TransformedDummy for date '2025-01-01' | ||
When I send a "GET" request to "/transformed_dummy_document_ressources/1" | ||
Then the response status code should be 200 | ||
And the response should be in JSON | ||
And the JSON node "year" should exist | ||
And the JSON node year should be equal to "2025" |
43 changes: 43 additions & 0 deletions
43
src/Doctrine/Common/State/ModelTransformerLocatorTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Doctrine\Common\State; | ||
|
||
use ApiPlatform\Metadata\Operation; | ||
use Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* Maybe merge this and LinksHandlerLocatorTrait into a OptionsHooksLocatorTrait or something similar? | ||
*/ | ||
trait ModelTransformerLocatorTrait | ||
{ | ||
private ?ContainerInterface $transformEntityLocator; | ||
|
||
protected function getEntityTransformer(Operation $operation): ?callable | ||
{ | ||
if (!($options = $operation->getStateOptions()) || !$options instanceof Options) { | ||
return null; | ||
} | ||
|
||
$transformModel = $options->getTransformModel(); | ||
if (\is_callable($transformModel)) { | ||
return $transformModel; | ||
} | ||
|
||
if ($this->transformEntityLocator && \is_string($transformModel) && $this->transformEntityLocator->has($transformModel)) { | ||
return [$this->transformEntityLocator->get($transformModel), 'transformModel']; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the feature, I think it'd be a great fit with #6801 best would be that when the mapper is installed, we add a default transformer that uses the object mapper!
I'm just not a fan of the naming but this one is hard haha. A few ideas:
I think that it's missing a way to transform a resource to an entity when persisting data (POST/PATCH) WDYT?
Last but not least, you should work on an interface that'd be used to autoconfigure the "transformEntityLocator", it would surely help with naming things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, i'm not a fan of it either - tried to find a name that works with both entities or documents, but model feels lame, and I didn't want to use mapper to avoid confusion with what you're preparing on that side.
How about resourceBuilder or something along those lines?
Wouldn't it be redundant when we already have entityClass + handlelinks?
I'll look into this. I was not sure about that part, actually i was wondering (and i actually left a comment in the code) if this locator trait is better by itself or if it should be merged with LinksHandlerLocatorTrait into a common OptionsHooksLocatorTrait of some sort...WDYT?