Completed
Push — middleware-wip ( 1bcdac...ffd957 )
by Romain
02:46
created

FormObjectStatic   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 4
dl 0
loc 122
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getClassName() 0 4 1
A getProperties() 0 4 1
A hasProperty() 0 4 1
A addProperty() 0 9 2
A getConfiguration() 0 7 1
A getConfigurationValidationResult() 0 4 1
A getObjectHash() 0 8 2
A calculateObjectHash() 0 4 1
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\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  $formConfiguration
49
     */
50
    public function __construct($className, array $formConfiguration)
51
    {
52
        $this->className = $className;
53
        $this->configuration = Core::instantiate(FormObjectConfiguration::class, $this, $formConfiguration);
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getClassName()
60
    {
61
        return $this->className;
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function getProperties()
68
    {
69
        return $this->properties;
70
    }
71
72
    /**
73
     * @param string $name
74
     * @return bool
75
     */
76
    public function hasProperty($name)
77
    {
78
        return in_array($name, $this->properties);
79
    }
80
81
    /**
82
     * Registers a new property for this form.
83
     *
84
     * @param string $name
85
     * @return $this
86
     */
87
    public function addProperty($name)
88
    {
89
        if (false === $this->hasProperty($name)) {
90
            $this->properties[] = $name;
91
            $this->objectHash = null;
92
        }
93
94
        return $this;
95
    }
96
97
    /**
98
     * @return Form
99
     */
100
    public function getConfiguration()
101
    {
102
        /** @var Form $configuration */
103
        $configuration = $this->configuration->getConfigurationObject()->getObject(true);
104
105
        return $configuration;
106
    }
107
108
    /**
109
     * This function will merge and return the validation results of both the
110
     * global FormZ configuration object, and this form configuration object.
111
     *
112
     * @return Result
113
     */
114
    public function getConfigurationValidationResult()
115
    {
116
        return $this->configuration->getConfigurationValidationResult();
117
    }
118
119
    /**
120
     * Returns the hash of the form object, which should be calculated only once
121
     * for performance concerns.
122
     *
123
     * @return string
124
     */
125
    public function getObjectHash()
126
    {
127
        if (null === $this->objectHash) {
128
            $this->objectHash = $this->calculateObjectHash();
129
        }
130
131
        return $this->objectHash;
132
    }
133
134
    /**
135
     * Returns the calculated hash of the form object.
136
     *
137
     * @return string
138
     */
139
    protected function calculateObjectHash()
140
    {
141
        return HashService::get()->getHash(serialize($this));
142
    }
143
}
144