-
Notifications
You must be signed in to change notification settings - Fork 57
/
EmbeddedListHelper.php
145 lines (125 loc) · 4.44 KB
/
EmbeddedListHelper.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace AlterPHP\EasyAdminExtensionBundle\Helper;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Symfony\Component\PropertyAccess\PropertyAccess;
/**
* This file is part of the EasyAdmin Extension package.
*/
class EmbeddedListHelper
{
/**
* @var ManagerRegistry
*/
private $doctrine;
/**
* @var array
*/
private $easyAdminConfig;
/**
* @param ManagerRegistry $doctrine
* @param array $easyAdminConfig
*/
public function __construct(ManagerRegistry $doctrine, array $easyAdminConfig)
{
$this->doctrine = $doctrine;
$this->easyAdminConfig = $easyAdminConfig;
}
/**
* Returns EasyAdmin entity entry name for a parent FQCN and property for embedded list.
*
* @param string $parentFqcn
* @param string $parentProperty
*
* @return mixed
*
* @throws \RuntimeException
*/
public function getEntityFqcnFromParent(string $parentFqcn, string $parentProperty)
{
$parentClassMetadata = $this->doctrine->getManagerForClass($parentFqcn)->getClassMetadata($parentFqcn);
// Required to use getAssociationMappings method
if (!$parentClassMetadata instanceof ClassMetadataInfo) {
return;
}
try {
$entityFqcn = $parentClassMetadata->getAssociationTargetClass($parentProperty);
} catch (\Exception $e) {
return;
}
return $entityFqcn;
}
/**
* Returns EasyAdmin entity entry name for a FQCN.
*
* @param string $entityFqcn
*
* @return string
*
* @throws \RuntimeException
*/
public function guessEntityEntry(string $entityFqcn)
{
$matchingEntityConfigs = array_filter(
$this->easyAdminConfig['entities'],
function ($entityConfig) use ($entityFqcn) {
return $entityFqcn === $entityConfig['class'];
}
);
if (empty($matchingEntityConfigs)) {
throw new \RuntimeException(
sprintf('No entity defined in EasyAdmin configuration matches %s FQCN.', $entityFqcn)
);
}
if (1 < count($matchingEntityConfigs)) {
throw new \RuntimeException(
sprintf('More than 1 entity defined in EasyAdmin configuration matches %s FQCN.', $entityFqcn)
);
}
return (string) key($matchingEntityConfigs);
}
/**
* Returns default filter for embeddedList.
*
* @param string $entityFqcn
* @param string $parentEntityProperty
* @param object $parentEntity
*
* @return array
*/
public function guessDefaultFilter(string $entityFqcn, string $parentEntityProperty, $parentEntity)
{
$entityClassMetadata = $this->doctrine->getManagerForClass($entityFqcn)->getClassMetadata($entityFqcn);
// Required to use getAssociationMappings method
if (!$entityClassMetadata instanceof ClassMetadataInfo) {
return [];
}
$entityAssociations = $entityClassMetadata->getAssociationMappings();
$parentEntityFqcn = get_class($parentEntity);
foreach ($entityAssociations as $assoc) {
// If association matches embeddedList relation
if ($parentEntityFqcn === $assoc['targetEntity'] && $parentEntityProperty === $assoc['inversedBy']) {
// OneToMany association
if (isset($assoc['joinColumns']) && 1 === count($assoc['joinColumns'])) {
$assocFieldPart = 'entity.'.$assoc['fieldName'];
$assocIdentifierValue = PropertyAccess::createPropertyAccessor()->getValue(
$parentEntity, $assoc['joinColumns'][0]['referencedColumnName']
);
return [$assocFieldPart => $assocIdentifierValue];
}
// ManyToMany association
elseif (isset($assoc['joinTable'])) {
$relatedItems = PropertyAccess::createPropertyAccessor()->getValue(
$parentEntity, $parentEntityProperty
);
$itemIds = $relatedItems->map(function ($entity) {
return $entity->getId();
});
return ['entity.id' => $itemIds->toArray()];
}
}
}
return [];
}
}