Test Failed
Push — master ( 16c22a...3a800f )
by Julien
03:51
created

Options::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 2
ccs 0
cts 1
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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 = null)
19
    {
20
        $this->initializeOptions($options);
21
    }
22
    
23
    /**
24
     * Initialize Options
25
     * @param array|null $options
26
     * @return void
27
     */
28
    public function initializeOptions(array $options = null) {
29
        $options ??= [];
30
        $this->defaultOptions = $options;
31
        $this->setOptions($options);
32
        $this->initialize();
33
    }
34
    
35
    /**
36
     * Classes can use this as a constructor after options are initialized
37
     * @return void
38
     */
39
    public function initialize()
40
    {
41
    
42
    }
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...
43
    
44
    /**
45
     * Set options array
46
     * @param array $options
47
     * @return void
48
     */
49
    public function setOptions(array $options): void
50
    {
51
        $this->options = $options;
52
    }
53
    
54
    /**
55
     * Get options array
56
     * @return array
57
     */
58
    public function getOptions(): array
59
    {
60
        return $this->options;
61
    }
62
    
63
    /**
64
     * Set an option value
65
     * @param string $key
66
     * @param mixed|null $value
67
     * @return void
68
     */
69
    public function setOption(string $key, $value = null): void
70
    {
71
        $this->options[$key] = $value;
72
    }
73
    
74
    /**
75
     * Retrieve an option value by key
76
     * - Return default value if null
77
     * @param string $key The key to retrieve
78
     * @param mixed|null $default Returned if the option couldn't be found
79
     * @return mixed|null
80
     */
81
    public function getOption(string $key, $default = null)
82
    {
83
        return $this->options[$key] ?? $default;
84
    }
85
    
86
    /**
87
     * Remove an option by key
88
     * @param string $key
89
     * @return void
90
     */
91
    public function removeOption(string $key): void
92
    {
93
        if (isset($this->options[$key])) {
94
            unset($this->options[$key]);
95
        }
96
    }
97
    
98
    /**
99
     * Reset options to default values
100
     * @return void
101
     */
102
    public function resetOptions(): void
103
    {
104
        $this->setOptions($this->defaultOptions);
105
    }
106
    
107
    /**
108
     * Clear options by forcing an empty array
109
     * @return void
110
     */
111
    public function clearOptions(): void
112
    {
113
        $this->options = [];
114
    }
115
}
116