-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseForm.php
More file actions
139 lines (116 loc) · 3.31 KB
/
Copy pathBaseForm.php
File metadata and controls
139 lines (116 loc) · 3.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
<?php
namespace ADT\DoctrineForms;
use ADT\DoctrineComponents\Entities\Entity;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\UnitOfWork;
use Exception;
/**
* @method onAfterMapToForm($form, Entity $entity)
* @method onAfterMapToEntity($form)
*/
abstract class BaseForm extends \ADT\Forms\BaseForm implements BaseFormInterface
{
abstract protected function getEntityManager(): EntityManagerInterface;
/** @var null|callable|Entity */
private $entity = null;
/**
* @internal
* @var callable[]
*/
public array $onAfterMapToForm = [];
/**
* @internal
* @var callable[]
*/
public array $onAfterMapToEntity = [];
public function __construct()
{
parent::__construct();
$this->setOnBeforeInitForm(function(Form $form) {
if ($this->getEntity()) {
$form->setEntity($this->entity);
}
});
// we don't call setter intentionally to avoid logic in setter
$this->onAfterInitForm[] = [$this, 'initOnAfterMapToForm'];
$this->setOnBeforeProcessForm(function(Form $form) {
if (!$this->getEntity()) {
if (is_callable($this->entity)) {
$this->entity = $this->invokeHandler($this->entity, $form->getValues());
} elseif (method_exists($this, 'createEntity')) {
$this->entity = $this->invokeHandler([$this, 'createEntity'], $form->getValues());
}
if ($this->entity) {
if (method_exists($this, 'initEntity')) {
$this->invokeHandler([$this, 'initEntity'], $form->getValues());
}
$form->setEntity($this->entity);
$this->getEntityManager()->persist($this->entity);
}
}
if ($this->form->getEntity()) {
$form->mapToEntity();
$this->onAfterMapToEntity($form);
}
});
$this->paramResolvers[] = function(string $type) {
if ($this->getEntity() && $this->getEntity() instanceof $type) {
return $this->getEntity();
} elseif ($type) {
return null;
}
return false;
};
}
// we need to call initOnAfterMapToForm last,
// so we will remove initOnAfterMapToForm, add callback and add initOnAfterMapToForm again
public function setOnAfterInitForm(callable $onAfterInitForm): static
{
array_pop($this->onAfterInitForm);
$this->onAfterInitForm[] = $onAfterInitForm;
$this->onAfterInitForm[] = [$this, 'initOnAfterMapToForm'];
return $this;
}
public function setOnAfterMapToForm(callable $onAfterMapToForm): static
{
$this->onAfterMapToForm[] = $onAfterMapToForm;
return $this;
}
public function setOnAfterMapToEntity(callable $onAfterMapToEntity): static
{
$this->onAfterMapToEntity[] = $onAfterMapToEntity;
return $this;
}
/**
* @throws Exception
* @internal
*/
public function initOnAfterMapToForm(Form $form): void
{
if ($this->getEntity()) {
$form->mapToForm();
$this->onAfterMapToForm($form, $this->getEntity());
}
}
/**
* @throws Exception
*/
final public function setEntity(Entity|callable|null $entity): static
{
if ($entity && !is_callable($entity)) {
if ($this->getEntityManager()->getUnitOfWork()->getEntityState($entity) === UnitOfWork::STATE_NEW) {
throw new Exception('Pass the new entity as a callback.');
}
}
$this->entity = $entity;
return $this;
}
protected function createComponentForm()
{
return new Form();
}
final protected function getEntity(): callable|Entity|null
{
return $this->entity && !is_callable($this->entity) ? $this->entity : null;
}
}