Completed
Push — middleware-wip-tmp2 ( 6948fe )
by Romain
03:15
created

DefaultFormObjectBuilder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 52
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 4 1
A getConfigurationArray() 0 4 1
B insertObjectProperties() 0 22 6
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\Formz\Service\TypoScriptService;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Extbase\Reflection\ReflectionService;
19
20
/**
21
 * Default form object builder.
22
 *
23
 * It fetches the form configuration array from TypoScript, and automatically
24
 * inserts the class properties of the form in the properties of the form
25
 * object.
26
 */
27
class DefaultFormObjectBuilder extends AbstractFormObjectBuilder
28
{
29
    const IGNORE_PROPERTY = 'formz-ignore';
30
31
    /**
32
     * @var array
33
     */
34
    private static $ignoredProperties = ['validationData', 'uid', 'pid', '_localizedUid', '_languageUid', '_versionedUid'];
35
36
    /**
37
     * @see DefaultFormObjectBuilder
38
     */
39
    public function process()
40
    {
41
        $this->insertObjectProperties();
42
    }
43
44
    /**
45
     * @return array
46
     */
47
    public function getConfigurationArray()
48
    {
49
        return TypoScriptService::get()->getFormConfiguration($this->className);
50
    }
51
52
    /**
53
     * Will insert all the accessible properties of the form class in the form
54
     * object.
55
     */
56
    protected function insertObjectProperties()
57
    {
58
        /** @var ReflectionService $reflectionService */
59
        $reflectionService = GeneralUtility::makeInstance(ReflectionService::class);
60
        $reflectionProperties = $reflectionService->getClassPropertyNames($this->className);
61
62
        $classReflection = new \ReflectionClass($this->className);
63
        $publicProperties = $classReflection->getProperties(\ReflectionProperty::IS_PUBLIC);
64
65
        foreach ($reflectionProperties as $property) {
66
            if (false === in_array($property, self::$ignoredProperties)
67
                && false === $reflectionService->isPropertyTaggedWith($this->className, $property, self::IGNORE_PROPERTY)
68
                && ((true === in_array($property, $publicProperties))
69
                    || $reflectionService->hasMethod($this->className, 'get' . ucfirst($property))
70
                )
71
            ) {
72
                $this->static->addProperty($property);
73
            }
74
        }
75
76
        unset($publicProperties);
77
    }
78
}
79