Completed
Push — middleware-wip ( 7414b2...785812 )
by Romain
02:52
created

FormObjectStatic::getClassName()   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\Configuration\Configuration;
17
use Romm\Formz\Configuration\ConfigurationFactory;
18
use Romm\Formz\Configuration\Form\Form;
19
use Romm\Formz\Core\Core;
20
use Romm\Formz\Form\FormObject\Service\FormObjectConfiguration;
21
use Romm\Formz\Service\HashService;
22
use Romm\Formz\Service\TypoScriptService;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Extbase\Error\Result;
25
use TYPO3\CMS\Extbase\Reflection\ReflectionService;
26
27
class FormObjectStatic
28
{
29
    const IGNORE_PROPERTY = 'formz-ignore';
30
31
    /**
32
     * @var string
33
     */
34
    protected $className;
35
36
    /**
37
     * The properties of the form.
38
     *
39
     * @var array
40
     */
41
    protected $properties = [];
42
43
    /**
44
     * @var FormObjectConfiguration
45
     */
46
    protected $configuration;
47
48
    /**
49
     * @var string
50
     */
51
    protected $objectHash;
52
53
    /**
54
     * @var array
55
     */
56
    private static $ignoredProperties = ['validationData', 'uid', 'pid', '_localizedUid', '_languageUid', '_versionedUid'];
57
58
    /**
59
     * @param string $className
60
     */
61
    public function __construct($className)
62
    {
63
        $this->className = $className;
64
65
        $formConfiguration = TypoScriptService::get()->getFormConfiguration($className);
66
        $this->configuration = Core::instantiate(FormObjectConfiguration::class, $this, $formConfiguration);
67
68
        $this->insertObjectProperties();
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getClassName()
75
    {
76
        return $this->className;
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public function getProperties()
83
    {
84
        return $this->properties;
85
    }
86
87
    /**
88
     * @param string $name
89
     * @return bool
90
     */
91
    public function hasProperty($name)
92
    {
93
        return in_array($name, $this->properties);
94
    }
95
96
    /**
97
     * Registers a new property for this form.
98
     *
99
     * @param string $name
100
     * @return $this
101
     */
102
    public function addProperty($name)
103
    {
104
        if (false === $this->hasProperty($name)) {
105
            $this->properties[] = $name;
106
            $this->objectHash = null;
107
        }
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return Form
114
     */
115
    public function getConfiguration()
116
    {
117
        /** @var Form $configuration */
118
        $configuration = $this->configuration->getConfigurationObject()->getObject(true);
119
120
        return $configuration;
121
    }
122
123
    /**
124
     * This function will merge and return the validation results of both the
125
     * global FormZ configuration object, and this form configuration object.
126
     *
127
     * @return Result
128
     */
129
    public function getConfigurationValidationResult()
130
    {
131
        return $this->configuration->getConfigurationValidationResult();
132
    }
133
134
    /**
135
     * Returns the hash of the form object, which should be calculated only once
136
     * for performance concerns.
137
     *
138
     * @return string
139
     */
140
    public function getObjectHash()
141
    {
142
        if (null === $this->objectHash) {
143
            $this->objectHash = $this->calculateObjectHash();
144
        }
145
146
        return $this->objectHash;
147
    }
148
149
    /**
150
     * Returns the calculated hash of the form object.
151
     *
152
     * @return string
153
     */
154
    protected function calculateObjectHash()
155
    {
156
        return HashService::get()->getHash(serialize($this));
157
    }
158
159
    /**
160
     * Will insert all the accessible properties of the given instance.
161
     */
162
    protected function insertObjectProperties()
163
    {
164
        /** @var ReflectionService $reflectionService */
165
        $reflectionService = GeneralUtility::makeInstance(ReflectionService::class);
166
        $reflectionProperties = $reflectionService->getClassPropertyNames($this->className);
167
168
        $classReflection = new \ReflectionClass($this->className);
169
        $publicProperties = $classReflection->getProperties(\ReflectionProperty::IS_PUBLIC);
170
171
        foreach ($reflectionProperties as $property) {
172
            if (false === in_array($property, self::$ignoredProperties)
173
                && false === $reflectionService->isPropertyTaggedWith($this->className, $property, self::IGNORE_PROPERTY)
174
                && ((true === in_array($property, $publicProperties))
175
                    || $reflectionService->hasMethod($this->className, 'get' . ucfirst($property))
176
                )
177
            ) {
178
                $this->addProperty($property);
179
            }
180
        }
181
182
        unset($publicProperties);
183
    }
184
185
    /**
186
     * Adds this form configuration to the global FormZ configuration object.
187
     */
188
    public function injectInFormConfiguration()
189
    {
190
        /** @var Configuration $formzConfiguration */
191
        $formzConfiguration = ConfigurationFactory::get()->getFormzConfiguration()->getObject(true);
192
193
        if (false === $formzConfiguration->hasForm($this->getClassName())) {
194
            $formzConfiguration->addForm($this);
195
        }
196
    }
197
}
198