Completed
Push — typo3-v8-compatibility ( ae4db2...381377 )
by Romain
02:16
created

FormService::setFormObject()   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\Error\FormResult;
17
use Romm\Formz\Exceptions\ContextNotFoundException;
18
use Romm\Formz\Form\FormInterface;
19
use Romm\Formz\Form\FormObject;
20
use Romm\Formz\ViewHelpers\FormViewHelper;
21
use TYPO3\CMS\Core\SingletonInterface;
22
23
/**
24
 * This class contains methods that help view helpers to manipulate data and
25
 * know more things concerning the current form state.
26
 *
27
 * It is mainly configured inside the `FormViewHelper`, and used in other
28
 * view helpers.
29
 */
30
class FormService implements SingletonInterface
31
{
32
    /**
33
     * @var bool
34
     */
35
    protected $formContext = false;
36
37
    /**
38
     * @var array|FormInterface
39
     */
40
    protected $formInstance;
41
42
    /**
43
     * @var bool
44
     */
45
    protected $formWasSubmitted = false;
46
47
    /**
48
     * @var FormResult
49
     */
50
    protected $formResult;
51
52
    /**
53
     * @var FormObject
54
     */
55
    protected $formObject;
56
57
    /**
58
     * Reset every state that can be used by this service.
59
     */
60
    public function resetState()
61
    {
62
        $this->formContext = false;
63
        $this->formInstance = null;
64
        $this->formResult = null;
65
        $this->formWasSubmitted = false;
66
    }
67
68
    /**
69
     * Will activate the form context, changing the result returned by the
70
     * function `formContextExists()`.
71
     *
72
     * @throws \Exception
73
     */
74
    public function activateFormContext()
75
    {
76
        if (true === $this->formContext) {
77
            throw new \Exception(
78
                'You can not use a form view helper inside another one.',
79
                1465242575
80
            );
81
        }
82
83
        $this->formContext = true;
84
    }
85
86
    /**
87
     * Returns `true` if the `FormViewHelper` context exists.
88
     *
89
     * @return bool
90
     */
91
    public function formContextExists()
92
    {
93
        return $this->formContext;
94
    }
95
96
    /**
97
     * Will mark the form as submitted (change the result returned by the
98
     * function `formWasSubmitted()`).
99
     */
100
    public function markFormAsSubmitted()
101
    {
102
        $this->formWasSubmitted = true;
103
    }
104
105
    /**
106
     * Returns `true` if the form was submitted by the user.
107
     *
108
     * @return bool
109
     */
110
    public function formWasSubmitted()
111
    {
112
        return $this->formWasSubmitted;
113
    }
114
115
    /**
116
     * Checks that the current `FormViewHelper` exists. If not, an exception is
117
     * thrown.
118
     *
119
     * @throws \Exception
120
     */
121
    public function checkIsInsideFormViewHelper()
122
    {
123
        if (false === $this->formContextExists()) {
124
            throw new ContextNotFoundException(
125
                'The view helper "' . get_called_class() . '" must be used inside the view helper "' . FormViewHelper::class . '".',
126
                1465243085
127
            );
128
        }
129
    }
130
131
    /**
132
     * If the form was submitted by the user, contains the array containing the
133
     * submitted values.
134
     *
135
     * @param array|FormInterface $formInstance
136
     */
137
    public function setFormInstance($formInstance)
138
    {
139
        $this->formInstance = $formInstance;
140
    }
141
142
    /**
143
     * @return array|FormInterface
144
     */
145
    public function getFormInstance()
146
    {
147
        return $this->formInstance;
148
    }
149
150
    /**
151
     * @return FormResult
152
     */
153
    public function getFormResult()
154
    {
155
        return $this->formResult;
156
    }
157
158
    /**
159
     * @param FormResult $formResult
160
     */
161
    public function setFormResult(FormResult $formResult)
162
    {
163
        $this->formResult = $formResult;
164
    }
165
166
    /**
167
     * @return FormObject
168
     */
169
    public function getFormObject()
170
    {
171
        return $this->formObject;
172
    }
173
174
    /**
175
     * @param FormObject $formObject
176
     */
177
    public function setFormObject(FormObject $formObject)
178
    {
179
        $this->formObject = $formObject;
180
    }
181
}
182