Completed
Push — feature/version-2 ( a353c5...4aa9b8 )
by Romain
19:54 queued 12:50
created

FieldSettings   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 159
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldContainerSelector() 0 4 1
A setFieldContainerSelector() 0 4 1
A getMessageContainerSelector() 0 4 1
A setMessageContainerSelector() 0 4 1
A getMessageListSelector() 0 4 1
A setMessageListSelector() 0 4 1
A getMessageTemplate() 0 4 1
A setMessageTemplate() 0 4 1
A formatSelector() 0 10 2
A getSettingsProperty() 0 18 2
A getFieldName() 0 9 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\Definition\Field\Settings;
15
16
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait;
17
use Romm\Formz\Configuration\AbstractFormzConfiguration;
18
use Romm\Formz\Configuration\Configuration;
19
use Romm\Formz\Form\Definition\Field\Field;
20
21
class FieldSettings extends AbstractFormzConfiguration
22
{
23
    use ParentsTrait;
24
25
    const FIELD_MARKER = '#FIELD#';
26
27
    /**
28
     * CSS selector to get the container of the field.
29
     *
30
     * @var string
31
     */
32
    protected $fieldContainerSelector;
33
34
    /**
35
     * CSS selector to get the error container of the parent field.
36
     *
37
     * @var string
38
     */
39
    protected $messageContainerSelector;
40
41
    /**
42
     * CSS selector to get the block element which will contain all the
43
     * messages. It must be a child element of `$messageContainerSelector`. If
44
     * the value is empty, then `$messageContainerSelector` is considered to be
45
     * both the container and the block element.
46
     *
47
     * @var string
48
     */
49
    protected $messageListSelector;
50
51
    /**
52
     * @var string
53
     */
54
    protected $messageTemplate;
55
56
    /**
57
     * @return string
58
     */
59
    public function getFieldContainerSelector()
60
    {
61
        return $this->formatSelector($this->getSettingsProperty('fieldContainerSelector'));
62
    }
63
64
    /**
65
     * @param string $selector
66
     */
67
    public function setFieldContainerSelector($selector)
68
    {
69
        $this->fieldContainerSelector = $selector;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getMessageContainerSelector()
76
    {
77
        return $this->formatSelector($this->getSettingsProperty('messageContainerSelector'));
78
    }
79
80
    /**
81
     * @param string $selector
82
     */
83
    public function setMessageContainerSelector($selector)
84
    {
85
        $this->messageContainerSelector = $selector;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getMessageListSelector()
92
    {
93
        return $this->formatSelector($this->getSettingsProperty('messageListSelector'));
94
    }
95
96
    /**
97
     * @param string $messageListSelector
98
     */
99
    public function setMessageListSelector($messageListSelector)
100
    {
101
        $this->messageListSelector = $messageListSelector;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getMessageTemplate()
108
    {
109
        return $this->formatSelector($this->getSettingsProperty('messageTemplate'));
110
    }
111
112
    /**
113
     * @param string $messageTemplate
114
     */
115
    public function setMessageTemplate($messageTemplate)
116
    {
117
        $this->messageTemplate = $messageTemplate;
118
    }
119
120
    /**
121
     * @param string $selector
122
     * @return string
123
     */
124
    protected function formatSelector($selector)
125
    {
126
        $fieldName = $this->getFieldName();
127
128
        if ($fieldName) {
129
            $selector = str_replace(self::FIELD_MARKER, $fieldName, $selector);
130
        }
131
132
        return $selector;
133
    }
134
135
    /**
136
     * This function will do the following: first, it will check if the wanted
137
     * property is set in this class instance (not null), then it returns it. If
138
     * the value is null, it will fetch the global FormZ configuration settings,
139
     * and return the default value for the asked property.
140
     *
141
     * Example:
142
     *  config.tx_formz.forms.My\Custom\Form.fields.myField.settings.fieldContainerSelector is null, then
143
     *  config.tx_formz.settings.defaultFieldSettings.fieldContainerSelector is returned
144
     *
145
     * @param string $propertyName Name of the wanted class property.
146
     * @return mixed|null
147
     */
148
    private function getSettingsProperty($propertyName)
149
    {
150
        $result = $this->$propertyName;
151
152
        if (empty($result)) {
153
            $result = $this->withFirstParent(
154
                Configuration::class,
155
                function (Configuration $configuration) use ($propertyName) {
156
                    $fieldName = $this->getFieldName();
157
                    $getter = 'get' . ucfirst($propertyName);
158
159
                    return $configuration->getSettings()->getDefaultFieldSettings()->$getter($fieldName);
160
                }
161
            );
162
        }
163
164
        return $result;
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    private function getFieldName()
171
    {
172
        return $this->withFirstParent(
173
            Field::class,
174
            function (Field $field) {
175
                return $field->getName();
176
            }
177
        );
178
    }
179
}
180