Completed
Push — tmp-feedback ( b00866 )
by Romain
02:36
created

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