-
-
Notifications
You must be signed in to change notification settings - Fork 971
Move normalizeIdentifiers method to a new Util class IdentifierManager #907
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Util; | ||
|
|
||
| use ApiPlatform\Core\Exception\PropertyNotFoundException; | ||
| use Doctrine\Common\Persistence\ObjectManager; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| trait IdentifierManagerTrait | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make it
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think Or perhaps another, because the "manager" part of
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about |
||
| { | ||
| /** | ||
| * Transform and check the identifier, composite or not. | ||
| * | ||
| * @param int|string $id | ||
| * @param ObjectManager $manager | ||
| * @param string $resourceClass | ||
| * | ||
| * @throws PropertyNotFoundException | ||
| * | ||
| * @return array | ||
| */ | ||
| public function normalizeIdentifiers($id, ObjectManager $manager, string $resourceClass): array | ||
| { | ||
| $identifierValues = [$id]; | ||
| $doctrineMetadataIdentifier = $manager->getClassMetadata($resourceClass)->getIdentifier(); | ||
|
|
||
| if (2 <= count($doctrineMetadataIdentifier)) { | ||
| $identifiers = explode(';', $id); | ||
| $identifiersMap = []; | ||
|
|
||
| // first transform identifiers to a proper key/value array | ||
| foreach ($identifiers as $identifier) { | ||
| $keyValue = explode('=', $identifier); | ||
| $identifiersMap[$keyValue[0]] = $keyValue[1]; | ||
| } | ||
| } | ||
|
|
||
| $identifiers = []; | ||
| $i = 0; | ||
|
|
||
| foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) { | ||
| $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); | ||
|
|
||
| $identifier = $propertyMetadata->isIdentifier(); | ||
| if (null === $identifier || false === $identifier) { | ||
| continue; | ||
| } | ||
|
|
||
| $identifier = !isset($identifiersMap) ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null; | ||
| if (null === $identifier) { | ||
| throw new PropertyNotFoundException(sprintf('Invalid identifier "%s", "%s" has not been found.', $id, $propertyName)); | ||
| } | ||
|
|
||
| $identifiers[$propertyName] = $identifier; | ||
| ++$i; | ||
| } | ||
|
|
||
| return $identifiers; | ||
| } | ||
| } | ||
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.
Problem: this is a BC break...
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.
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.
why btw? Interface hasn't been impacted or is it because the class isn't "final" and so can be extended?
Uh oh!
There was an error while loading. Please reload this page.
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.
It's because if someone initializes this class by himself (and some people including me do) it will break.
This document is a nice reference of what is a BC break or not: http://symfony.com/doc/current/contributing/code/bc.html
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.
Nice link! To bad though, this is a nice improvement IMO.
I can make this work without BC by still injecting both metadata factories and calling
new IdentifierManagermanually in the constructor, not sure that it'd be accepted though.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.
It's a good solution for now.
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.
Btw, maybe should we make this new class internal as this logic should not be exposed or replaced. Maybe can we just use a trait too.
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 agree this could be a trait
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.
lol @Simperfit still we don't need the factories in the data provider but they're needed to get the identifiers. What would be the best to inject those?
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.
Let's use a trait.