Completed
Push — feature/improve-form-object-ha... ( a46062...6909c1 )
by Romain
03:22
created

FormObject::hasProperty()   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 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\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
     * @return Form
80
     */
81
    public function getConfiguration()
82
    {
83
        return $this->static->getConfigurationService();
84
    }
85
86
    /**
87
     * @return Result
88
     */
89
    public function getConfigurationValidationResult()
90
    {
91
        return $this->static->getConfigurationValidationResult();
92
    }
93
94
    /**
95
     * @return FormInterface
96
     */
97
    public function getForm()
98
    {
99
        return $this->getProxy()->getForm();
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    public function hasForm()
106
    {
107
        return $this->proxy !== null;
108
    }
109
110
    /**
111
     * @param FormInterface $form
112
     * @throws DuplicateEntryException
113
     */
114
    public function setForm(FormInterface $form)
115
    {
116
        if ($this->proxy) {
117
            throw DuplicateEntryException::formInstanceAlreadyAdded($this);
118
        }
119
120
        $this->proxy = $this->createProxy($form);
121
    }
122
123
    /**
124
     * @return bool
125
     */
126
    public function formWasSubmitted()
127
    {
128
        return $this->hasForm() && $this->getProxy()->formWasSubmitted();
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    public function formWasValidated()
135
    {
136
        return $this->hasForm() && $this->getProxy()->formWasValidated();
137
    }
138
139
    /**
140
     * @return FormResult
141
     */
142
    public function getFormResult()
143
    {
144
        return $this->getProxy()->getFormResult();
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getFormHash()
151
    {
152
        return $this->getProxy()->getFormHash();
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    public function getObjectHash()
159
    {
160
        return $this->static->getObjectHash();
161
    }
162
163
    /**
164
     * @return FormObjectProxy
165
     * @throws PropertyNotAccessibleException
166
     */
167
    protected function getProxy()
168
    {
169
        if (null === $this->proxy) {
170
            throw PropertyNotAccessibleException::formInstanceNotSet();
171
        }
172
173
        return $this->proxy;
174
    }
175
176
    /**
177
     * Wrapper for unit tests.
178
     *
179
     * @param FormInterface $form
180
     * @return FormObjectProxy
181
     */
182
    protected function createProxy(FormInterface $form)
183
    {
184
        return $this->formObjectFactory->getProxy($form);
185
    }
186
187
    /**
188
     * @param FormObjectFactory $formObjectFactory
189
     */
190
    public function injectFormObjectFactory(FormObjectFactory $formObjectFactory)
191
    {
192
        $this->formObjectFactory = $formObjectFactory;
193
    }
194
}
195