Completed
Push — middleware-wip ( 48fd00...ab9573 )
by Romain
05:02
created

FormObject::createProxy()   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\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\FormObject\Service\FormObjectRequestData;
23
use Romm\Formz\Persistence\PersistenceManager;
24
use TYPO3\CMS\Extbase\Error\Result;
25
26
/**
27
 * This is the object representation of a form. In here we can manage which
28
 * properties the form does have, its configuration, and more.
29
 */
30
class FormObject
31
{
32
    /**
33
     * @var string
34
     */
35
    protected $name;
36
37
    /**
38
     * @var FormObjectStatic
39
     */
40
    protected $static;
41
42
    /**
43
     * @var FormObjectProxy
44
     */
45
    protected $proxy;
46
47
    /**
48
     * @var FormObjectFactory
49
     */
50
    protected $formObjectFactory;
51
52
    /**
53
     * @var PersistenceManager
54
     */
55
    protected $persistenceManager;
56
57
    /**
58
     * You should never create a new instance of this class directly, use the
59
     * `FormObjectFactory->getInstanceFromClassName()` function instead.
60
     *
61
     * @param string           $name
62
     * @param FormObjectStatic $static
63
     */
64
    public function __construct($name, FormObjectStatic $static)
65
    {
66
        $this->name = $name;
67
        $this->static = $static;
68
69
        $this->persistenceManager = Core::instantiate(PersistenceManager::class, $this);
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getName()
76
    {
77
        return $this->name;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function getClassName()
84
    {
85
        return $this->static->getClassName();
86
    }
87
88
    /**
89
     * @return Form
90
     */
91
    public function getConfiguration()
92
    {
93
        return $this->static->getConfiguration();
94
    }
95
96
    /**
97
     * @return Result
98
     */
99
    public function getConfigurationValidationResult()
100
    {
101
        return $this->static->getConfigurationValidationResult();
102
    }
103
104
    /**
105
     * @return FormInterface
106
     */
107
    public function getForm()
108
    {
109
        return $this->getProxy()->getForm();
110
    }
111
112
    /**
113
     * @return bool
114
     */
115
    public function hasForm()
116
    {
117
        return $this->proxy !== null;
118
    }
119
120
    /**
121
     * @param FormInterface $form
122
     */
123
    public function setForm(FormInterface $form)
124
    {
125
        $this->proxy = $this->createProxy($form);
126
    }
127
128
    /**
129
     * Will mark the form as submitted (change the result returned by the
130
     * function `formWasSubmitted()`).
131
     */
132
    public function markFormAsSubmitted()
133
    {
134
        $this->getProxy()->markFormAsSubmitted();
135
    }
136
137
    /**
138
     * Returns `true` if the form was submitted by the user.
139
     *
140
     * @return bool
141
     */
142
    public function formWasSubmitted()
143
    {
144
        return $this->hasForm() && $this->getProxy()->formWasSubmitted();
145
    }
146
147
    /**
148
     * @return FormResult
149
     */
150
    public function getFormResult()
151
    {
152
        return $this->getProxy()->getFormResult();
153
    }
154
155
    /**
156
     * @return bool
157
     */
158
    public function hasFormResult()
159
    {
160
        return $this->getProxy()->hasFormResult();
161
    }
162
163
    /**
164
     * @param FormResult $formResult
165
     */
166
    public function setFormResult(FormResult $formResult)
167
    {
168
        $this->getProxy()->setFormResult($formResult);
169
    }
170
171
    /**
172
     * @return FormObjectRequestData
173
     */
174
    public function getRequestData()
175
    {
176
        return $this->getProxy()->getRequestData();
177
    }
178
179
    /**
180
     * @return FormMetadata
181
     */
182
    public function getFormMetadata()
183
    {
184
        return $this->getProxy()->getFormMetadata();
185
    }
186
187
    /**
188
     * @return string
189
     */
190
    public function getFormHash()
191
    {
192
        return $this->getProxy()->getFormHash();
193
    }
194
195
    /**
196
     * @param string $hash
197
     */
198
    public function setFormHash($hash)
199
    {
200
        $this->getProxy()->setFormHash($hash);
201
    }
202
203
    /**
204
     * Returns a unique hash for this form object.
205
     *
206
     * @return string
207
     */
208
    public function getObjectHash()
209
    {
210
        return $this->static->getObjectHash();
211
    }
212
213
    /**
214
     * @return PersistenceManager
215
     */
216
    public function getPersistenceManager()
217
    {
218
        return $this->persistenceManager;
219
    }
220
221
    /**
222
     * @return FormObjectProxy
223
     * @throws PropertyNotAccessibleException
224
     */
225
    protected function getProxy()
226
    {
227
        if (null === $this->proxy) {
228
            throw PropertyNotAccessibleException::formInstanceNotSet();
229
        }
230
231
        return $this->proxy;
232
    }
233
234
    /**
235
     * Wrapper for unit tests.
236
     *
237
     * @param FormInterface $form
238
     * @return FormObjectProxy
239
     */
240
    protected function createProxy(FormInterface $form)
241
    {
242
        return $this->formObjectFactory->getProxy($this, $form);
243
    }
244
245
    /**
246
     * @param FormObjectFactory $formObjectFactory
247
     */
248
    public function injectFormObjectFactory(FormObjectFactory $formObjectFactory)
249
    {
250
        $this->formObjectFactory = $formObjectFactory;
251
    }
252
}
253