1 | <?php |
||
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 |
|
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 |
|
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 |
|
136 | |||
137 | /** |
||
138 | * Remove provided parameter |
||
139 | * |
||
140 | * @param string $key |
||
141 | * |
||
142 | * @return void |
||
143 | */ |
||
144 | 1 | public function unset(string $key): void |
|
148 | |||
149 | /** |
||
150 | * Alias to `->get()` |
||
151 | * |
||
152 | * @param string $key |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | 1 | public function __get(string $key): string |
|
160 | |||
161 | /** |
||
162 | * Alias to `->has()` |
||
163 | * |
||
164 | * @param string $key |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | 2 | public function __isset(string $key): bool |
|
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) |
|
183 | } |
||
184 |