Completed
Branch unit-test-view-helpers (ccaf92)
by Romain
01:50
created

FormzViewHelperService::setCurrentField()   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
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * 2016 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\ViewHelpers\Service;
15
16
use Romm\Formz\Configuration\Form\Field\Field;
17
use Romm\Formz\Error\FormResult;
18
use Romm\Formz\Form\FormInterface;
19
use Romm\Formz\Form\FormObject;
20
use Romm\Formz\ViewHelpers\FieldViewHelper;
21
use Romm\Formz\ViewHelpers\FormViewHelper;
22
use TYPO3\CMS\Core\SingletonInterface;
23
24
/**
25
 * This class contains methods that help view helpers to manipulate data and
26
 * know more things concerning the current form state.
27
 *
28
 * It is mainly configured inside the `FormViewHelper`, and used in other
29
 * view helpers.
30
 */
31
class FormzViewHelperService implements SingletonInterface
32
{
33
34
    /**
35
     * @var FormzViewHelperService
36
     */
37
    protected static $instance;
38
39
    /**
40
     * @var bool
41
     */
42
    protected $formContext;
43
44
    /**
45
     * @var array|FormInterface
46
     */
47
    protected $formInstance;
48
49
    /**
50
     * @var Field
51
     */
52
    protected $currentField;
53
54
    /**
55
     * @var bool
56
     */
57
    protected $formWasSubmitted;
58
59
    /**
60
     * @var FormResult
61
     */
62
    protected $formResult;
63
64
    /**
65
     * @var FormObject
66
     */
67
    protected $formObject;
68
69
    /**
70
     * Reset every state that can be used by this service.
71
     */
72
    public function resetState()
73
    {
74
        $this->formContext = false;
75
        $this->formInstance = null;
76
        $this->formResult = null;
77
        $this->formWasSubmitted = false;
78
        $this->currentField = null;
79
    }
80
81
    /**
82
     * Will activate the form context, changing the result returned by the
83
     * function `formContextExists`.
84
     *
85
     * @throws \Exception
86
     */
87
    public function activateFormContext()
88
    {
89
        if (true === $this->formContext) {
90
            throw new \Exception(
91
                'You can not use a form view helper inside another one.',
92
                1465242575
93
            );
94
        }
95
96
        $this->formContext = true;
97
    }
98
99
    /**
100
     * Returns `true` if the `FormViewHelper` context exists.
101
     *
102
     * @return bool
103
     */
104
    public function formContextExists()
105
    {
106
        return $this->formContext;
107
    }
108
109
    /**
110
     * Will mark the form as submitted (change the result returned by the
111
     * function `formWasSubmitted()`).
112
     */
113
    public function markFormAsSubmitted()
114
    {
115
        $this->formWasSubmitted = true;
116
    }
117
118
    /**
119
     * Returns `true` if the form was submitted by the user.
120
     *
121
     * @return bool
122
     */
123
    public function formWasSubmitted()
124
    {
125
        return $this->formWasSubmitted;
126
    }
127
128
    /**
129
     * Checks that the `FieldViewHelper` has been called. If not, an exception
130
     * is thrown.
131
     *
132
     * @return bool
133
     */
134
    public function fieldContextExists()
135
    {
136
        return $this->currentField instanceof Field;
137
    }
138
139
    /**
140
     * Returns the current field which was defined by the `FieldViewHelper`.
141
     *
142
     * Returns null if no current field was found.
143
     *
144
     * @return Field|null
145
     */
146
    public function getCurrentField()
147
    {
148
        return $this->currentField;
149
    }
150
151
    /**
152
     * @param Field $field
153
     */
154
    public function setCurrentField(Field $field)
155
    {
156
        $this->currentField = $field;
157
    }
158
159
    /**
160
     * Unset the current field.
161
     */
162
    public function removeCurrentField()
163
    {
164
        $this->currentField = null;
165
    }
166
167
    /**
168
     * Checks that the current `FormViewHelper` exists. If not, an exception is
169
     * thrown.
170
     *
171
     * @throws \Exception
172
     */
173
    public function checkIsInsideFormViewHelper()
174
    {
175
        if (false === $this->formContextExists()) {
176
            throw new \Exception(
177
                'The view helper "' . get_called_class() . '" must be used inside the view helper "' . FormViewHelper::class . '".',
178
                1465243085
179
            );
180
        }
181
    }
182
183
    /**
184
     * Checks that the `FieldViewHelper` has been called. If not, an exception
185
     * is thrown.
186
     *
187
     * @throws \Exception
188
     */
189
    public function checkIsInsideFieldViewHelper()
190
    {
191
        if (false === $this->fieldContextExists()) {
192
            throw new \Exception(
193
                'The view helper "' . get_called_class() . '" must be used inside the view helper "' . FieldViewHelper::class . '".',
194
                1465243085
195
            );
196
        }
197
    }
198
199
    /**
200
     * If the form was submitted by the user, contains the array containing the
201
     * submitted values.
202
     *
203
     * @param array|FormInterface $formInstance
204
     */
205
    public function setFormInstance($formInstance)
206
    {
207
        $this->formInstance = $formInstance;
208
    }
209
210
    /**
211
     * @return array|FormInterface
212
     */
213
    public function getFormInstance()
214
    {
215
        return $this->formInstance;
216
    }
217
218
    /**
219
     * @return FormResult
220
     */
221
    public function getFormResult()
222
    {
223
        return $this->formResult;
224
    }
225
226
    /**
227
     * @param FormResult $formResult
228
     */
229
    public function setFormResult(FormResult $formResult)
230
    {
231
        $this->formResult = $formResult;
232
    }
233
234
    /**
235
     * @return FormObject
236
     */
237
    public function getFormObject()
238
    {
239
        return $this->formObject;
240
    }
241
242
    /**
243
     * @param FormObject $formObject
244
     */
245
    public function setFormObject(FormObject $formObject)
246
    {
247
        $this->formObject = $formObject;
248
    }
249
}
250