-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToOne.php
More file actions
166 lines (141 loc) · 4.05 KB
/
Copy pathToOne.php
File metadata and controls
166 lines (141 loc) · 4.05 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
<?php
namespace ADT\DoctrineForms\Controls;
use ADT\DoctrineComponents\Entities\Entity;
use ADT\Forms\StaticContainer;
use Doctrine\Common\Collections\Collection;
use ADT\DoctrineForms\EntityFormMapper;
use ADT\DoctrineForms\IComponentMapper;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use Nette\ComponentModel\Component;
use ReflectionException;
class ToOne implements IComponentMapper
{
use CreateEntityTrait;
/**
* @var EntityFormMapper
*/
private EntityFormMapper $mapper;
public function __construct(EntityFormMapper $mapper)
{
$this->mapper = $mapper;
}
/**
* @throws ReflectionException
* @throws MappingException
*/
public function load(ClassMetadata $meta, Component $component, $entity): bool
{
if (!$component instanceof StaticContainer) {
return false;
}
if ($callback = $this->mapper->getForm()->getComponentFormMapper($component)) {
$callback($this->mapper, $component, $entity);
return true;
}
if (!$relation = $this->getRelation($meta, $component, $entity)) {
return false;
}
// we have to fill isFilled component value
// if the field is not empty and isFilled component is set
if (
!$relation->isNew()
&&
$component->getIsFilledComponent()
) {
$component->getIsFilledComponent()->setDefaultValue(true);
}
$this->mapper->load($relation, $component);
return true;
}
/**
* @throws ReflectionException
* @throws MappingException
*/
public function save(ClassMetadata $meta, Component $component, Entity $entity): bool
{
if (!$component instanceof StaticContainer) {
return false;
}
if ($callback = $this->mapper->getForm()->getComponentEntityMapper($component)) {
$callback($this->mapper, $component, $entity);
return true;
}
if (!$relation = $this->getRelation($meta, $component, $entity)) {
return false;
}
// we want to delete the entity
// if the field is not empty and isFilled component value is empty or the entire container is empty
if (
!$relation->isNew()
&&
(
$component->getIsFilledComponent() && !$component->getIsFilledComponent()->getValue()
||
$component->isEmpty()
)
) {
$this->removeComponent($meta, $component, $entity);
return true;
}
// we want to delete the entity
// if isFilled component is set and any other container control is filled
// we use this when someone updated the old values with the new ones
elseif (
!$relation->isNew()
&&
$component->getIsFilledComponent()
&&
!$component->isEmpty(excludeIsFilledComponent: true)
) {
$this->removeComponent($meta, $component, $entity);
$relation = $this->getRelation($meta, $component, $entity);
}
// we don't want to create an entity
// if the entire container is empty
elseif (
$relation->isNew()
&&
$component->isEmpty()
) {
$meta->setFieldValue($entity, $component->getName(), null);
return true;
}
$this->mapper->save($relation, $component);
return true;
}
/**
* @throws ReflectionException
* @throws MappingException
*/
private function removeComponent(ClassMetadata $meta, $component, Entity $entity): void
{
$relation = $this->getRelation($meta, $component, $entity);
// we don't want to rely on orphanRemoval
$this->mapper->getEntityManager()->remove($relation);
// this must not be before entity removal
// otherwise the relation is refreshed
$meta->setFieldValue($entity, $component->getName(), null);
}
/**
* @throws ReflectionException
* @throws MappingException
*/
private function getRelation(ClassMetadata $meta, StaticContainer $component, Entity $entity): ?Entity
{
$field = $component->getName();
if (!$meta->hasAssociation($field) || !$meta->isSingleValuedAssociation($field)) {
return null;
}
// todo: allow access using property or method
$relation = $meta->getFieldValue($entity, $field);
if ($relation instanceof Collection) {
return null;
}
if ($relation === NULL) {
$relation = $this->createEntity($meta, $component, $entity);
$meta->setFieldValue($entity, $field, $relation);
}
return $relation;
}
}