-
-
Notifications
You must be signed in to change notification settings - Fork 271
/
EntityMetadata.php
105 lines (86 loc) · 3.32 KB
/
EntityMetadata.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\UX\Autocomplete\Doctrine;
use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\Persistence\Mapping\ClassMetadata;
/**
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
class EntityMetadata
{
public function __construct(
private ClassMetadata $metadata,
) {
}
public function getAllPropertyNames(): array
{
return $this->metadata->getFieldNames();
}
public function isAssociation(string $propertyName): bool
{
return \array_key_exists($propertyName, $this->metadata->associationMappings)
|| (str_contains($propertyName, '.') && !$this->isEmbeddedClassProperty($propertyName));
}
public function isEmbeddedClassProperty(string $propertyName): bool
{
$propertyNameParts = explode('.', $propertyName, 2);
return \array_key_exists($propertyNameParts[0], $this->metadata->embeddedClasses);
}
public function getPropertyMetadata(string $propertyName): array
{
trigger_deprecation('symfony/ux-autocomplete', '2.15.0', 'Calling EntityMetadata::getPropertyMetadata() is deprecated. You should stop using it, as it will be removed in the future.');
try {
return $this->getFieldMetadata($propertyName);
} catch (\InvalidArgumentException $e) {
return $this->getAssociationMetadata($propertyName);
}
}
/**
* @internal
*
* @return array<string, mixed>
*/
public function getFieldMetadata(string $propertyName): array
{
if (\array_key_exists($propertyName, $this->metadata->fieldMappings)) {
// Cast to array, because in doctrine/orm:^3.0; $metadata will be a FieldMapping object
return (array) $this->metadata->fieldMappings[$propertyName];
}
throw new \InvalidArgumentException(sprintf('The "%s" field does not exist in the "%s" entity.', $propertyName, $this->metadata->getName()));
}
/**
* @internal
*
* @return array<string, mixed>
*/
public function getAssociationMetadata(string $propertyName): array
{
if (\array_key_exists($propertyName, $this->metadata->associationMappings)) {
$associationMapping = $this->metadata->associationMappings[$propertyName];
// Doctrine ORM 3.0
if (class_exists(AssociationMapping::class) && $associationMapping instanceof AssociationMapping) {
return $associationMapping->toArray();
}
return $associationMapping;
}
throw new \InvalidArgumentException(sprintf('The "%s" field does not exist in the "%s" entity.', $propertyName, $this->metadata->getName()));
}
public function getPropertyDataType(string $propertyName): string
{
if (\array_key_exists($propertyName, $this->metadata->fieldMappings)) {
return $this->getFieldMetadata($propertyName)['type'];
}
return $this->getAssociationMetadata($propertyName)['type'];
}
public function getIdValue(object $entity): string
{
return current($this->metadata->getIdentifierValues($entity));
}
}