1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\Config; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Abstract base class for config components. |
8
|
|
|
* |
9
|
|
|
* #### History |
10
|
|
|
* - *2021-01-01* initial version |
11
|
|
|
* |
12
|
|
|
* @package SKien/GCalendar |
13
|
|
|
* @version 1.0.0 |
14
|
|
|
* @author Stefanius <[email protected]> |
15
|
|
|
* @copyright MIT License - see the LICENSE file for details |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractConfig implements ConfigInterface |
18
|
|
|
{ |
19
|
|
|
/** @var array holding the config data */ |
20
|
|
|
protected ?array $aConfig = null; |
21
|
|
|
/** @var string format for date parameters */ |
22
|
|
|
protected string $strDateFormat = 'Y-m-d'; |
23
|
|
|
/** @var string format for datetime parameters */ |
24
|
|
|
protected string $strDateTimeFormat = 'Y-m-d H:i'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The constructor expects an valid filename/path to the JSON file. |
28
|
|
|
* @param string $strConfigFile |
29
|
|
|
*/ |
30
|
|
|
public function __construct(string $strConfigFile) |
31
|
|
|
{ |
32
|
|
|
$this->aConfig = $this->parseFile($strConfigFile); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set the format for date parameters. |
37
|
|
|
* See the formatting options DateTime::createFromFormat. |
38
|
|
|
* In most cases, the same letters as for the date() can be used. |
39
|
|
|
* @param string $strFormat |
40
|
|
|
* @link https://www.php.net/manual/en/datetime.createfromformat.php |
41
|
|
|
*/ |
42
|
|
|
public function setDateFormat(string $strFormat) : void |
43
|
|
|
{ |
44
|
|
|
$this->strDateFormat = $strFormat; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set the format for datetime parameters. |
49
|
|
|
* See the formatting options DateTime::createFromFormat. |
50
|
|
|
* In most cases, the same letters as for the date() can be used. |
51
|
|
|
* @param string $strFormat |
52
|
|
|
* @link https://www.php.net/manual/en/datetime.createfromformat.php |
53
|
|
|
*/ |
54
|
|
|
public function setDateTimeFormat(string $strFormat) : void |
55
|
|
|
{ |
56
|
|
|
$this->strDateTimeFormat = $strFormat; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the value specified by path. |
61
|
|
|
* @param string $strPath |
62
|
|
|
* @param mixed $default |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function getValue(string $strPath, $default = null) |
66
|
|
|
{ |
67
|
|
|
if ($this->aConfig === null) { |
68
|
|
|
// without valid config file just return the default value |
69
|
|
|
return $default; |
70
|
|
|
} |
71
|
|
|
$aPath = $this->splitPath($strPath); |
72
|
|
|
$iDepth = count($aPath); |
73
|
|
|
$value = null; |
74
|
|
|
$aValues = $this->aConfig; |
75
|
|
|
for ($i = 0; $i < $iDepth; $i++) { |
76
|
|
|
if (!is_array($aValues)) { |
77
|
|
|
$value = null; |
78
|
|
|
break; |
79
|
|
|
} |
80
|
|
|
$value = $aValues[$aPath[$i]] ?? null; |
81
|
|
|
if ($value === null) { |
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
$aValues = $value; |
85
|
|
|
} |
86
|
|
|
return $value ?? $default; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the string value specified by path. |
91
|
|
|
* @param string $strPath |
92
|
|
|
* @param string $strDefault |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getString(string $strPath, string $strDefault = '') : string |
96
|
|
|
{ |
97
|
|
|
return (string) $this->getValue($strPath, $strDefault); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the integer value specified by path. |
102
|
|
|
* @param string $strPath |
103
|
|
|
* @param int $iDefault |
104
|
|
|
* @return int |
105
|
|
|
*/ |
106
|
|
|
public function getInt(string $strPath, int $iDefault = 0) : int |
107
|
|
|
{ |
108
|
|
|
return intval($this->getValue($strPath, $iDefault)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the integer value specified by path. |
113
|
|
|
* @param string $strPath |
114
|
|
|
* @param float $fltDefault |
115
|
|
|
* @return float |
116
|
|
|
*/ |
117
|
|
|
public function getFloat(string $strPath, float $fltDefault = 0.0) : float |
118
|
|
|
{ |
119
|
|
|
return floatval($this->getValue($strPath, $fltDefault)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the boolean value specified by path. |
124
|
|
|
* @param string $strPath |
125
|
|
|
* @param bool $bDefault |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function getBool(string $strPath, bool $bDefault = false) : bool |
129
|
|
|
{ |
130
|
|
|
return (bool) $this->getValue($strPath, $bDefault); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the date value specified by path. |
135
|
|
|
* @param string $strPath |
136
|
|
|
* @param mixed $default default value (unix timestamp, DateTime object or date string) |
137
|
|
|
* @return int date as unix timestamp |
138
|
|
|
*/ |
139
|
|
|
public function getDate(string $strPath, $default = 0) : int |
140
|
|
|
{ |
141
|
|
|
$date = $this->getValue($strPath, $default); |
142
|
|
|
if (!is_int($date)) { |
143
|
|
|
$dt = \DateTime::createFromFormat($this->strDateFormat, $date); |
144
|
|
|
$date = $default; |
145
|
|
|
if ($dt !== false) { |
146
|
|
|
$aError = $dt->getLastErrors(); |
147
|
|
|
if ($aError['error_count'] == 0) { |
148
|
|
|
$dt->setTime(0, 0); |
149
|
|
|
$date = $dt->getTimestamp(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
return $date; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the date and time value specified by path as unix timestamp. |
158
|
|
|
* @param string $strPath |
159
|
|
|
* @param int $default default value (unix timestamp) |
160
|
|
|
* @return int unix timestamp |
161
|
|
|
*/ |
162
|
|
|
public function getDateTime(string $strPath, $default = 0) : int |
163
|
|
|
{ |
164
|
|
|
$date = $this->getValue($strPath, $default); |
165
|
|
|
if (!is_int($date)) { |
166
|
|
|
$dt = \DateTime::createFromFormat($this->strDateTimeFormat, $date); |
167
|
|
|
$date = $default; |
168
|
|
|
if ($dt !== false) { |
169
|
|
|
$aError = $dt->getLastErrors(); |
170
|
|
|
if ($aError['error_count'] == 0) { |
171
|
|
|
$date = $dt->getTimestamp(); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
return $date; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the array specified by path. |
180
|
|
|
* @param string $strPath |
181
|
|
|
* @param array $aDefault |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
|
|
public function getArray(string $strPath, array $aDefault = []) : array |
185
|
|
|
{ |
186
|
|
|
$value = $this->getValue($strPath, $aDefault); |
187
|
|
|
return is_array($value) ? $value : $aDefault; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Parse the given file an add all settings to the internal configuration. |
192
|
|
|
* Method MUST be implemented in derived classes! |
193
|
|
|
* @param string $strConfigFile |
194
|
|
|
*/ |
195
|
|
|
protected function parseFile(string $strConfigFile) : array |
196
|
|
|
{ |
197
|
|
|
trigger_error('parseFile() MUST be implemented in class ' . get_class($this) . '!', E_USER_ERROR); |
198
|
|
|
return []; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Split the given path in its components. |
203
|
|
|
* @param string $strPath |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
protected function splitPath(string $strPath) : array |
207
|
|
|
{ |
208
|
|
|
return explode('.', $strPath); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Checks whether the passed value is a valid expression for bool 'True'. |
213
|
|
|
* Accepted values for bool 'true' are (case insensitiv): <i>true, on, yes, 1</i> |
214
|
|
|
* @param string $strValue |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
|
|
protected function isTrue(string $strValue) : bool |
218
|
|
|
{ |
219
|
|
|
$strValue = strtolower($strValue); |
220
|
|
|
return in_array($strValue, ['true', 'on', 'yes', '1']); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Checks whether the passed value is a valid expression for bool 'False'. |
225
|
|
|
* Accepted values for bool 'false' are (case insensitiv): <i>false, off, no, none, 0</i> |
226
|
|
|
* @param string $strValue |
227
|
|
|
* @return bool |
228
|
|
|
*/ |
229
|
|
|
protected function isFalse(string $strValue) : bool |
230
|
|
|
{ |
231
|
|
|
$strValue = strtolower($strValue); |
232
|
|
|
return in_array($strValue, ['false', 'off', 'no', 'none', '0']); |
233
|
|
|
} |
234
|
|
|
} |