1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Form\FormObject; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Configuration\Form\Form; |
17
|
|
|
use Romm\Formz\Core\Core; |
18
|
|
|
use Romm\Formz\Domain\Model\FormMetadata; |
19
|
|
|
use Romm\Formz\Error\FormResult; |
20
|
|
|
use Romm\Formz\Exceptions\PropertyNotAccessibleException; |
21
|
|
|
use Romm\Formz\Form\FormInterface; |
22
|
|
|
use Romm\Formz\Form\Service\FormObjectMetadata; |
23
|
|
|
use Romm\Formz\Form\Service\FormObjectRequestData; |
24
|
|
|
use Romm\Formz\Persistence\PersistenceManager; |
25
|
|
|
use TYPO3\CMS\Extbase\Error\Result; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* This is the object representation of a form. In here we can manage which |
29
|
|
|
* properties the form does have, its configuration, and more. |
30
|
|
|
*/ |
31
|
|
|
class FormObject |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $name; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var FormObjectStatic |
40
|
|
|
*/ |
41
|
|
|
protected $static; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var FormObjectProxy |
45
|
|
|
*/ |
46
|
|
|
protected $proxy; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var FormObjectFactory |
50
|
|
|
*/ |
51
|
|
|
protected $formObjectFactory; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var PersistenceManager |
55
|
|
|
*/ |
56
|
|
|
protected $persistenceManager; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* You should never create a new instance of this class directly, use the |
60
|
|
|
* `FormObjectFactory->getInstanceFromClassName()` function instead. |
61
|
|
|
* |
62
|
|
|
* @param string $name |
63
|
|
|
* @param FormObjectStatic $static |
64
|
|
|
*/ |
65
|
|
|
public function __construct($name, FormObjectStatic $static) |
66
|
|
|
{ |
67
|
|
|
$this->name = $name; |
68
|
|
|
$this->static = $static; |
69
|
|
|
|
70
|
|
|
$this->metadata = Core::instantiate(FormObjectMetadata::class, $this); |
|
|
|
|
71
|
|
|
$this->persistenceManager = Core::instantiate(PersistenceManager::class, $this); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getName() |
78
|
|
|
{ |
79
|
|
|
return $this->name; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getClassName() |
86
|
|
|
{ |
87
|
|
|
return $this->static->getClassName(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Form |
92
|
|
|
*/ |
93
|
|
|
public function getConfiguration() |
94
|
|
|
{ |
95
|
|
|
return $this->static->getConfiguration(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return Result |
100
|
|
|
*/ |
101
|
|
|
public function getConfigurationValidationResult() |
102
|
|
|
{ |
103
|
|
|
return $this->static->getConfigurationValidationResult(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return FormInterface |
108
|
|
|
*/ |
109
|
|
|
public function getForm() |
110
|
|
|
{ |
111
|
|
|
return $this->getProxy()->getForm(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function hasForm() |
118
|
|
|
{ |
119
|
|
|
return $this->proxy !== null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param FormInterface $form |
124
|
|
|
*/ |
125
|
|
|
public function setForm(FormInterface $form) |
126
|
|
|
{ |
127
|
|
|
$this->proxy = $this->formObjectFactory->getProxy($this, $form); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Will mark the form as submitted (change the result returned by the |
132
|
|
|
* function `formWasSubmitted()`). |
133
|
|
|
*/ |
134
|
|
|
public function markFormAsSubmitted() |
135
|
|
|
{ |
136
|
|
|
$this->getProxy()->markFormAsSubmitted(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns `true` if the form was submitted by the user. |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function formWasSubmitted() |
145
|
|
|
{ |
146
|
|
|
return $this->hasForm() && $this->getProxy()->formWasSubmitted(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return FormResult |
151
|
|
|
*/ |
152
|
|
|
public function getFormResult() |
153
|
|
|
{ |
154
|
|
|
return $this->getProxy()->getFormResult(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
|
|
public function hasFormResult() |
161
|
|
|
{ |
162
|
|
|
return $this->getProxy()->hasFormResult(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param FormResult $formResult |
167
|
|
|
*/ |
168
|
|
|
public function setFormResult(FormResult $formResult) |
169
|
|
|
{ |
170
|
|
|
$this->getProxy()->setFormResult($formResult); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return FormObjectRequestData |
175
|
|
|
*/ |
176
|
|
|
public function getRequestData() |
177
|
|
|
{ |
178
|
|
|
return $this->getProxy()->getRequestData(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return FormMetadata |
183
|
|
|
*/ |
184
|
|
|
public function getMetadata() |
185
|
|
|
{ |
186
|
|
|
return $this->getProxy()->getMetadata(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param FormMetadata $metadata |
191
|
|
|
*/ |
192
|
|
|
public function setMetadata(FormMetadata $metadata) |
193
|
|
|
{ |
194
|
|
|
$this->getProxy()->setMetadata($metadata); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function getFormHash() |
201
|
|
|
{ |
202
|
|
|
return $this->getProxy()->getFormHash(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns a unique hash for this form object. |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
public function getObjectHash() |
211
|
|
|
{ |
212
|
|
|
return $this->static->getObjectHash(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return PersistenceManager |
217
|
|
|
*/ |
218
|
|
|
public function getPersistenceManager() |
219
|
|
|
{ |
220
|
|
|
return $this->persistenceManager; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return FormObjectProxy |
225
|
|
|
* @throws PropertyNotAccessibleException |
226
|
|
|
*/ |
227
|
|
|
protected function getProxy() |
228
|
|
|
{ |
229
|
|
|
if (null === $this->proxy) { |
230
|
|
|
throw PropertyNotAccessibleException::formInstanceNotSet(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $this->proxy; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param FormObjectFactory $formObjectFactory |
238
|
|
|
*/ |
239
|
|
|
public function injectFormObjectFactory(FormObjectFactory $formObjectFactory) |
240
|
|
|
{ |
241
|
|
|
$this->formObjectFactory = $formObjectFactory; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: