Completed
Push — middleware-wip ( e0880a...664dc8 )
by Romain
02:51
created

FormObject::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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