Completed
Push — unit-test-form-view-helper ( 80118a...83100a )
by Romain
02:13
created

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