Section::set()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XL2TP;
6
7
use InvalidArgumentException;
8
use XL2TP\Interfaces\SectionInterface;
9
use XL2TP\Interfaces\Sections\GlobalInterface;
10
use XL2TP\Interfaces\Sections\LacInterface;
11
use XL2TP\Interfaces\Sections\LnsInterface;
12
13
class Section implements SectionInterface
14
{
15
    /**
16
     * Default name of section
17
     *
18
     * @var string
19
     */
20
    public $section = 'global';
21
22
    /**
23
     * Default suffix of section
24
     *
25
     * @var string
26
     */
27
    public $suffix = 'default';
28
29
    /**
30
     * List of preconfigurationured parameters
31
     *
32
     * @var array
33
     */
34
    public $parameters = [];
35
36
    /**
37
     * List of allowed parameters
38
     *
39
     * @var array
40
     */
41
    private $allowed;
42
43
    /**
44
     * Binding to allowed lists of parameters
45
     */
46
    public const RELATIONS = [
47
        'global' => GlobalInterface::class,
48
        'lns'    => LnsInterface::class,
49
        'lac'    => LacInterface::class,
50
    ];
51
52
    /**
53
     * Section constructor.
54
     *
55
     * @param string|null $section
56
     * @param string|null $suffix
57
     *
58
     * @throws \InvalidArgumentException If section is not allowed
59
     */
60 15
    public function __construct(string $section = null, string $suffix = null)
61
    {
62
        // Set section
63 15
        if (!empty($section) && !empty(trim($section))) {
64 12
            $this->section = mb_strtolower(trim($section));
65
66
            // Check if section is allowed
67 12
            if (!array_key_exists($section, self::RELATIONS)) {
68 1
                throw new InvalidArgumentException('Section "' . $section . '" is not allowed');
69
            }
70
        }
71
72
        // Set suffix
73 15
        if (!empty($suffix) && !empty(trim($suffix))) {
74 10
            $this->suffix = mb_strtolower(trim($suffix));
75
        }
76
77
        // Extract allowed list
78
        /** @var \XL2TP\Interfaces\Sections\GlobalInterface|\XL2TP\Interfaces\Sections\LnsInterface|\XL2TP\Interfaces\Sections\LacInterface $allowed */
79 15
        $allowed       = self::RELATIONS[$this->section];
80 15
        $this->allowed = $allowed::ALLOWED;
81 15
    }
82
83
    /**
84
     * Check if section has provided parameter
85
     *
86
     * @param string $key
87
     *
88
     * @return bool
89
     */
90 3
    public function has(string $key): bool
91
    {
92 3
        return isset($this->parameters[$key]);
93
    }
94
95
    /**
96
     * Get value of section
97
     *
98
     * @param string $key
99
     *
100
     * @return string|int|null
101
     * @throws InvalidArgumentException
102
     */
103 3
    public function get(string $key)
104
    {
105 3
        return $this->parameters[$key];
106
    }
107
108
    /**
109
     * Set parameter of section
110
     *
111
     * @param string          $key
112
     * @param string|int|null $value
113
     *
114
     * @return SectionInterface
115
     */
116 14
    public function set(string $key, $value = null): SectionInterface
117
    {
118 14
        $key = Helpers::decamelize($key);
119 14
        if (!in_array($key, $this->allowed, true)) {
120 2
            throw new InvalidArgumentException('Parameter "' . $key . '" is not allowed');
121
        }
122
123
        // Unset empty value
124 14
        if (empty($value)) {
125 1
            $this->unset($key);
126
        } else {
127 14
            $this->parameters[$key] = $value;
128
        }
129
130 14
        return $this;
131
    }
132
133
    /**
134
     * Remove provided parameter
135
     *
136
     * @param string $key
137
     *
138
     * @return void
139
     */
140 1
    public function unset(string $key): void
141
    {
142 1
        unset($this->parameters[$key]);
143 1
    }
144
145
    /**
146
     * Alias to `->get()`
147
     *
148
     * @param string $key
149
     *
150
     * @return string|int|null
151
     */
152 2
    public function __get(string $key)
153
    {
154 2
        return $this->get($key);
155
    }
156
157
    /**
158
     * Alias to `->has()`
159
     *
160
     * @param string $key
161
     *
162
     * @return bool
163
     */
164 2
    public function __isset(string $key): bool
165
    {
166 2
        return $this->has($key);
167
    }
168
169
    /**
170
     * Alias to `->set()`
171
     *
172
     * @param string          $key
173
     * @param string|int|null $value
174
     */
175 12
    public function __set(string $key, $value = null)
176
    {
177 12
        $this->set($key, $value);
178 12
    }
179
}
180