Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 57ae86d

Browse files
authored
Make sure we never use a country with name AND code as null (#768)
* Make sure we never use a country with name AND code as null * cs * Bugfixes * Make sure one cannot create a country without data
1 parent 272924a commit 57ae86d

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

Formatter/StringFormatter.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ final class StringFormatter
5050
*/
5151
public function format(Location $location, string $format): string
5252
{
53-
if (null !== $code = $location->getCountry()->getCode()) {
54-
$code = strtoupper($code);
53+
$countryName = null;
54+
$code = null;
55+
if (null !== $country = $location->getCountry()) {
56+
$countryName = $country->getName();
57+
if (null !== $code = $country->getCode()) {
58+
$code = strtoupper($code);
59+
}
5560
}
5661

5762
$replace = [
@@ -60,7 +65,7 @@ public function format(Location $location, string $format): string
6065
self::LOCALITY => $location->getLocality(),
6166
self::POSTAL_CODE => $location->getPostalCode(),
6267
self::SUB_LOCALITY => $location->getSubLocality(),
63-
self::COUNTRY => $location->getCountry()->getName(),
68+
self::COUNTRY => $countryName,
6469
self::COUNTRY_CODE => $code,
6570
self::TIMEZONE => $location->getTimezone(),
6671
];

Model/Address.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,7 @@ public static function createFromArray(array $data)
265265
$data['postalCode'],
266266
$data['locality'],
267267
$data['subLocality'],
268-
new Country(
269-
$data['country'],
270-
$data['countryCode']
271-
),
268+
self::createCountry($data['country'], $data['countryCode']),
272269
$data['timezone']
273270
);
274271
}
@@ -288,6 +285,21 @@ private static function createCoordinates($latitude, $longitude)
288285
return new Coordinates($latitude, $longitude);
289286
}
290287

288+
/**
289+
* @param string|null $name
290+
* @param string|null $code
291+
*
292+
* @return Country|null
293+
*/
294+
private static function createCountry($name, $code)
295+
{
296+
if (null === $name && null === $code) {
297+
return null;
298+
}
299+
300+
return new Country($name, $code);
301+
}
302+
291303
/**
292304
* @param float $south
293305
* @param float $west

Model/Country.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212

1313
namespace Geocoder\Model;
1414

15+
use Geocoder\Exception\InvalidArgument;
16+
1517
/**
18+
* A Country has either a name or a code. A Country will never be without data.
19+
*
1620
* @author William Durand <[email protected]>
1721
*/
1822
final class Country
@@ -33,6 +37,10 @@ final class Country
3337
*/
3438
public function __construct(string $name = null, string $code = null)
3539
{
40+
if (null === $name && null === $code) {
41+
throw new InvalidArgument('A country must have either a name or a code');
42+
}
43+
3644
$this->name = $name;
3745
$this->code = $code;
3846
}

0 commit comments

Comments
 (0)