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\Orm\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\ORM\EntityManagerInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @internal |
23
|
|
|
*/ |
24
|
|
|
trait IdentifierManagerTrait |
25
|
|
|
{ |
26
|
|
|
private $propertyNameCollectionFactory; |
27
|
|
|
private $propertyMetadataFactory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Transform and check the identifier, composite or not. |
31
|
|
|
* |
32
|
|
|
* @param int|string $id |
33
|
|
|
* @param ObjectManager $manager |
34
|
|
|
* @param string $resourceClass |
35
|
|
|
* |
36
|
|
|
* @throws PropertyNotFoundException |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
private function normalizeIdentifiers($id, ObjectManager $manager, string $resourceClass): array |
41
|
|
|
{ |
42
|
|
|
$identifierValues = [$id]; |
43
|
|
|
$doctrineClassMetadata = $manager->getClassMetadata($resourceClass); |
44
|
|
|
$doctrineIdentifierFields = $doctrineClassMetadata->getIdentifier(); |
45
|
|
|
$isOrm = interface_exists(EntityManagerInterface::class) && $manager instanceof EntityManagerInterface; |
46
|
|
|
$platform = $isOrm ? $manager->getConnection()->getDatabasePlatform() : null; |
47
|
|
|
|
48
|
|
|
if (count($doctrineIdentifierFields) > 1) { |
49
|
|
|
$identifiersMap = []; |
50
|
|
|
|
51
|
|
|
// first transform identifiers to a proper key/value array |
52
|
|
|
foreach (explode(';', $id) as $identifier) { |
53
|
|
|
$identifierPair = explode('=', $identifier); |
54
|
|
|
$identifiersMap[$identifierPair[0]] = $identifierPair[1]; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$identifiers = []; |
59
|
|
|
$i = 0; |
60
|
|
|
|
61
|
|
|
foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) { |
62
|
|
|
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
63
|
|
|
|
64
|
|
|
if (!$propertyMetadata->isIdentifier()) { |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$identifier = !isset($identifiersMap) ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null; |
69
|
|
|
if (null === $identifier) { |
70
|
|
|
throw new PropertyNotFoundException(sprintf('Invalid identifier "%s", "%s" has not been found.', $id, $propertyName)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$doctrineTypeName = $doctrineClassMetadata->getTypeOfField($propertyName); |
74
|
|
|
|
75
|
|
|
if ($isOrm && null !== $doctrineTypeName && DBALType::hasType($doctrineTypeName)) { |
76
|
|
|
$identifier = DBALType::getType($doctrineTypeName)->convertToPHPValue($identifier, $platform); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$identifiers[$propertyName] = $identifier; |
80
|
|
|
++$i; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $identifiers; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|