Completed
Branch unit-test-view-helpers (d3d368)
by Romain
02:08
created

FieldSettings::setMessageTemplate()   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
 * 2016 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 $feedbackContainerSelector;
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 $feedbackListSelector;
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 null $fieldName
65
     * @return string
66
     */
67
    public function getFeedbackContainerSelector($fieldName = null)
68
    {
69
        return $this->formatSelector($this->getSettingsProperty('feedbackContainerSelector'), $fieldName);
70
    }
71
72
    /**
73
     * @param string $fieldName
74
     * @return string
75
     */
76
    public function getFeedbackListSelector($fieldName = null)
77
    {
78
        return $this->formatSelector($this->getSettingsProperty('feedbackListSelector'), $fieldName);
79
    }
80
81
    /**
82
     * @param string $fieldName
83
     * @return string
84
     */
85
    public function getMessageTemplate($fieldName = null)
86
    {
87
        return $this->formatSelector($this->getSettingsProperty('messageTemplate'), $fieldName);
88
    }
89
90
    /**
91
     * @param string $messageTemplate
92
     */
93
    public function setMessageTemplate($messageTemplate)
94
    {
95
        $this->messageTemplate = $messageTemplate;
96
    }
97
98
    /**
99
     * @param string $selector
100
     * @param string $fieldName
101
     * @return string
102
     */
103
    protected function formatSelector($selector, $fieldName = null)
104
    {
105
        if (null === $fieldName) {
106
            $fieldName = $this->getFieldName();
107
        }
108
109
        return str_replace('#FIELD#', $fieldName, $selector);
110
    }
111
112
    /**
113
     * This function will do the following: first, it will check if the wanted
114
     * property is set in this class instance (not null), then it returns it. If
115
     * the value is null, it will fetch the global Formz configuration settings,
116
     * and return the default value for the asked property.
117
     *
118
     * Example:
119
     *  config.tx_formz.forms.My\Custom\Form.fields.myField.settings.fieldContainerSelector is null, then
120
     *  config.tx_formz.settings.defaultFieldSettings.fieldContainerSelector is returned
121
     *
122
     * @param string $propertyName Name of the wanted class property.
123
     * @return mixed|null
124
     */
125
    private function getSettingsProperty($propertyName)
126
    {
127
        $result = $this->$propertyName;
128
129
        if (null === $result) {
130
            $fieldName = $this->getFieldName();
131
132
            $result = $this->withFirstParent(
133
                Configuration::class,
134
                function (Configuration $configuration) use ($propertyName, $fieldName) {
135
                    $getter = 'get' . ucfirst($propertyName);
136
137
                    return $configuration->getSettings()->getDefaultFieldSettings()->$getter($fieldName);
138
                }
139
            );
140
        }
141
142
        return $result;
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    private function getFieldName()
149
    {
150
        return $this->withFirstParent(
151
            Field::class,
152
            function (Field $field) {
153
                return $field->getFieldName();
154
            }
155
        );
156
    }
157
}
158