3
3
namespace Selective \Config ;
4
4
5
5
use Cake \Chronos \Chronos ;
6
- use Exception ;
7
6
use InvalidArgumentException ;
8
7
9
8
/**
@@ -32,13 +31,15 @@ public function __construct(array $data = [])
32
31
* @param string $key The key
33
32
* @param int|null $default The default value
34
33
*
34
+ * @throws InvalidArgumentException
35
+ *
35
36
* @return int The value
36
37
*/
37
38
public function getInt (string $ key , int $ default = null ): int
38
39
{
39
40
$ value = $ this ->find ($ key , $ default );
40
41
41
- if ($ value === null ) {
42
+ if ($ this -> isNullOrBlank ( $ value) ) {
42
43
throw new InvalidArgumentException (sprintf ('No value found for key "%s" ' , $ key ));
43
44
}
44
45
@@ -49,19 +50,19 @@ public function getInt(string $key, int $default = null): int
49
50
* Get value as integer or null.
50
51
*
51
52
* @param string $key The key
52
- * @param int $default The default value
53
+ * @param int|null $default The default value
53
54
*
54
55
* @return int|null The value
55
56
*/
56
57
public function findInt (string $ key , int $ default = null )
57
58
{
58
- $ result = $ this ->find ($ key , $ default );
59
+ $ value = $ this ->find ($ key , $ default );
59
60
60
- if ($ result === null ) {
61
+ if ($ this -> isNullOrBlank ( $ value ) ) {
61
62
return null ;
62
63
}
63
64
64
- return (int )$ result ;
65
+ return (int )$ value ;
65
66
}
66
67
67
68
/**
@@ -70,6 +71,8 @@ public function findInt(string $key, int $default = null)
70
71
* @param string $key The key
71
72
* @param string|null $default The default value
72
73
*
74
+ * @throws InvalidArgumentException
75
+ *
73
76
* @return string The value
74
77
*/
75
78
public function getString (string $ key , string $ default = null ): string
@@ -108,13 +111,15 @@ public function findString(string $key, string $default = null)
108
111
* @param string $key The key
109
112
* @param array|null $default The default value
110
113
*
114
+ * @throws InvalidArgumentException
115
+ *
111
116
* @return array The value
112
117
*/
113
118
public function getArray (string $ key , array $ default = null ): array
114
119
{
115
120
$ value = $ this ->find ($ key , $ default );
116
121
117
- if ($ value === null ) {
122
+ if ($ this -> isNullOrBlank ( $ value) ) {
118
123
throw new InvalidArgumentException (sprintf ('No value found for key "%s" ' , $ key ));
119
124
}
120
125
@@ -133,7 +138,7 @@ public function findArray(string $key, array $default = null)
133
138
{
134
139
$ value = $ this ->find ($ key , $ default );
135
140
136
- if ($ value === null ) {
141
+ if ($ this -> isNullOrBlank ( $ value) ) {
137
142
return null ;
138
143
}
139
144
@@ -146,13 +151,15 @@ public function findArray(string $key, array $default = null)
146
151
* @param string $key The key
147
152
* @param float|null $default The default value
148
153
*
154
+ * @throws InvalidArgumentException
155
+ *
149
156
* @return float The value
150
157
*/
151
158
public function getFloat (string $ key , float $ default = null ): float
152
159
{
153
160
$ value = $ this ->find ($ key , $ default );
154
161
155
- if ($ value === null ) {
162
+ if ($ this -> isNullOrBlank ( $ value) ) {
156
163
throw new InvalidArgumentException (sprintf ('No value found for key "%s" ' , $ key ));
157
164
}
158
165
@@ -171,7 +178,7 @@ public function findFloat(string $key, float $default = null)
171
178
{
172
179
$ value = $ this ->find ($ key , $ default );
173
180
174
- if ($ value === null ) {
181
+ if ($ this -> isNullOrBlank ( $ value) ) {
175
182
return null ;
176
183
}
177
184
@@ -184,13 +191,15 @@ public function findFloat(string $key, float $default = null)
184
191
* @param string $key The key
185
192
* @param bool|null $default The default value
186
193
*
194
+ * @throws InvalidArgumentException
195
+ *
187
196
* @return bool The value
188
197
*/
189
198
public function getBool (string $ key , bool $ default = null ): bool
190
199
{
191
200
$ value = $ this ->find ($ key , $ default );
192
201
193
- if ($ value === null ) {
202
+ if ($ this -> isNullOrBlank ( $ value) ) {
194
203
throw new InvalidArgumentException (sprintf ('No value found for key "%s" ' , $ key ));
195
204
}
196
205
@@ -209,7 +218,7 @@ public function findBool(string $key, bool $default = null)
209
218
{
210
219
$ value = $ this ->find ($ key , $ default );
211
220
212
- if ($ value === null ) {
221
+ if ($ this -> isNullOrBlank ( $ value) ) {
213
222
return null ;
214
223
}
215
224
@@ -222,13 +231,15 @@ public function findBool(string $key, bool $default = null)
222
231
* @param string $key The key
223
232
* @param Chronos|null $default The default value
224
233
*
234
+ * @throws InvalidArgumentException
235
+ *
225
236
* @return Chronos The value
226
237
*/
227
238
public function getChronos (string $ key , Chronos $ default = null ): Chronos
228
239
{
229
240
$ value = $ this ->find ($ key , $ default );
230
241
231
- if ($ value === null ) {
242
+ if ($ this -> isNullOrBlank ( $ value) ) {
232
243
throw new InvalidArgumentException (sprintf ('No value found for key "%s" ' , $ key ));
233
244
}
234
245
@@ -245,15 +256,17 @@ public function getChronos(string $key, Chronos $default = null): Chronos
245
256
* @param string $key The key
246
257
* @param Chronos $default The default value
247
258
*
248
- * @throws Exception Chronos date time parsing error
249
- *
250
259
* @return Chronos|null The value
251
260
*/
252
261
public function findChronos (string $ key , Chronos $ default = null )
253
262
{
254
263
$ value = $ this ->find ($ key , $ default );
255
264
256
- if ($ value === null || $ value instanceof Chronos) {
265
+ if ($ this ->isNullOrBlank ($ value )) {
266
+ return null ;
267
+ }
268
+
269
+ if ($ value instanceof Chronos) {
257
270
return $ value ;
258
271
}
259
272
@@ -285,12 +298,24 @@ public function find(string $path, $default = null)
285
298
}
286
299
287
300
/**
288
- * Return all data as array.
301
+ * Return all settings as array.
289
302
*
290
303
* @return array The data
291
304
*/
292
305
public function all (): array
293
306
{
294
307
return $ this ->data ;
295
308
}
309
+
310
+ /**
311
+ * Is null or blank.
312
+ *
313
+ * @param mixed $value The value
314
+ *
315
+ * @return bool The status
316
+ */
317
+ private function isNullOrBlank ($ value ): bool
318
+ {
319
+ return $ value === null || $ value === '' ;
320
+ }
296
321
}
0 commit comments