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\MongoDbOdm\Metadata\Property; |
||
15 | |||
16 | use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
||
17 | use ApiPlatform\Core\Metadata\Property\PropertyMetadata; |
||
18 | use Doctrine\Common\Persistence\ManagerRegistry; |
||
19 | use Doctrine\ODM\MongoDB\DocumentManager; |
||
20 | |||
21 | /** |
||
22 | * Use Doctrine metadata to populate the identifier property. |
||
23 | * |
||
24 | * @experimental |
||
25 | * |
||
26 | * @author Kévin Dunglas <[email protected]> |
||
27 | * @author Alan Poulain <[email protected]> |
||
28 | */ |
||
29 | final class DoctrineMongoDbOdmPropertyMetadataFactory implements PropertyMetadataFactoryInterface |
||
30 | { |
||
31 | private $decorated; |
||
32 | private $managerRegistry; |
||
33 | |||
34 | public function __construct(ManagerRegistry $managerRegistry, PropertyMetadataFactoryInterface $decorated) |
||
35 | { |
||
36 | $this->managerRegistry = $managerRegistry; |
||
37 | $this->decorated = $decorated; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata |
||
44 | { |
||
45 | $propertyMetadata = $this->decorated->create($resourceClass, $property, $options); |
||
46 | |||
47 | if (null !== $propertyMetadata->isIdentifier()) { |
||
48 | return $propertyMetadata; |
||
49 | } |
||
50 | |||
51 | $manager = $this->managerRegistry->getManagerForClass($resourceClass); |
||
52 | if (!$manager instanceof DocumentManager) { |
||
53 | return $propertyMetadata; |
||
54 | } |
||
55 | |||
56 | $doctrineClassMetadata = $manager->getClassMetadata($resourceClass); |
||
57 | $identifiers = $doctrineClassMetadata->getIdentifier(); |
||
58 | |||
59 | foreach ($identifiers as $identifier) { |
||
60 | if ($identifier === $property) { |
||
61 | $propertyMetadata = $propertyMetadata->withIdentifier(true); |
||
62 | |||
63 | if (null !== $propertyMetadata->isWritable()) { |
||
64 | break; |
||
65 | } |
||
66 | |||
67 | $propertyMetadata = $propertyMetadata->withWritable(false); |
||
68 | |||
69 | break; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | if (null === $propertyMetadata->isIdentifier()) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
74 | $propertyMetadata = $propertyMetadata->withIdentifier(false); |
||
75 | } |
||
76 | |||
77 | return $propertyMetadata; |
||
78 | } |
||
79 | } |
||
80 |