-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextControl.php
More file actions
264 lines (220 loc) · 7.31 KB
/
Copy pathTextControl.php
File metadata and controls
264 lines (220 loc) · 7.31 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
namespace ADT\DoctrineForms\Controls;
use ADT\DoctrineComponents\Entities\Entity;
use ADT\DoctrineForms\EntityFormMapper;
use ADT\DoctrineForms\IComponentMapper;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use Exception;
use Nette\ComponentModel\Component;
use Nette\Forms\Controls\BaseControl;
use Nette\Forms\Controls\ChoiceControl;
use Nette\Forms\Controls\MultiChoiceControl;
use ReflectionException;
use ReflectionProperty;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Doctrine\Common\Collections\ArrayCollection;
use UnitEnum;
class TextControl implements IComponentMapper
{
private EntityFormMapper $mapper;
private ?PropertyAccessor $accessor;
private EntityManagerInterface $em;
public function __construct(EntityFormMapper $mapper)
{
$this->mapper = $mapper;
$this->em = $this->mapper->getEntityManager();
$this->accessor = $mapper->getAccessor();
}
/**
* @throws ReflectionException
* @throws MappingException
*/
public function load(ClassMetadata $meta, Component $component, Entity $entity): bool
{
if (!$component instanceof BaseControl) {
return FALSE;
}
if ($callback = $this->mapper->getForm()->getComponentFormMapper($component)) {
$callback($this->mapper, $component, $entity);
return true;
}
if ($meta->hasField($name = $component->getOption(self::FIELD_NAME) ?? $component->getName())) {
$reflectionProperty = new ReflectionProperty(get_class($entity), $name);
$gettedValue = !$entity->isNew() || $reflectionProperty->isInitialized($entity) ? $reflectionProperty->getValue($entity) : null;
if ($component->getOption('hidden') !== true) {
$component->setDefaultValue($gettedValue instanceof UnitEnum ? $gettedValue->value : $gettedValue);
}
return TRUE;
}
if (!$meta->hasAssociation($name)) {
return FALSE;
}
$this->setItems($component, $entity, $name);
/** @var MultiChoiceControl $component */
if ($component instanceof MultiChoiceControl) {
if (!$collection = $this->getCollection($meta, $entity, $component->getName())) {
return FALSE;
}
$UoW = $this->em->getUnitOfWork();
$value = [];
foreach ($collection as $relation) {
$value[] = $UoW->getSingleIdentifierValue($relation);
}
$component->setDefaultValue($value);
} else {
$reflectionProperty = new ReflectionProperty(get_class($entity), $name);
$relation = $reflectionProperty->isInitialized($entity) ? $reflectionProperty->getValue($entity) : null;
if ($relation) {
$UoW = $this->em->getUnitOfWork();
$component->setDefaultValue($UoW->getSingleIdentifierValue($relation));
}
}
return TRUE;
}
private function relatedMetadata(Entity $entity, string $relationName): ClassMetadata
{
$meta = $this->em->getClassMetadata(get_class($entity));
$targetClass = $meta->getAssociationTargetClass($relationName);
return $this->em->getClassMetadata($targetClass);
}
/**
* @param ClassMetadata $meta
* @param $criteria
* @param $orderBy
* @param $nameKey
* @return array
* @throws MappingException
*/
private function findPairs(ClassMetadata $meta, $criteria, $orderBy, $nameKey): array
{
$repository = $this->em->getRepository($meta->getName());
$items = array();
$idKey = $meta->getSingleIdentifierFieldName();
foreach ($repository->findBy($criteria, $orderBy) as $entity) {
$items[$this->accessor->getValue($entity, $idKey)] = is_callable($nameKey)
? $nameKey($entity)
: $this->accessor->getValue($entity, $nameKey);
}
return $items;
}
/**
* @param ClassMetadata $meta
* @param Component $component
* @param $entity
* @return bool
* @throws Exception
*/
public function save(ClassMetadata $meta, Component $component, $entity): bool
{
if (!$component instanceof BaseControl) {
return false;
}
if ($callback = $this->mapper->getForm()->getComponentEntityMapper($component)) {
$callback($this->mapper, $component, $entity);
return true;
}
if ($meta->hasField($name = $component->getOption(self::FIELD_NAME) ?? $component->getName())) {
$value = $component->getValue();
if ($meta->isNullable($component->getName()) && $value === '' || $component->getOption('hidden') === true) {
$value = NULL;
}
$value = $this->getEnumOrValue(get_class($entity), $name, $value);
$this->accessor->setValue($entity, $name, $value);
return true;
}
// sometimes we want to save some metadata to an entity,
// for example, base64 of an image cropped in a browser
if (
!$meta->hasAssociation($name)
&&
property_exists($entity, $component->getName())
) {
$this->accessor->setValue($entity, $name, $component->getValue());
return true;
}
if (!$meta->hasAssociation($name)) {
return false;
}
$this->setItems($component, $entity, $name);
$identifier = $component->getValue();
$repository = $this->em->getRepository($this->relatedMetadata($entity, $name)->getName());
/** @var MultiChoiceControl $component */
if ($component instanceof MultiChoiceControl) {
if (!$collection = $this->getCollection($meta, $entity, $component->getName())) {
return false;
}
$collectionByIds = [];
foreach ($collection as $i) {
$collectionByIds[] = $i->getId();
}
$identifiers = $identifier ?: [];
$received = [];
foreach ($identifiers as $identifier) {
if (empty($identifier)) continue;
if (!in_array($identifier, $collectionByIds)) { // entity was added from the client
$collection[] = $repository->find($identifier);
}
$received[] = $identifier;
}
foreach ($collection as $key => $relation) {
if (!in_array($relation->getId(), $received)) {
unset($collection[$key]);
}
}
} else {
$this->accessor->setValue($entity, $name, $identifier ? $repository->find($identifier) : null);
}
return TRUE;
}
private function getCollection(ClassMetadata $meta, Entity $entity, string $field)
{
if (!$meta->hasAssociation($field) || $meta->isSingleValuedAssociation($field)) {
return false;
}
$collection = $meta->getFieldValue($entity, $field);
if ($collection === NULL) {
$collection = new ArrayCollection();
$meta->setFieldValue($entity, $field, $collection);
}
return $collection;
}
/**
* @throws MappingException
*/
private function setItems(Component $component, Entity $entity, string $name): void
{
/** @var ChoiceControl|MultiChoiceControl $component */
if (
($component instanceof ChoiceControl || $component instanceof MultiChoiceControl)
&&
!count($component->getItems())
&&
($nameKey = $component->getOption(self::ITEMS_TITLE) ?? FALSE)
) {
$criteria = $component->getOption(self::ITEMS_FILTER) ?? array();
$orderBy = $component->getOption(self::ITEMS_ORDER) ?? array();
$related = $this->relatedMetadata($entity, $name);
$items = $this->findPairs($related, $criteria, $orderBy, $nameKey);
$component->setItems($items);
}
}
/**
* @throws ReflectionException
*/
private function getEnumOrValue(string $class, string $property, $value)
{
$reflectionProperty = new ReflectionProperty($class, $property);
$type = $reflectionProperty->getType();
if ($type && !$type->isBuiltin()) {
$enumType = $type->getName();
if (is_subclass_of($enumType, UnitEnum::class)) {
return $value ? $enumType::from($value) : null;
} else {
return $value;
}
}
return $value;
}
}