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