Skip to content

Commit 6afc479

Browse files
committed
add more Guards
fixes #117
1 parent 54ac05a commit 6afc479

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

src/Guards.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515

1616
final class Guards
1717
{
18+
/**
19+
* Assert that input is not an empty string.
20+
*
21+
* @throws \League\ISO3166\Exception\DomainException if input is an empty string
22+
*/
23+
public static function guardAgainstInvalidName(string $name): void
24+
{
25+
if ('' === trim($name)) {
26+
throw new DomainException('Expected string, got empty string');
27+
}
28+
}
29+
1830
/**
1931
* Assert that input looks like an alpha2 key.
2032
*

src/ISO3166.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public function __construct(array $countries = [])
4343
*/
4444
public function name(string $name): array
4545
{
46+
Guards::guardAgainstInvalidName($name);
47+
4648
return $this->lookup(self::KEY_NAME, $name);
4749
}
4850

@@ -81,6 +83,8 @@ public function numeric(string $numeric): array
8183
*/
8284
public function exactName(string $name): array
8385
{
86+
Guards::guardAgainstInvalidName($name);
87+
8488
$value = mb_strtolower($name);
8589

8690
foreach ($this->countries as $country) {

src/ISO3166DataValidator.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,26 @@ public function validate(array $data): array
3636
*/
3737
private function assertEntryHasRequiredKeys(array $entry): void
3838
{
39+
if (!isset($entry[ISO3166::KEY_NAME])) {
40+
throw new DomainException('Each data entry must have a name key.');
41+
}
42+
43+
Guards::guardAgainstInvalidName($entry[ISO3166::KEY_NAME]);
44+
3945
if (!isset($entry[ISO3166::KEY_ALPHA2])) {
40-
throw new DomainException('Each data entry must have a valid alpha2 key.');
46+
throw new DomainException('Each data entry must have a alpha2 key.');
4147
}
4248

4349
Guards::guardAgainstInvalidAlpha2($entry[ISO3166::KEY_ALPHA2]);
4450

4551
if (!isset($entry[ISO3166::KEY_ALPHA3])) {
46-
throw new DomainException('Each data entry must have a valid alpha3 key.');
52+
throw new DomainException('Each data entry must have a alpha3 key.');
4753
}
4854

4955
Guards::guardAgainstInvalidAlpha3($entry[ISO3166::KEY_ALPHA3]);
5056

5157
if (!isset($entry[ISO3166::KEY_NUMERIC])) {
52-
throw new DomainException('Each data entry must have a valid numeric key.');
58+
throw new DomainException('Each data entry must have a numeric key.');
5359
}
5460

5561
Guards::guardAgainstInvalidNumeric($entry[ISO3166::KEY_NUMERIC]);

tests/ISO3166DataValidatorTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,27 @@ public function requiredKeysProvider(): array
5353
{
5454
return [
5555
'entry missing alpha2' => [
56-
[[ISO3166::KEY_ALPHA3 => 'FOO', ISO3166::KEY_NUMERIC => '001']],
56+
[[ISO3166::KEY_ALPHA3 => 'FOO', ISO3166::KEY_NUMERIC => '001', ISO3166::KEY_NAME => 'Foo']],
5757
DomainException::class,
58-
'{^Each data entry must have a valid alpha2 key.$}',
58+
'{^Each data entry must have a alpha2 key.$}',
5959
],
6060
'entry missing alpha3' => [
61-
[[ISO3166::KEY_ALPHA2 => 'FO', ISO3166::KEY_NUMERIC => '001']],
61+
[[ISO3166::KEY_ALPHA2 => 'FO', ISO3166::KEY_NUMERIC => '001', ISO3166::KEY_NAME => 'Foo']],
6262
DomainException::class,
63-
'{^Each data entry must have a valid alpha3 key.$}',
63+
'{^Each data entry must have a alpha3 key.$}',
6464
],
6565
'entry missing numeric' => [
66-
[[ISO3166::KEY_ALPHA2 => 'FO', ISO3166::KEY_ALPHA3 => 'FOO']],
66+
[[ISO3166::KEY_ALPHA2 => 'FO', ISO3166::KEY_ALPHA3 => 'FOO', ISO3166::KEY_NAME => 'Foo']],
6767
DomainException::class,
68-
'{^Each data entry must have a valid numeric key.$}',
68+
'{^Each data entry must have a numeric key.$}',
6969
],
70-
'entry is complete' => [
70+
'entry missing name' => [
7171
[[ISO3166::KEY_ALPHA2 => 'FO', ISO3166::KEY_ALPHA3 => 'FOO', ISO3166::KEY_NUMERIC => '001']],
72+
DomainException::class,
73+
'{^Each data entry must have a name key.$}',
74+
],
75+
'entry is complete' => [
76+
[[ISO3166::KEY_ALPHA2 => 'FO', ISO3166::KEY_ALPHA3 => 'FOO', ISO3166::KEY_NUMERIC => '001', ISO3166::KEY_NAME => 'Foo']],
7277
null,
7378
null,
7479
],

tests/ISO3166Test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ public function invalidNameProvider(): array
185185
$noMatch = sprintf('{^No "%s" key found matching: .*$}', ISO3166::KEY_NAME);
186186

187187
return [
188-
['000', OutOfBoundsException::class, $noMatch],
188+
['', DomainException::class, '{^Expected string, got empty string$}'],
189+
['Dummy', OutOfBoundsException::class, $noMatch],
189190
];
190191
}
191192

@@ -221,6 +222,7 @@ public function invalidExactNameProvider(): array
221222
$noMatch = sprintf('{^No "%s" key found matching: .*$}', ISO3166::KEY_NAME);
222223

223224
return [
225+
['', DomainException::class, '{^Expected string, got empty string$}'],
224226
['FO', OutOfBoundsException::class, $noMatch],
225227
['BA', OutOfBoundsException::class, $noMatch],
226228
];

0 commit comments

Comments
 (0)