Completed
Push — feature/improve-form-object-ha... ( 8bb971 )
by Romain
10:18
created

FormObject::formWasValidated()   A

Complexity

Conditions 2
Paths 2

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