Completed
Push — middleware-wip ( 357c0d...3d734d )
by Romain
02:39
created

FormObject::injectRequestData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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;
15
16
use Romm\Formz\Configuration\Form\Form;
17
use Romm\Formz\Core\Core;
18
use Romm\Formz\Error\FormResult;
19
use Romm\Formz\Service\HashService;
20
use TYPO3\CMS\Extbase\Error\Result;
21
22
/**
23
 * This is the object representation of a form. In here we can manage which
24
 * properties the form does have, its configuration, and more.
25
 */
26
class FormObject
27
{
28
    /**
29
     * Name of the form.
30
     *
31
     * @var string
32
     */
33
    protected $name;
34
35
    /**
36
     * @var string
37
     */
38
    protected $className;
39
40
    /**
41
     * The properties of the form.
42
     *
43
     * @var array
44
     */
45
    protected $properties = [];
46
47
    /**
48
     * @var FormObjectConfiguration
49
     */
50
    protected $configuration;
51
52
    /**
53
     * @var FormObjectRequestData
54
     */
55
    protected $requestData;
56
57
    /**
58
     * @var FormInterface
59
     */
60
    protected $form;
61
62
    /**
63
     * @var bool
64
     */
65
    protected $formWasSubmitted = false;
66
67
    /**
68
     * @var FormResult
69
     */
70
    protected $formResult;
71
72
    /**
73
     * @var string
74
     */
75
    protected $hash;
76
77
    /**
78
     * You should never create a new instance of this class directly, use the
79
     * `FormObjectFactory->getInstanceFromClassName()` function instead.
80
     *
81
     * @param string $className
82
     * @param string $name
83
     * @param array  $formConfiguration
84
     */
85
    public function __construct($className, $name, array $formConfiguration)
86
    {
87
        $this->className = $className;
88
        $this->name = $name;
89
        $this->setUpConfiguration($formConfiguration);
90
    }
91
92
    /**
93
     * @todo
94
     */
95
    public function reset()
96
    {
97
        unset($this->form);
98
        $this->formWasSubmitted = false;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getName()
105
    {
106
        return $this->name;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getClassName()
113
    {
114
        return $this->className;
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    public function getProperties()
121
    {
122
        return $this->properties;
123
    }
124
125
    /**
126
     * @param string $name
127
     * @return bool
128
     */
129
    public function hasProperty($name)
130
    {
131
        return in_array($name, $this->properties);
132
    }
133
134
    /**
135
     * Registers a new property for this form.
136
     *
137
     * @param string $name
138
     * @return $this
139
     */
140
    public function addProperty($name)
141
    {
142
        if (false === $this->hasProperty($name)) {
143
            $this->properties[] = $name;
144
            $this->resetHash();
145
        }
146
147
        return $this;
148
    }
149
150
    /**
151
     * @return Form
152
     */
153
    public function getConfiguration()
154
    {
155
        /** @var Form $configuration */
156
        $configuration = $this->configuration->getConfigurationObject()->getObject(true);
157
158
        return $configuration;
159
    }
160
161
    /**
162
     * This function will merge and return the validation results of both the
163
     * global FormZ configuration object, and this form configuration object.
164
     *
165
     * @return Result
166
     */
167
    public function getConfigurationValidationResult()
168
    {
169
        return $this->configuration->getConfigurationValidationResult();
170
    }
171
172
    /**
173
     * @return FormObjectRequestData
174
     */
175
    public function getRequestData()
176
    {
177
        return $this->requestData;
178
    }
179
180
    /**
181
     * @return FormInterface
182
     */
183
    public function getForm()
184
    {
185
        return $this->form;
186
    }
187
188
    /**
189
     * @return bool
190
     */
191
    public function hasForm()
192
    {
193
        return null !== $this->form;
194
    }
195
196
    /**
197
     * @param FormInterface $form
198
     */
199
    public function setForm(FormInterface $form)
200
    {
201
        $this->form = $form;
202
    }
203
204
    /**
205
     * Will mark the form as submitted (change the result returned by the
206
     * function `formWasSubmitted()`).
207
     */
208
    public function markFormAsSubmitted()
209
    {
210
        $this->formWasSubmitted = true;
211
    }
212
213
    /**
214
     * Returns `true` if the form was submitted by the user.
215
     *
216
     * @return bool
217
     */
218
    public function formWasSubmitted()
219
    {
220
        return $this->formWasSubmitted;
221
    }
222
223
    /**
224
     * @return FormResult
225
     */
226
    public function getFormResult()
227
    {
228
        return $this->formResult;
229
    }
230
231
    /**
232
     * @return bool
233
     */
234
    public function hasFormResult()
235
    {
236
        return null !== $this->formResult;
237
    }
238
239
    /**
240
     * @param FormResult $formResult
241
     */
242
    public function setFormResult($formResult)
243
    {
244
        $this->formResult = $formResult;
245
    }
246
247
    /**
248
     * Returns the hash, which should be calculated only once for performance
249
     * concerns.
250
     *
251
     * @return string
252
     */
253
    public function getHash()
254
    {
255
        if (null === $this->hash) {
256
            $this->hash = $this->calculateHash();
257
        }
258
259
        return $this->hash;
260
    }
261
262
    /**
263
     * @param array $formConfiguration
264
     */
265
    protected function setUpConfiguration(array $formConfiguration)
266
    {
267
        $this->configuration = Core::instantiate(FormObjectConfiguration::class, $this, $formConfiguration);
268
    }
269
270
    /**
271
     * Returns the calculated hash of this class.
272
     *
273
     * @return string
274
     */
275
    protected function calculateHash()
276
    {
277
        return HashService::get()->getHash(serialize($this));
278
    }
279
280
    /**
281
     * Resets the hash, which will be calculated on next access.
282
     */
283
    protected function resetHash()
284
    {
285
        $this->hash = null;
286
    }
287
288
    /**
289
     * @param FormObjectRequestData $requestData
290
     */
291
    public function injectRequestData(FormObjectRequestData $requestData)
292
    {
293
        $this->requestData = $requestData;
294
    }
295
296
    /**
297
     * Injects external dependencies.
298
     */
299
    public function __wakeup()
300
    {
301
        $this->requestData = Core::instantiate(FormObjectRequestData::class);
302
    }
303
304
    /**
305
     * When this instance is saved in TYPO3 cache, we need not to store all the
306
     * properties to increase performance.
307
     *
308
     * @return array
309
     */
310
    public function __sleep()
311
    {
312
        return ['name', 'className', 'properties', 'hash', 'configuration'];
313
    }
314
}
315