Completed
Push — feature/improve-form-object-ha... ( b02d23...a46062 )
by Romain
15:30
created

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