Skip to content

Commit d0e2505

Browse files
committed
Add null and blank value handling
1 parent 22438e6 commit d0e2505

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

src/Configuration.php

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Selective\Config;
44

55
use Cake\Chronos\Chronos;
6-
use Exception;
76
use InvalidArgumentException;
87

98
/**
@@ -32,13 +31,15 @@ public function __construct(array $data = [])
3231
* @param string $key The key
3332
* @param int|null $default The default value
3433
*
34+
* @throws InvalidArgumentException
35+
*
3536
* @return int The value
3637
*/
3738
public function getInt(string $key, int $default = null): int
3839
{
3940
$value = $this->find($key, $default);
4041

41-
if ($value === null) {
42+
if ($this->isNullOrBlank($value)) {
4243
throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key));
4344
}
4445

@@ -49,19 +50,19 @@ public function getInt(string $key, int $default = null): int
4950
* Get value as integer or null.
5051
*
5152
* @param string $key The key
52-
* @param int $default The default value
53+
* @param int|null $default The default value
5354
*
5455
* @return int|null The value
5556
*/
5657
public function findInt(string $key, int $default = null)
5758
{
58-
$result = $this->find($key, $default);
59+
$value = $this->find($key, $default);
5960

60-
if ($result === null) {
61+
if ($this->isNullOrBlank($value)) {
6162
return null;
6263
}
6364

64-
return (int)$result;
65+
return (int)$value;
6566
}
6667

6768
/**
@@ -70,6 +71,8 @@ public function findInt(string $key, int $default = null)
7071
* @param string $key The key
7172
* @param string|null $default The default value
7273
*
74+
* @throws InvalidArgumentException
75+
*
7376
* @return string The value
7477
*/
7578
public function getString(string $key, string $default = null): string
@@ -108,13 +111,15 @@ public function findString(string $key, string $default = null)
108111
* @param string $key The key
109112
* @param array|null $default The default value
110113
*
114+
* @throws InvalidArgumentException
115+
*
111116
* @return array The value
112117
*/
113118
public function getArray(string $key, array $default = null): array
114119
{
115120
$value = $this->find($key, $default);
116121

117-
if ($value === null) {
122+
if ($this->isNullOrBlank($value)) {
118123
throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key));
119124
}
120125

@@ -133,7 +138,7 @@ public function findArray(string $key, array $default = null)
133138
{
134139
$value = $this->find($key, $default);
135140

136-
if ($value === null) {
141+
if ($this->isNullOrBlank($value)) {
137142
return null;
138143
}
139144

@@ -146,13 +151,15 @@ public function findArray(string $key, array $default = null)
146151
* @param string $key The key
147152
* @param float|null $default The default value
148153
*
154+
* @throws InvalidArgumentException
155+
*
149156
* @return float The value
150157
*/
151158
public function getFloat(string $key, float $default = null): float
152159
{
153160
$value = $this->find($key, $default);
154161

155-
if ($value === null) {
162+
if ($this->isNullOrBlank($value)) {
156163
throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key));
157164
}
158165

@@ -171,7 +178,7 @@ public function findFloat(string $key, float $default = null)
171178
{
172179
$value = $this->find($key, $default);
173180

174-
if ($value === null) {
181+
if ($this->isNullOrBlank($value)) {
175182
return null;
176183
}
177184

@@ -184,13 +191,15 @@ public function findFloat(string $key, float $default = null)
184191
* @param string $key The key
185192
* @param bool|null $default The default value
186193
*
194+
* @throws InvalidArgumentException
195+
*
187196
* @return bool The value
188197
*/
189198
public function getBool(string $key, bool $default = null): bool
190199
{
191200
$value = $this->find($key, $default);
192201

193-
if ($value === null) {
202+
if ($this->isNullOrBlank($value)) {
194203
throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key));
195204
}
196205

@@ -209,7 +218,7 @@ public function findBool(string $key, bool $default = null)
209218
{
210219
$value = $this->find($key, $default);
211220

212-
if ($value === null) {
221+
if ($this->isNullOrBlank($value)) {
213222
return null;
214223
}
215224

@@ -222,13 +231,15 @@ public function findBool(string $key, bool $default = null)
222231
* @param string $key The key
223232
* @param Chronos|null $default The default value
224233
*
234+
* @throws InvalidArgumentException
235+
*
225236
* @return Chronos The value
226237
*/
227238
public function getChronos(string $key, Chronos $default = null): Chronos
228239
{
229240
$value = $this->find($key, $default);
230241

231-
if ($value === null) {
242+
if ($this->isNullOrBlank($value)) {
232243
throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key));
233244
}
234245

@@ -245,15 +256,17 @@ public function getChronos(string $key, Chronos $default = null): Chronos
245256
* @param string $key The key
246257
* @param Chronos $default The default value
247258
*
248-
* @throws Exception Chronos date time parsing error
249-
*
250259
* @return Chronos|null The value
251260
*/
252261
public function findChronos(string $key, Chronos $default = null)
253262
{
254263
$value = $this->find($key, $default);
255264

256-
if ($value === null || $value instanceof Chronos) {
265+
if ($this->isNullOrBlank($value)) {
266+
return null;
267+
}
268+
269+
if ($value instanceof Chronos) {
257270
return $value;
258271
}
259272

@@ -285,12 +298,24 @@ public function find(string $path, $default = null)
285298
}
286299

287300
/**
288-
* Return all data as array.
301+
* Return all settings as array.
289302
*
290303
* @return array The data
291304
*/
292305
public function all(): array
293306
{
294307
return $this->data;
295308
}
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+
}
296321
}

0 commit comments

Comments
 (0)