Completed
Push — middleware-wip ( 846df6...f76eb7 )
by Romain
05:21
created

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