Completed
Push — task/configuration-unit-tests ( b70cf2...4a1486 )
by Romain
02:51
created

FormSettings::setDefaultErrorMessage()   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\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\Form;
20
use Romm\Formz\Service\ContextService;
21
22
class FormSettings extends AbstractFormzConfiguration
23
{
24
    use ParentsTrait;
25
26
    const DEFAULT_ERROR_MESSAGE_KEY = 'default_error_message';
27
28
    /**
29
     * @var string
30
     */
31
    protected $defaultClass;
32
33
    /**
34
     * @var string
35
     */
36
    protected $defaultErrorMessage;
37
38
    /**
39
     * @return string
40
     */
41
    public function getDefaultClass()
42
    {
43
        return $this->getSettingsProperty('defaultClass');
44
    }
45
46
    /**
47
     * @param string $defaultClass
48
     */
49
    public function setDefaultClass($defaultClass)
50
    {
51
        $this->defaultClass = $defaultClass;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getDefaultErrorMessage()
58
    {
59
        $message = $this->getSettingsProperty('defaultErrorMessage') ?: self::DEFAULT_ERROR_MESSAGE_KEY;
60
61
        return ContextService::get()->translate($message);
62
    }
63
64
    /**
65
     * @param string $defaultErrorMessage
66
     */
67
    public function setDefaultErrorMessage($defaultErrorMessage)
68
    {
69
        $this->defaultErrorMessage = $defaultErrorMessage;
70
    }
71
72
    /**
73
     * This function will do the following: first, it will check if the wanted
74
     * property is set in this class instance (not null), then it returns it. If
75
     * the value is null, it will fetch the global FormZ configuration settings,
76
     * and return the default value for the asked property.
77
     *
78
     * Example:
79
     *  config.tx_formz.forms.My\Custom\Form.settings.defaultClass is null, then
80
     *  config.tx_formz.settings.defaultFormSettings.defaultClass is returned
81
     *
82
     * @param string $propertyName Name of the wanted class property.
83
     * @return mixed|null
84
     */
85
    private function getSettingsProperty($propertyName)
86
    {
87
        $result = $this->$propertyName;
88
89
        if (empty($result)) {
90
            if ($this->hasParent(Form::class)
91
                && $this->hasParent(Configuration::class)
92
            ) {
93
                $result = $this->withFirstParent(
94
                    Configuration::class,
95
                    function (Configuration $configuration) use ($propertyName) {
96
                        $getter = 'get' . ucfirst($propertyName);
97
98
                        return $configuration->getSettings()->getDefaultFormSettings()->$getter();
99
                    }
100
                );
101
            }
102
        }
103
104
        return $result;
105
    }
106
}
107