Skip to content

Commit f68d517

Browse files
committed
Added methods for arrays.
1 parent ed3388a commit f68d517

File tree

4 files changed

+166
-2
lines changed

4 files changed

+166
-2
lines changed

src/TypedConfig.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class TypedConfig
2121
private $config;
2222

2323
//--------------------------------------------------------------------------------------------------------------------
24-
2524
/**
2625
* TypedConfig constructor.
2726
*
@@ -58,6 +57,28 @@ public function getConfig(): Config
5857
return $this->config;
5958
}
6059

60+
//--------------------------------------------------------------------------------------------------------------------
61+
/**
62+
* Returns the value of a mandatory nested configuration setting.
63+
*
64+
* @param string $key The key of the configuration setting. The key might be nested using dot notation.
65+
* @param array|null $default The default value.
66+
*
67+
* @return array
68+
*
69+
* @throws TypedConfigException
70+
*/
71+
public function getManArray(string $key, ?array $default = null): array
72+
{
73+
$value = $this->config->get($key, $default);
74+
if (is_array($value))
75+
{
76+
return $value;
77+
}
78+
79+
throw self::createException($key, 'array', $value);
80+
}
81+
6182
//--------------------------------------------------------------------------------------------------------------------
6283
/**
6384
* Returns the value of a mandatory boolean configuration setting.
@@ -178,6 +199,28 @@ public function getManString(string $key, ?string $default = null): string
178199
}
179200
}
180201

202+
//--------------------------------------------------------------------------------------------------------------------
203+
/**
204+
* Returns the value of an optional nested configuration setting.
205+
*
206+
* @param string $key The key of the configuration setting. The key might be nested using dot notation.
207+
* @param array|null $default The default value.
208+
*
209+
* @return array|null
210+
*
211+
* @throws TypedConfigException
212+
*/
213+
public function getOptArray(string $key, ?array $default = null): ?array
214+
{
215+
$value = $this->config->get($key, $default);
216+
if ($value===null || is_array($value))
217+
{
218+
return $value;
219+
}
220+
221+
throw self::createException($key, 'array', $value);
222+
}
223+
181224
//--------------------------------------------------------------------------------------------------------------------
182225
/**
183226
* Returns the value of an optional boolean configuration setting.

src/TypedConfigException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private static function message(string $key, string $type, $value): string
4040
return sprintf("Empty mandatory %s value found for key '%s'", $type, $key);
4141
}
4242

43-
$a = ($type=='integer') ? 'an' : 'a';
43+
$a = (in_array($type, ['array', 'integer'])) ? 'an' : 'a';
4444

4545
if (Cast::isManString($value))
4646
{

test/TypedConfigTest.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ class TypedConfigTest extends TestCase
2828
*/
2929
private $typeConfig;
3030

31+
//--------------------------------------------------------------------------------------------------------------------
32+
/**
33+
* Data provider for invalid mandatory arrays.
34+
*
35+
* @return array
36+
*/
37+
public function invalidManArrayCases(): array
38+
{
39+
$cases = $this->invalidOptArrayCases();
40+
41+
$cases[] = ['null.array', null];
42+
43+
return $cases;
44+
}
45+
3146
//--------------------------------------------------------------------------------------------------------------------
3247
/**
3348
* Data provider for invalid mandatory booleans.
@@ -103,6 +118,17 @@ public function invalidManStringCases(): array
103118
return $cases;
104119
}
105120

121+
//--------------------------------------------------------------------------------------------------------------------
122+
/**
123+
* Data provider for invalid optional arrays.
124+
*
125+
* @return array
126+
*/
127+
public function invalidOptArrayCases(): array
128+
{
129+
return [['invalid.array', null]];
130+
}
131+
106132
//--------------------------------------------------------------------------------------------------------------------
107133
/**
108134
* Data provider for invalid optional booleans.
@@ -178,6 +204,21 @@ public function testGetConfig(): void
178204
self::assertSame($this->config, $this->typeConfig->getConfig());
179205
}
180206

207+
//--------------------------------------------------------------------------------------------------------------------
208+
/**
209+
* Test case for invalid mandatory array.
210+
*
211+
* @param string $key The key.
212+
* @param array|null $default The default value.
213+
*
214+
* @dataProvider invalidManArrayCases
215+
*/
216+
public function testInvalidArrayBool(string $key, ?array $default): void
217+
{
218+
$this->expectException(TypedConfigException::class);
219+
$this->typeConfig->getManArray($key, $default);
220+
}
221+
181222
//--------------------------------------------------------------------------------------------------------------------
182223
/**
183224
* Test case for invalid mandatory boolean.
@@ -253,6 +294,21 @@ public function testInvalidManString(string $key, ?int $default): void
253294
$this->typeConfig->getManString($key, $default);
254295
}
255296

297+
//--------------------------------------------------------------------------------------------------------------------
298+
/**
299+
* Test case for invalid optional array.
300+
*
301+
* @param string $key The key.
302+
* @param array|null $default The default value.
303+
*
304+
* @dataProvider invalidOptArrayCases
305+
*/
306+
public function testInvalidOptArray(string $key, ?array $default): void
307+
{
308+
$this->expectException(TypedConfigException::class);
309+
$this->typeConfig->getOptArray($key, $default);
310+
}
311+
256312
//--------------------------------------------------------------------------------------------------------------------
257313
/**
258314
* Test case for invalid optional boolean.
@@ -344,6 +400,22 @@ public function testValidInt(string $key, ?int $default, ?int $expected): void
344400
self::assertSame($value, $expected);
345401
}
346402

403+
//--------------------------------------------------------------------------------------------------------------------
404+
/**
405+
* Test case for mandatory array.
406+
*
407+
* @param string $key The key.
408+
* @param array|null $default The default value.
409+
* @param array|null $expected The expected value.
410+
*
411+
* @dataProvider validManArrayCases
412+
*/
413+
public function testValidManArray(string $key, ?array $default, ?array $expected): void
414+
{
415+
$value = $this->typeConfig->getManArray($key, $default);
416+
self::assertSame($value, $expected);
417+
}
418+
347419
//--------------------------------------------------------------------------------------------------------------------
348420
/**
349421
* Test case for mandatory boolean.
@@ -392,6 +464,22 @@ public function testValidManFloat(string $key, ?float $default, ?float $expected
392464
self::assertSame($value, $expected);
393465
}
394466

467+
//--------------------------------------------------------------------------------------------------------------------
468+
/**
469+
* Test case for optional array.
470+
*
471+
* @param string $key The key.
472+
* @param array|null $default The default value.
473+
* @param array|null $expected The expected value.
474+
*
475+
* @dataProvider validOptArrayCases
476+
*/
477+
public function testValidOptArray(string $key, ?array $default, ?array $expected): void
478+
{
479+
$value = $this->typeConfig->getOptArray($key, $default);
480+
self::assertSame($value, $expected);
481+
}
482+
395483
//--------------------------------------------------------------------------------------------------------------------
396484
/**
397485
* Test case for optional boolean.
@@ -488,6 +576,18 @@ public function testValidString(string $key, ?string $default, ?string $expected
488576
self::assertSame($value, $expected);
489577
}
490578

579+
//--------------------------------------------------------------------------------------------------------------------
580+
/**
581+
* Data provider for valid mandatory arrays.
582+
*
583+
* @return array
584+
*/
585+
public function validManArrayCases(): array
586+
{
587+
return [['valid-array', null, ['one' => '1', 'two' => '2', 'three' => '3']],
588+
['null.array', [], []]];
589+
}
590+
491591
//--------------------------------------------------------------------------------------------------------------------
492592
/**
493593
* Data provider for valid mandatory booleans.
@@ -551,6 +651,21 @@ public function validManStringCases(): array
551651
['null.string', 'default', 'default']];
552652
}
553653

654+
//--------------------------------------------------------------------------------------------------------------------
655+
/**
656+
* Data provider for valid optional arrays.
657+
*
658+
* @return array
659+
*/
660+
public function validOptArrayCases(): array
661+
{
662+
$cases = $this->validManArrayCases();
663+
664+
$cases[] = ['null.array', null, null];
665+
666+
return $cases;
667+
}
668+
554669
//--------------------------------------------------------------------------------------------------------------------
555670
/**
556671
* Data provider for valid optional booleans.

test/test.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ integer = 123
66
string = "Hello, world!"
77

88
[invalid]
9+
array = not an array
910
bool = not a boolean
1011
finite-float = not a float
1112
float = not a float
1213
integer = not an integer
1314
string = replace with handle
15+
16+
[valid-array]
17+
one = 1
18+
two = 2
19+
three = 3

0 commit comments

Comments
 (0)