Completed
Push — feature/version-2 ( a1aace...47461d )
by Romain
03:02
created

getFormDefinitionObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
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\Builder;
15
16
use Romm\ConfigurationObject\ConfigurationObjectFactory;
17
use Romm\Formz\Form\Definition\FormDefinition;
18
use Romm\Formz\Form\FormObject\Definition\FormDefinitionObject;
19
use Romm\Formz\Service\TypoScriptService;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Extbase\Reflection\ReflectionService;
22
23
/**
24
 * Default form object builder.
25
 *
26
 * It fetches the form configuration array from TypoScript, and automatically
27
 * inserts the class properties of the form in the properties of the form
28
 * object.
29
 */
30
class DefaultFormObjectBuilder extends AbstractFormObjectBuilder
31
{
32
    const IGNORE_PROPERTY = 'formz-ignore';
33
34
    /**
35
     * @var array
36
     */
37
    private static $ignoredProperties = ['validationData', 'uid', 'pid', '_localizedUid', '_languageUid', '_versionedUid'];
38
39
    /**
40
     * @see DefaultFormObjectBuilder
41
     */
42
    public function process()
43
    {
44
        $this->insertObjectProperties();
45
    }
46
47
    /**
48
     * @return FormDefinitionObject
49
     */
50
    protected function getFormDefinitionObject()
51
    {
52
        $configurationArray = TypoScriptService::get()->getFormConfiguration($this->className);
53
        $configurationObject = ConfigurationObjectFactory::convert(FormDefinition::class, $configurationArray);
54
55
        /** @var FormDefinitionObject $formDefinitionObject */
56
        $formDefinitionObject = GeneralUtility::makeInstance(
57
            FormDefinitionObject::class,
58
            $configurationObject->getObject(true),
59
            $configurationObject->getValidationResult()
60
        );
61
62
        return $formDefinitionObject;
63
    }
64
65
    /**
66
     * Will insert all the accessible properties of the form class in the form
67
     * object.
68
     */
69
    protected function insertObjectProperties()
70
    {
71
        /** @var ReflectionService $reflectionService */
72
        $reflectionService = GeneralUtility::makeInstance(ReflectionService::class);
73
        $reflectionProperties = $reflectionService->getClassPropertyNames($this->className);
74
75
        $classReflection = new \ReflectionClass($this->className);
76
        $publicProperties = $classReflection->getProperties(\ReflectionProperty::IS_PUBLIC);
77
78
        foreach ($reflectionProperties as $propertyName) {
79
            if (false === in_array($propertyName, self::$ignoredProperties)
80
                && false === $reflectionService->isPropertyTaggedWith($this->className, $propertyName, self::IGNORE_PROPERTY)
81
                && ((true === in_array($propertyName, $publicProperties))
82
                    || $reflectionService->hasMethod($this->className, 'get' . ucfirst($propertyName))
83
                )
84
                && false === $this->static->getDefinition()->hasField($propertyName)
85
            ) {
86
                $this->static->getDefinition()->addField($propertyName);
87
            }
88
        }
89
90
        unset($publicProperties);
91
    }
92
93
    /**
94
     * @return FormDefinition
95
     */
96
    public function getFormDefinition()
97
    {
98
        return null;
99
    }
100
}
101