Completed
Push — task/hash-handling ( e82a8d )
by Romain
02:34
created

FormObject::setUpConfiguration()   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 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 FormObjectHash
68
     */
69
    protected $hashService;
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->configuration = Core::instantiate(FormObjectConfiguration::class, $this, $formConfiguration);
84
        $this->hashService = Core::instantiate(FormObjectHash::class, $this);
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->hashService->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 a unique hash for this form object.
227
     *
228
     * @return string
229
     */
230
    public function getHash()
231
    {
232
        return $this->hashService->getHash();
233
    }
234
235
    /**
236
     * When this instance is saved in TYPO3 cache, we need not to store all the
237
     * properties to increase performance.
238
     *
239
     * @return array
240
     */
241
    public function __sleep()
242
    {
243
        return ['name', 'className', 'properties', 'hashService', 'configuration'];
244
    }
245
}
246