Completed
Push — feature/method-get-properties ( 045265 )
by Romain
02:45
created

FormObjectStatic::getProperties()   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\Core\Core;
17
use Romm\Formz\Form\Definition\FormDefinition;
18
use Romm\Formz\Form\FormObject\Definition\FormDefinitionObject;
19
use Romm\Formz\Form\FormObject\Service\FormObjectConfiguration;
20
use Romm\Formz\Form\FormObject\Service\FormObjectProperties;
21
use Romm\Formz\Service\HashService;
22
use TYPO3\CMS\Extbase\Error\Result;
23
24
class FormObjectStatic
25
{
26
    /**
27
     * @var string
28
     */
29
    protected $className;
30
31
    /**
32
     * @var FormDefinitionObject
33
     */
34
    protected $definition;
35
36
    /**
37
     * @var string
38
     */
39
    protected $objectHash;
40
41
    /**
42
     * @var FormObjectConfiguration
43
     */
44
    protected $configurationService;
45
46
    /**
47
     * @var FormObjectProperties
48
     */
49
    protected $propertiesService;
50
51
    /**
52
     * @param string               $className
53
     * @param FormDefinitionObject $definition
54
     */
55
    public function __construct($className, FormDefinitionObject $definition)
56
    {
57
        $this->className = $className;
58
        $this->definition = $definition;
59
        $this->configurationService = Core::instantiate(FormObjectConfiguration::class, $this, $definition);
60
        $this->propertiesService = Core::instantiate(FormObjectProperties::class, $this);
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getClassName()
67
    {
68
        return $this->className;
69
    }
70
71
    /**
72
     * @return FormDefinition
73
     */
74
    public function getDefinition()
75
    {
76
        return $this->definition->getDefinition();
77
    }
78
79
    /**
80
     * This function will merge and return the validation results of both the
81
     * global FormZ configuration object, and this form configuration object.
82
     *
83
     * @return Result
84
     */
85
    public function getDefinitionValidationResult()
86
    {
87
        return $this->configurationService->getConfigurationValidationResult();
88
    }
89
90
    /**
91
     * Returns the hash of the form object, which should be calculated only once
92
     * for performance concerns.
93
     *
94
     * @return string
95
     */
96
    public function getObjectHash()
97
    {
98
        if (null === $this->objectHash) {
99
            $this->objectHash = $this->calculateObjectHash();
100
        }
101
102
        return $this->objectHash;
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function getProperties()
109
    {
110
        return $this->propertiesService->getProperties();
111
    }
112
113
    /**
114
     * Returns the calculated hash of the form object.
115
     *
116
     * @return string
117
     */
118
    protected function calculateObjectHash()
119
    {
120
        /*
121
         * Triggering the validation result and the properties calculations, to
122
         * be sure these values will be in the serialization string.
123
         */
124
        $this->getDefinitionValidationResult();
125
        $this->getProperties();
126
127
        return HashService::get()->getHash(serialize($this));
128
    }
129
}
130