Test Failed
Push — master ( 51765d...a30637 )
by Julien
03:53
created

Options   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 14
c 1
b 0
f 0
dl 0
loc 86
ccs 0
cts 21
cp 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A removeOption() 0 4 2
A resetOptions() 0 3 1
A setOption() 0 3 1
A init() 0 1 1
A setOptions() 0 3 1
A clearOptions() 0 3 1
A __construct() 0 5 1
A getOptions() 0 3 1
A getOption() 0 3 1
1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Support;
12
13
trait Options
14
{
15
    protected array $defaultOptions = [];
16
    protected array $options = [];
17
    
18
    public function __construct(array $options = [])
19
    {
20
        $this->defaultOptions = $options;
21
        $this->setOptions($options);
22
        $this->init();
23
    }
24
    
25
    public function init() {
26
    
27
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
28
    
29
    /**
30
     * Set options array
31
     * @param array $options
32
     * @return void
33
     */
34
    public function setOptions(array $options): void
35
    {
36
        $this->options = $options;
37
    }
38
    
39
    /**
40
     * Get options array
41
     * @return array
42
     */
43
    public function getOptions(): array
44
    {
45
        return $this->options;
46
    }
47
    
48
    /**
49
     * Set an option value
50
     * @param string $key
51
     * @param $value
52
     * @return void
53
     */
54
    public function setOption(string $key, $value = null): void
55
    {
56
        $this->options[$key] = $value;
57
    }
58
    
59
    /**
60
     * Retrieve an option value by key
61
     * - Return default value if null
62
     * @param string $key The key to retrieve
63
     * @param mixed|null $default Returned if the option couldn't be found
64
     * @return mixed|null
65
     */
66
    public function getOption(string $key, $default = null)
67
    {
68
        return $this->options[$key] ?? $default;
69
    }
70
    
71
    /**
72
     * Remove an option by key
73
     * @param string $key
74
     * @return void
75
     */
76
    public function removeOption(string $key): void
77
    {
78
        if (isset($this->options[$key])) {
79
            unset($this->options[$key]);
80
        }
81
    }
82
    
83
    /**
84
     * Reset options to default values
85
     * @return void
86
     */
87
    public function resetOptions(): void
88
    {
89
        $this->setOptions($this->defaultOptions);
90
    }
91
    
92
    /**
93
     * Clear options by forcing an empty array
94
     * @return void
95
     */
96
    public function clearOptions(): void
97
    {
98
        $this->options = [];
99
    }
100
}
101