Skip to content

Commit cf4316d

Browse files
committed
feature: add getCountriesName method to Collection class
1 parent 8f09d8e commit cf4316d

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/Collection.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ public function validateLocale(string $locale): void
4949
}
5050
}
5151

52+
public function getCountriesName(string $locale = 'en'): array
53+
{
54+
$this->validateLocale($locale);
55+
56+
$result = [];
57+
foreach ($this->countries as $country) {
58+
$result[$country->alpha2] = $country->getName($locale);
59+
}
60+
61+
return $result;
62+
}
63+
5264
public function getCountriesAlpha2(string $locale = 'en'): array
5365
{
5466
$this->validateLocale($locale);

tests/Unit/CollectionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@
121121
expect($country)->toBeNull();
122122
});
123123

124+
it('returns countries with names', function () {
125+
$collection = new Collection();
126+
$countries = $collection->getCountriesName();
127+
128+
expect($countries)->toBeArray();
129+
expect($countries)->not->toBeEmpty();
130+
foreach ($countries as $alpha2 => $name) {
131+
expect($alpha2)->toBeString();
132+
expect($name)->toBeString();
133+
}
134+
});
135+
124136
it('returns countries with alpha2 codes', function () {
125137
$collection = new Collection();
126138
$countries = $collection->getCountriesAlpha2();

0 commit comments

Comments
 (0)