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