Completed
Push — wip/steps ( 551eb2...b236f9 )
by Romain
03:12
created

FormObjectProxy::setFormHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
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\Core\Core;
17
use Romm\Formz\Domain\Model\FormMetadata;
18
use Romm\Formz\Error\FormResult;
19
use Romm\Formz\Form\FormInterface;
20
use Romm\Formz\Form\FormObject\Service\FormObjectMetadata;
21
use Romm\Formz\Form\FormObject\Service\FormObjectRequestData;
22
use Romm\Formz\Service\HashService;
23
24
class FormObjectProxy
25
{
26
    /**
27
     * @var FormObject
28
     */
29
    protected $formObject;
30
31
    /**
32
     * @var FormInterface
33
     */
34
    protected $form;
35
36
    /**
37
     * @var string
38
     */
39
    protected $formHash;
40
41
    /**
42
     * @var bool
43
     */
44
    protected $formWasSubmitted = false;
45
46
    /**
47
     * @var bool
48
     */
49
    protected $formWasValidated = false;
50
    /**
51
     * @var FormResult
52
     */
53
    protected $formResult;
54
55
    /**
56
     * @var FormObjectRequestData
57
     */
58
    protected $requestData;
59
60
    /**
61
     * @var FormObjectMetadata
62
     */
63
    protected $metadata;
64
65
    /**
66
     * @var bool
67
     */
68
    protected $formIsPersistent = false;
69
70
    /**
71
     * @param FormObject    $formObject
72
     * @param FormInterface $form
73
     */
74
    public function __construct(FormObject $formObject, FormInterface $form)
75
    {
76
        $this->formObject = $formObject;
77
        $this->form = $form;
78
    }
79
80
    /**
81
     * @return FormInterface
82
     */
83
    public function getForm()
84
    {
85
        return $this->form;
86
    }
87
88
    /**
89
     * Will mark the form as submitted (change the result returned by the
90
     * function `formWasSubmitted()`).
91
     *
92
     * @internal
93
     */
94
    public function markFormAsSubmitted()
95
    {
96
        $this->formWasSubmitted = true;
97
    }
98
99
    /**
100
     * Returns `true` if the form was submitted by the user.
101
     *
102
     * @return bool
103
     */
104
    public function formWasSubmitted()
105
    {
106
        return $this->formWasSubmitted;
107
    }
108
109
    /**
110
     * Marks the form as validated.
111
     *
112
     * @internal
113
     */
114
    public function markFormAsValidated()
115
    {
116
        $this->formWasValidated = true;
117
    }
118
119
    /**
120
     * @return bool
121
     */
122
    public function formWasValidated()
123
    {
124
        return $this->formWasValidated;
125
    }
126
127
    /**
128
     * @internal
129
     */
130
    public function markFormAsPersistent()
131
    {
132
        $this->formIsPersistent = true;
133
    }
134
135
    /**
136
     * @return bool
137
     */
138
    public function formIsPersistent()
139
    {
140
        return $this->formIsPersistent;
141
    }
142
143
    /**
144
     * @return FormResult
145
     */
146
    public function getFormResult()
147
    {
148
        if (null === $this->formResult) {
149
            $this->formResult = new FormResult;
150
        }
151
152
        return $this->formResult;
153
    }
154
155
    /**
156
     * @return FormObjectRequestData
157
     */
158
    public function getRequestData()
159
    {
160
        return $this->requestData;
161
    }
162
163
    /**
164
     * @return FormMetadata
165
     */
166
    public function getFormMetadata()
167
    {
168
        if (null === $this->metadata) {
169
            $this->metadata = Core::instantiate(FormObjectMetadata::class, $this->formObject);
170
        }
171
172
        return $this->metadata->getMetadata();
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    public function getFormHash()
179
    {
180
        if (null === $this->formHash) {
181
            $this->formHash = HashService::get()->getHash(uniqid(get_class($this->form)));
182
        }
183
184
        return $this->formHash;
185
    }
186
187
    /**
188
     * @param string $hash
189
     */
190
    public function setFormHash($hash)
191
    {
192
        $this->formHash = $hash;
193
    }
194
195
    /**
196
     * @param FormObjectRequestData $requestData
197
     */
198
    public function injectRequestData(FormObjectRequestData $requestData)
199
    {
200
        $this->requestData = $requestData;
201
    }
202
}
203