Completed
Push — master ( 139af3...06c5f0 )
by
unknown
04:58 queued 02:06
created

FormObject::getConfigurationArray()   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
 * 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\Form;
15
16
use Romm\ConfigurationObject\ConfigurationObjectInstance;
17
use Romm\ConfigurationObject\ConfigurationObjectFactory;
18
use Romm\Formz\Configuration\Form\Form;
19
use Romm\Formz\Core\Core;
20
21
/**
22
 * This is the object representation of a form. In here we can manage which
23
 * properties the form does have, its configuration, and more.
24
 */
25
class FormObject
26
{
27
28
    /**
29
     * Name of the form.
30
     *
31
     * @var string
32
     */
33
    protected $name;
34
35
    /**
36
     * @var string
37
     */
38
    protected $className;
39
40
    /**
41
     * The properties of the form.
42
     *
43
     * @var array
44
     */
45
    protected $properties = [];
46
47
    /**
48
     * Contains the form configuration.
49
     *
50
     * @var array
51
     */
52
    protected $configurationArray = [];
53
54
    /**
55
     * Contains the form configuration object, which was created from the
56
     * configuration array.
57
     *
58
     * @var ConfigurationObjectInstance
59
     */
60
    protected $configurationObject;
61
62
    /**
63
     * @var string
64
     */
65
    protected $hash;
66
67
    /**
68
     * @var bool
69
     */
70
    protected $hashShouldBeCalculated = true;
71
72
    /**
73
     * You should never create a new instance of this class directly, use the
74
     * `FormObjectFactory->getInstanceFromClassName()` function instead.
75
     *
76
     * @param string $className
77
     * @param string $name
78
     */
79
    public function __construct($className, $name)
80
    {
81
        $this->className = $className;
82
        $this->name = $name;
83
    }
84
85
    /**
86
     * Registers a new property for this form.
87
     *
88
     * @param string $name
89
     * @return $this
90
     */
91
    public function addProperty($name)
92
    {
93
        if (false === in_array($name, $this->properties)) {
94
            $this->properties[] = $name;
95
            $this->hashShouldBeCalculated = true;
96
        }
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return Form
103
     */
104
    public function getConfiguration()
105
    {
106
        /** @var Form $configuration */
107
        $configuration = $this->getConfigurationObject()->getObject(true);
108
109
        return $configuration;
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getName()
116
    {
117
        return $this->name;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getClassName()
124
    {
125
        return $this->className;
126
    }
127
128
    /**
129
     * @return array
130
     */
131
    public function getProperties()
132
    {
133
        return $this->properties;
134
    }
135
136
    /**
137
     * Returns the hash, which should be calculated only once for performance
138
     * concerns.
139
     *
140
     * @return string
141
     */
142
    public function getHash()
143
    {
144
        if (true === $this->hashShouldBeCalculated
145
            || null === $this->hash
146
        ) {
147
            $this->hashShouldBeCalculated = false;
148
            $this->hash = $this->calculateHash();
149
        }
150
151
        return $this->hash;
152
    }
153
154
    /**
155
     * Returns an instance of configuration object. Checks if it was previously
156
     * stored in cache, otherwise it is created from scratch.
157
     *
158
     * @return ConfigurationObjectInstance
159
     * @internal
160
     */
161
    public function getConfigurationObject()
162
    {
163
        if (null === $this->configurationObject) {
164
            $cacheInstance = Core::getCacheInstance();
165
            $cacheIdentifier = 'configuration-' . $this->getHash();
166
167
            if ($cacheInstance->has($cacheIdentifier)) {
168
                $configurationObject = $cacheInstance->get($cacheIdentifier);
169
            } else {
170
                $configurationObject =  ConfigurationObjectFactory::getInstance()
171
                    ->get(Form::class, $this->configurationArray);
172
173
                if (false === $configurationObject->getValidationResult()->hasErrors()) {
174
                    $cacheInstance->set($cacheIdentifier, $configurationObject);
175
                }
176
            }
177
178
            $this->configurationObject = $configurationObject;
179
        }
180
181
        return $this->configurationObject;
182
    }
183
184
    /**
185
     * This function will clean the configuration array by removing useless data
186
     * and updating needed ones.
187
     *
188
     * @param array $configuration
189
     * @return array
190
     */
191
    protected function sanitizeConfiguration(array $configuration)
192
    {
193
        // Removing configuration of fields which do not exist for this form.
194
        $sanitizedFieldsConfiguration = [];
195
        $fieldsConfiguration = (isset($configuration['fields']))
196
            ? $configuration['fields']
197
            : [];
198
199
        foreach ($this->properties as $property) {
200
            $sanitizedFieldsConfiguration[$property] = (isset($fieldsConfiguration[$property]))
201
                ? $fieldsConfiguration[$property]
202
                : [];
203
        }
204
205
        $configuration['fields'] = $sanitizedFieldsConfiguration;
206
207
        return $configuration;
208
    }
209
210
    /**
211
     * Returns the calculated hash of this class.
212
     *
213
     * @return string
214
     */
215
    protected function calculateHash()
216
    {
217
        return sha1(serialize($this));
218
    }
219
220
    /**
221
     * When this instance is saved in TYPO3 cache, we need not to store all the
222
     * properties to increase performance.
223
     *
224
     * @return array
225
     */
226
    public function __sleep()
227
    {
228
        return ['name', 'className', 'properties', 'configurationArray', 'hash'];
229
    }
230
231
    /**
232
     * When this class is unserialized, we update the flag to know if the hash
233
     * should be calculated or not (if it was calculated before it was
234
     * serialized, there is no need to calculate it again).
235
     */
236
    public function __wakeup()
237
    {
238
        $this->hashShouldBeCalculated = (null === $this->hash);
239
    }
240
241
    /**
242
     * @return array
243
     * @internal Should not be used, it is here only for unit tests.
244
     */
245
    public function getConfigurationArray()
246
    {
247
        return $this->configurationArray;
248
    }
249
250
    /**
251
     * @param array $configuration
252
     * @return $this
253
     */
254
    public function setConfigurationArray($configuration)
255
    {
256
        $this->configurationArray = $this->sanitizeConfiguration($configuration);
257
        $this->hashShouldBeCalculated = true;
258
259
        return $this;
260
    }
261
}
262