1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Utils\Entity; |
4
|
|
|
|
5
|
|
|
use Cdf\BiCoreBundle\Utils\String\StringUtils; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use Doctrine\Common\Proxy\Proxy; |
8
|
|
|
use function count; |
9
|
|
|
|
10
|
|
|
class EntityUtils |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
private EntityManagerInterface $em; |
14
|
|
|
|
15
|
18 |
|
public function __construct(EntityManagerInterface $em) |
16
|
|
|
{ |
17
|
18 |
|
$this->em = $em; |
18
|
|
|
} |
19
|
|
|
|
20
|
5 |
|
public function getClassNameToShortcutNotations(string $entity): string |
21
|
|
|
{ |
22
|
5 |
|
$cleanClassName = str_replace('\\Entity', '\:', $entity); |
23
|
5 |
|
$parts = explode('\\', $cleanClassName); |
24
|
5 |
|
return implode('', $parts); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
* @param string $entity |
30
|
|
|
* @return array<mixed> |
31
|
|
|
*/ |
32
|
12 |
|
public function getEntityColumns(string $entity): array |
33
|
|
|
{ |
34
|
|
|
/* @phpstan-ignore-next-line */ |
35
|
12 |
|
$infocolonne = $this->em->getMetadataFactory()->getMetadataFor($entity); |
36
|
12 |
|
$colonne = array(); |
37
|
12 |
|
$fieldMappings = $infocolonne->fieldMappings; |
38
|
|
|
//dump($fieldMappings); |
39
|
12 |
|
foreach ($fieldMappings as $colonna) { |
40
|
12 |
|
$colonne[$colonna['fieldName']] = $colonna; |
41
|
12 |
|
$colonne[$colonna['fieldName']]['entityClass'] = $entity; |
42
|
|
|
} |
43
|
12 |
|
$joinTables = $this->getEntityJoinTables($entity); |
44
|
12 |
|
foreach ($joinTables as $entityjoin => $entityproperties) { |
45
|
6 |
|
$key = $entityproperties['entity']['fieldName']; |
46
|
6 |
|
$colonne[$key]['fieldName'] = $key; |
47
|
6 |
|
$colonne[$key]['columnName'] = $key; |
48
|
6 |
|
$colonne[$key]['entityClass'] = $entityjoin; |
49
|
6 |
|
$colonne[$key]['sourceEntityClass'] = $entity; |
50
|
6 |
|
$colonne[$key]['association'] = true; |
51
|
6 |
|
$colonne[$key]['associationtable'] = $entityproperties['entity']; |
52
|
|
|
} |
53
|
12 |
|
return $colonne; |
54
|
|
|
} |
55
|
|
|
|
56
|
11 |
|
public function getTableFromEntity(string $entity): string |
57
|
|
|
{ |
58
|
11 |
|
$metadata = $this->em->getClassMetadata($entity); |
59
|
11 |
|
$tablename = $metadata->getTablename(); |
60
|
|
|
|
61
|
11 |
|
return $tablename; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function entityHasJoinTables(string $entityclass): bool |
65
|
|
|
{ |
66
|
|
|
$jointables = $this->getEntityJoinTables($entityclass); |
67
|
|
|
|
68
|
|
|
return count($jointables) > 0 ? true : false; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
public function getJoinTableField(string $entityclass, string $field): ?string |
72
|
|
|
{ |
73
|
2 |
|
$joinfields = $this->getEntityJoinTables($entityclass); |
74
|
2 |
|
foreach ($joinfields as $joinfield) { |
75
|
2 |
|
if (1 != count($joinfield)) { |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
2 |
|
$jointableentity = $this->getJoinTable($joinfield, $field); |
79
|
2 |
|
if ($jointableentity) { |
80
|
2 |
|
return $jointableentity; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
public function getJoinTableFieldProperty(string $entityclass, string $field): ?string |
88
|
|
|
{ |
89
|
2 |
|
$joinfields = $this->getEntityJoinTables($entityclass); |
90
|
2 |
|
foreach ($joinfields as $joinfield) { |
91
|
2 |
|
if (1 != count($joinfield)) { |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
2 |
|
$joinfieldname = $this->getJoinFieldName($joinfield, $field); |
95
|
2 |
|
if ($joinfieldname) { |
96
|
2 |
|
return $joinfieldname; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
return null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* |
105
|
|
|
* @param string $fieldname |
106
|
|
|
* @param mixed $objrecord |
107
|
|
|
* @return array<mixed> |
108
|
|
|
*/ |
109
|
3 |
|
public function getEntityProperties(string $fieldname, $objrecord): array |
110
|
|
|
{ |
111
|
3 |
|
$parametri = array('str' => $fieldname, 'primamaiuscola' => true); |
112
|
3 |
|
$getfieldname = StringUtils::toCamelCase($parametri); |
113
|
3 |
|
if (!method_exists($objrecord, $getfieldname)) { |
114
|
3 |
|
$getfieldname = 'has' . StringUtils::toCamelCase($parametri); |
115
|
3 |
|
if (!method_exists($objrecord, $getfieldname)) { |
116
|
3 |
|
$getfieldname = 'is' . StringUtils::toCamelCase($parametri); |
117
|
3 |
|
if (!method_exists($objrecord, $getfieldname)) { |
118
|
3 |
|
$getfieldname = 'get' . StringUtils::toCamelCase($parametri); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
3 |
|
$setfieldname = 'set' . StringUtils::toCamelCase($parametri); |
123
|
|
|
|
124
|
3 |
|
return array('get' => $getfieldname, 'set' => $setfieldname); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* |
129
|
|
|
* @param mixed $className |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
1 |
|
public function entityExists($className): bool |
133
|
|
|
{ |
134
|
1 |
|
if (is_object($className)) { |
135
|
|
|
$className = ($className instanceof Proxy) ? get_parent_class($className) : get_class($className); |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
return !$this->em->getMetadataFactory()->isTransient($className); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* |
143
|
|
|
* @param string $entityclass |
144
|
|
|
* @return array<mixed> |
145
|
|
|
*/ |
146
|
14 |
|
public function getEntityJoinTables(string $entityclass): array |
147
|
|
|
{ |
148
|
14 |
|
$jointables = array(); |
149
|
14 |
|
$metadata = $this->em->getClassMetadata($entityclass); |
150
|
14 |
|
$fielsassoc = $metadata->associationMappings; |
151
|
14 |
|
foreach ($fielsassoc as $tableassoc) { |
152
|
12 |
|
if ($tableassoc['inversedBy']) { |
153
|
8 |
|
$jointables[$tableassoc['targetEntity']] = array('entity' => $tableassoc); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
14 |
|
return $jointables; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* |
162
|
|
|
* @param array<mixed> $joinfield |
163
|
|
|
* @param string $field |
164
|
|
|
* @return string|null |
165
|
|
|
*/ |
166
|
2 |
|
private function getJoinFieldName(array $joinfield, string $field): ?string |
167
|
|
|
{ |
168
|
2 |
|
$joinFieldentity = $joinfield['entity']; |
169
|
2 |
|
$joinColumns = $joinFieldentity['joinColumns']; |
170
|
2 |
|
foreach ($joinColumns as $joinColumn) { |
171
|
2 |
|
if ($field === $joinColumn['name']) { |
172
|
2 |
|
$joinFieldName = $joinFieldentity['fieldName']; |
173
|
|
|
|
174
|
2 |
|
return $joinFieldName; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
2 |
|
return null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* |
183
|
|
|
* @param array<mixed> $joinfield |
184
|
|
|
* @param string $field |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
2 |
|
private function getJoinTable(array $joinfield, string $field): ?string |
188
|
|
|
{ |
189
|
2 |
|
$joinTableEntity = $joinfield['entity']; |
190
|
2 |
|
$joinColumns = $joinTableEntity['joinColumns']; |
191
|
2 |
|
foreach ($joinColumns as $joinColumn) { |
192
|
2 |
|
if ($field === $joinColumn['name']) { |
193
|
2 |
|
return $joinTableEntity['targetEntity']; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
2 |
|
return null; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|