1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the API Platform project. |
||
5 | * |
||
6 | * (c) Kévin Dunglas <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace ApiPlatform\Core\Bridge\Doctrine\Common\Util; |
||
15 | |||
16 | use ApiPlatform\Core\Exception\PropertyNotFoundException; |
||
17 | use Doctrine\Common\Persistence\ObjectManager; |
||
18 | use Doctrine\DBAL\Types\Type as DBALType; |
||
19 | use Doctrine\ODM\MongoDB\DocumentManager; |
||
20 | use Doctrine\ODM\MongoDB\Types\Type as MongoDbType; |
||
21 | use Doctrine\ORM\EntityManagerInterface; |
||
22 | |||
23 | /** |
||
24 | * @internal |
||
25 | */ |
||
26 | trait IdentifierManagerTrait |
||
27 | { |
||
28 | private $propertyNameCollectionFactory; |
||
29 | private $propertyMetadataFactory; |
||
30 | |||
31 | /** |
||
32 | * Transform and check the identifier, composite or not. |
||
33 | * |
||
34 | * @param int|string $id |
||
35 | * |
||
36 | * @throws PropertyNotFoundException |
||
37 | */ |
||
38 | private function normalizeIdentifiers($id, ObjectManager $manager, string $resourceClass): array |
||
39 | { |
||
40 | $identifierValues = [$id]; |
||
41 | $doctrineClassMetadata = $manager->getClassMetadata($resourceClass); |
||
42 | $doctrineIdentifierFields = $doctrineClassMetadata->getIdentifier(); |
||
43 | $isOrm = $manager instanceof EntityManagerInterface; |
||
44 | $isOdm = $manager instanceof DocumentManager; |
||
45 | $platform = $isOrm ? $manager->getConnection()->getDatabasePlatform() : null; |
||
46 | $identifiersMap = null; |
||
47 | |||
48 | if (\count($doctrineIdentifierFields) > 1) { |
||
49 | $identifiersMap = []; |
||
50 | |||
51 | // first transform identifiers to a proper key/value array |
||
52 | foreach (explode(';', (string) $id) as $identifier) { |
||
53 | if (!$identifier) { |
||
54 | continue; |
||
55 | } |
||
56 | |||
57 | $identifierPair = explode('=', $identifier); |
||
58 | $identifiersMap[$identifierPair[0]] = $identifierPair[1]; |
||
59 | } |
||
60 | } |
||
61 | |||
62 | $identifiers = []; |
||
63 | $i = 0; |
||
64 | |||
65 | foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) { |
||
66 | $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
||
67 | |||
68 | if (!$propertyMetadata->isIdentifier()) { |
||
69 | continue; |
||
70 | } |
||
71 | |||
72 | $identifier = null === $identifiersMap ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null; |
||
73 | if (null === $identifier) { |
||
74 | throw new PropertyNotFoundException(sprintf('Invalid identifier "%s", "%s" was not found.', $id, $propertyName)); |
||
75 | } |
||
76 | |||
77 | $doctrineTypeName = $doctrineClassMetadata->getTypeOfField($propertyName); |
||
78 | |||
79 | if ($isOrm && null !== $doctrineTypeName && DBALType::hasType($doctrineTypeName)) { |
||
80 | $identifier = DBALType::getType($doctrineTypeName)->convertToPHPValue($identifier, $platform); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
81 | } |
||
82 | if ($isOdm && null !== $doctrineTypeName && MongoDbType::hasType($doctrineTypeName)) { |
||
83 | $identifier = MongoDbType::getType($doctrineTypeName)->convertToPHPValue($identifier); |
||
84 | } |
||
85 | |||
86 | $identifiers[$propertyName] = $identifier; |
||
87 | ++$i; |
||
88 | } |
||
89 | |||
90 | return $identifiers; |
||
91 | } |
||
92 | } |
||
93 |