Completed
Pull Request — master (#61)
by Romain
02:16
created

FormObject::setConfigurationArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1

1 Method

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