diff options
author | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
commit | bf6655a534a6775d30cafa67bd801276bda1d98d (patch) | |
tree | c6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/doctrine/orm/src/Utility/PersisterHelper.php | |
parent | 94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff) | |
download | AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip |
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/doctrine/orm/src/Utility/PersisterHelper.php')
-rw-r--r-- | vendor/doctrine/orm/src/Utility/PersisterHelper.php | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Utility/PersisterHelper.php b/vendor/doctrine/orm/src/Utility/PersisterHelper.php new file mode 100644 index 0000000..76e9242 --- /dev/null +++ b/vendor/doctrine/orm/src/Utility/PersisterHelper.php | |||
@@ -0,0 +1,108 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\ORM\Utility; | ||
6 | |||
7 | use Doctrine\ORM\EntityManagerInterface; | ||
8 | use Doctrine\ORM\Mapping\ClassMetadata; | ||
9 | use Doctrine\ORM\Query\QueryException; | ||
10 | use RuntimeException; | ||
11 | |||
12 | use function sprintf; | ||
13 | |||
14 | /** | ||
15 | * The PersisterHelper contains logic to infer binding types which is used in | ||
16 | * several persisters. | ||
17 | * | ||
18 | * @link www.doctrine-project.org | ||
19 | */ | ||
20 | class PersisterHelper | ||
21 | { | ||
22 | /** | ||
23 | * @return list<string> | ||
24 | * | ||
25 | * @throws QueryException | ||
26 | */ | ||
27 | public static function getTypeOfField(string $fieldName, ClassMetadata $class, EntityManagerInterface $em): array | ||
28 | { | ||
29 | if (isset($class->fieldMappings[$fieldName])) { | ||
30 | return [$class->fieldMappings[$fieldName]->type]; | ||
31 | } | ||
32 | |||
33 | if (! isset($class->associationMappings[$fieldName])) { | ||
34 | return []; | ||
35 | } | ||
36 | |||
37 | $assoc = $class->associationMappings[$fieldName]; | ||
38 | |||
39 | if (! $assoc->isOwningSide()) { | ||
40 | return self::getTypeOfField($assoc->mappedBy, $em->getClassMetadata($assoc->targetEntity), $em); | ||
41 | } | ||
42 | |||
43 | if ($assoc->isManyToManyOwningSide()) { | ||
44 | $joinData = $assoc->joinTable; | ||
45 | } else { | ||
46 | $joinData = $assoc; | ||
47 | } | ||
48 | |||
49 | $types = []; | ||
50 | $targetClass = $em->getClassMetadata($assoc->targetEntity); | ||
51 | |||
52 | foreach ($joinData->joinColumns as $joinColumn) { | ||
53 | $types[] = self::getTypeOfColumn($joinColumn->referencedColumnName, $targetClass, $em); | ||
54 | } | ||
55 | |||
56 | return $types; | ||
57 | } | ||
58 | |||
59 | /** @throws RuntimeException */ | ||
60 | public static function getTypeOfColumn(string $columnName, ClassMetadata $class, EntityManagerInterface $em): string | ||
61 | { | ||
62 | if (isset($class->fieldNames[$columnName])) { | ||
63 | $fieldName = $class->fieldNames[$columnName]; | ||
64 | |||
65 | if (isset($class->fieldMappings[$fieldName])) { | ||
66 | return $class->fieldMappings[$fieldName]->type; | ||
67 | } | ||
68 | } | ||
69 | |||
70 | // iterate over to-one association mappings | ||
71 | foreach ($class->associationMappings as $assoc) { | ||
72 | if (! $assoc->isToOneOwningSide()) { | ||
73 | continue; | ||
74 | } | ||
75 | |||
76 | foreach ($assoc->joinColumns as $joinColumn) { | ||
77 | if ($joinColumn->name === $columnName) { | ||
78 | $targetColumnName = $joinColumn->referencedColumnName; | ||
79 | $targetClass = $em->getClassMetadata($assoc->targetEntity); | ||
80 | |||
81 | return self::getTypeOfColumn($targetColumnName, $targetClass, $em); | ||
82 | } | ||
83 | } | ||
84 | } | ||
85 | |||
86 | // iterate over to-many association mappings | ||
87 | foreach ($class->associationMappings as $assoc) { | ||
88 | if (! $assoc->isManyToManyOwningSide()) { | ||
89 | continue; | ||
90 | } | ||
91 | |||
92 | foreach ($assoc->joinTable->joinColumns as $joinColumn) { | ||
93 | if ($joinColumn->name === $columnName) { | ||
94 | $targetColumnName = $joinColumn->referencedColumnName; | ||
95 | $targetClass = $em->getClassMetadata($assoc->targetEntity); | ||
96 | |||
97 | return self::getTypeOfColumn($targetColumnName, $targetClass, $em); | ||
98 | } | ||
99 | } | ||
100 | } | ||
101 | |||
102 | throw new RuntimeException(sprintf( | ||
103 | 'Could not resolve type of column "%s" of class "%s"', | ||
104 | $columnName, | ||
105 | $class->getName(), | ||
106 | )); | ||
107 | } | ||
108 | } | ||