Completed
Push — master ( c3d3bd...9f3343 )
by Casey
57s
created

AbstractSettingDefinition::processValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Settings Manager
5
 *
6
 * @license http://opensource.org/licenses/MIT
7
 * @link https://github.com/caseyamcl/settings-manager
8
 * @package caseyamcl/settings-manager
9
 * @author Casey McLaughlin <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 *
14
 *  ------------------------------------------------------------------
15
 */
16
17
declare(strict_types=1);
18
19
namespace SettingsManager\Model;
20
21
use LogicException;
22
use SettingsManager\Contract\SettingDefinition;
23
24
/**
25
 * Abstract Setting - provides shortcut constants for common setting parameters
26
 *
27
 * @author Casey McLaughlin <[email protected]>
28
 */
29
abstract class AbstractSettingDefinition implements SettingDefinition
30
{
31
    public const NAME = null;
32
    public const DISPLAY_NAME = null;
33
    public const NOTES = '';
34
    public const DEFAULT = null;
35
    public const SENSITIVE = true;
36
37
    /**
38
     * @return string
39
     */
40
    public function getName(): string
41
    {
42
        return $this->requireConstant('NAME');
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getDisplayName(): string
49
    {
50
        return $this->requireConstant('DISPLAY_NAME');
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getNotes(): string
57
    {
58
        return static::NOTES;
59
    }
60
61
    /**
62
     * @return mixed|null
63
     */
64
    public function getDefault()
65
    {
66
        return static::DEFAULT;
67
    }
68
69
    /**
70
     * @return bool
71
     */
72
    public function isSensitive(): bool
73
    {
74
        return (bool) static::SENSITIVE;
75
    }
76
77
    /**
78
     * @param mixed $value
79
     * @return mixed
80
     */
81
    public function processValue($value)
82
    {
83
        return $value;
84
    }
85
86
87
    /**
88
     * Require that a constant exists
89
     *
90
     * @param string $name
91
     * @return mixed
92
     */
93
    final private function requireConstant(string $name)
94
    {
95
        if ($constant = constant(get_called_class() . '::' . $name)) {
96
            return $constant;
97
        } else {
98
            $caller = debug_backtrace()[1]['function'] ?? '(?)';
99
            throw new LogicException(sprintf(
100
                "%s must implement constant '%s' or method '%s'",
101
                get_called_class(),
102
                $name,
103
                $caller
104
            ));
105
        }
106
    }
107
}
108