Completed
Push — feature/method-get-properties ( 045265 )
by Romain
02:45
created

FormObject::getProperties()   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 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\Error\FormResult;
17
use Romm\Formz\Exceptions\DuplicateEntryException;
18
use Romm\Formz\Exceptions\PropertyNotAccessibleException;
19
use Romm\Formz\Form\Definition\FormDefinition;
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
     * You should never create a new instance of this class directly, use the
46
     * `FormObjectFactory->getInstanceFromClassName()` function instead.
47
     *
48
     * @param string           $name
49
     * @param FormObjectStatic $static
50
     */
51
    public function __construct($name, FormObjectStatic $static)
52
    {
53
        $this->name = $name;
54
        $this->static = $static;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getName()
61
    {
62
        return $this->name;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getClassName()
69
    {
70
        return $this->static->getClassName();
71
    }
72
73
    /**
74
     * @return FormDefinition
75
     */
76
    public function getDefinition()
77
    {
78
        return $this->static->getDefinition();
79
    }
80
81
    /**
82
     * @return Result
83
     */
84
    public function getDefinitionValidationResult()
85
    {
86
        return $this->static->getDefinitionValidationResult();
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function getProperties()
93
    {
94
        return $this->static->getProperties();
95
    }
96
97
    /**
98
     * @return FormInterface
99
     */
100
    public function getForm()
101
    {
102
        return $this->getProxy()->getForm();
103
    }
104
105
    /**
106
     * @return bool
107
     */
108
    public function hasForm()
109
    {
110
        return $this->proxy !== null;
111
    }
112
113
    /**
114
     * @param FormInterface $form
115
     * @throws DuplicateEntryException
116
     */
117
    public function setForm(FormInterface $form)
118
    {
119
        if ($this->proxy) {
120
            throw DuplicateEntryException::formInstanceAlreadyAdded($this);
121
        }
122
123
        $this->registerFormInstance($form);
124
125
        $this->proxy = $this->createProxy($form);
126
    }
127
128
    /**
129
     * @return bool
130
     */
131
    public function formWasSubmitted()
132
    {
133
        return $this->hasForm() && $this->getProxy()->formWasSubmitted();
134
    }
135
136
    /**
137
     * @return bool
138
     */
139
    public function formWasValidated()
140
    {
141
        return $this->hasForm() && $this->getProxy()->formWasValidated();
142
    }
143
144
    /**
145
     * @return FormResult
146
     */
147
    public function getFormResult()
148
    {
149
        return $this->getProxy()->getFormResult();
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function getFormHash()
156
    {
157
        return $this->getProxy()->getFormHash();
158
    }
159
160
    /**
161
     * @return string
162
     */
163
    public function getObjectHash()
164
    {
165
        return $this->static->getObjectHash();
166
    }
167
168
    /**
169
     * @param FormInterface $form
170
     */
171
    protected function registerFormInstance(FormInterface $form)
172
    {
173
        if (false === FormObjectFactory::get()->formInstanceWasRegistered($form)) {
174
            FormObjectFactory::get()->registerFormInstance($form, $this->getName());
175
        }
176
    }
177
178
    /**
179
     * @return FormObjectProxy
180
     * @throws PropertyNotAccessibleException
181
     */
182
    protected function getProxy()
183
    {
184
        if (null === $this->proxy) {
185
            throw PropertyNotAccessibleException::formInstanceNotSet();
186
        }
187
188
        return $this->proxy;
189
    }
190
191
    /**
192
     * Wrapper for unit tests.
193
     *
194
     * @param FormInterface $form
195
     * @return FormObjectProxy
196
     */
197
    protected function createProxy(FormInterface $form)
198
    {
199
        return FormObjectFactory::get()->getProxy($form);
200
    }
201
}
202