Completed
Push — feature/version-2 ( 4aa9b8...6dfc1b )
by Romain
02:16
created

DefaultFormObjectBuilder::insertObjectProperties()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 3
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\Field\Field;
18
use Romm\Formz\Form\Definition\FormDefinition;
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 FormDefinition
49
     */
50
    public function getFormDefinition()
51
    {
52
        $configurationArray = TypoScriptService::get()->getFormConfiguration($this->className);
53
54
        /** @var FormDefinition $configurationObject */
55
        $configurationObject = ConfigurationObjectFactory::getInstance()->get(FormDefinition::class, $configurationArray)->getObject(true);
56
57
        return $configurationObject;
58
    }
59
60
    /**
61
     * Will insert all the accessible properties of the form class in the form
62
     * object.
63
     */
64
    protected function insertObjectProperties()
65
    {
66
        /** @var ReflectionService $reflectionService */
67
        $reflectionService = GeneralUtility::makeInstance(ReflectionService::class);
68
        $reflectionProperties = $reflectionService->getClassPropertyNames($this->className);
69
70
        $classReflection = new \ReflectionClass($this->className);
71
        $publicProperties = $classReflection->getProperties(\ReflectionProperty::IS_PUBLIC);
72
73
        foreach ($reflectionProperties as $property) {
74
            if (false === in_array($property, self::$ignoredProperties)
75
                && false === $reflectionService->isPropertyTaggedWith($this->className, $property, self::IGNORE_PROPERTY)
76
                && ((true === in_array($property, $publicProperties))
77
                    || $reflectionService->hasMethod($this->className, 'get' . ucfirst($property))
78
                )
79
                && false === $this->static->getDefinition()->hasField($property)
80
            ) {
81
                $field = new Field();
82
                $field->setName($property);
83
                $this->static->getDefinition()->addField($field);
84
            }
85
        }
86
87
        unset($publicProperties);
88
    }
89
}
90