Skip to content

Commit a57af12

Browse files
committed
Add PhoneNumber::getDescription()
Encapsulates PhoneNumberOfflineGeocoder.
1 parent f9cdef7 commit a57af12

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/PhoneNumber.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Brick\PhoneNumber;
66

77
use JsonSerializable;
8+
use libphonenumber\geocoding\PhoneNumberOfflineGeocoder;
89
use libphonenumber\NumberParseException;
910
use libphonenumber\PhoneNumberUtil;
1011

@@ -212,6 +213,43 @@ public function jsonSerialize(): string
212213
return (string) $this;
213214
}
214215

216+
/**
217+
* Returns a text description for the given phone number, in the language provided. The description might consist of
218+
* the name of the country where the phone number is from, or the name of the geographical area the phone number is
219+
* from if more detailed information is available.
220+
*
221+
* If $userRegion is set, we also consider the region of the user. If the phone number is from the same region as
222+
* the user, only a lower-level description will be returned, if one exists. Otherwise, the phone number's region
223+
* will be returned, with optionally some more detailed information.
224+
*
225+
* For example, for a user from the region "US" (United States), we would show "Mountain View, CA" for a particular
226+
* number, omitting the United States from the description. For a user from the United Kingdom (region "GB"), for
227+
* the same number we may show "Mountain View, CA, United States" or even just "United States".
228+
*
229+
* If no description is found, this method returns null.
230+
*
231+
* @param string $locale The locale for which the description should be written.
232+
* @param string|null $userRegion The region code for a given user. This region will be omitted from the description
233+
* if the phone number comes from this region. It is a two-letter uppercase CLDR
234+
* region code.
235+
*
236+
* @return string|null
237+
*/
238+
public function getDescription(string $locale, ?string $userRegion = null) : ?string
239+
{
240+
$description = PhoneNumberOfflineGeocoder::getInstance()->getDescriptionForNumber(
241+
$this->phoneNumber,
242+
$locale,
243+
$userRegion
244+
);
245+
246+
if ($description === '') {
247+
return null;
248+
}
249+
250+
return $description;
251+
}
252+
215253
/**
216254
* Returns a string representation of this phone number in international E164 format.
217255
*

tests/PhoneNumberTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,4 +592,51 @@ public function testJsonSerializable(): void
592592

593593
self::assertSame('{"phoneNumber":"+33123000000"}', json_encode($data));
594594
}
595+
596+
/**
597+
* @dataProvider providerGetDescription
598+
*/
599+
public function testGetDescription(string $phoneNumber, string $locale, ?string $userRegion, ?string $expected) : void
600+
{
601+
self::assertSame($expected, PhoneNumber::parse($phoneNumber)->getDescription($locale, $userRegion));
602+
}
603+
604+
public function providerGetDescription() : array
605+
{
606+
return [
607+
['+16509036313', 'EN', null, 'Mountain View, CA'],
608+
['+16509036313', 'EN', 'US', 'Mountain View, CA'],
609+
['+16509036313', 'EN', 'GB', 'United States'],
610+
['+16509036313', 'EN', 'FR', 'United States'],
611+
['+16509036313', 'EN', 'XX', 'United States'],
612+
['+16509036313', 'FR', null, 'Mountain View, CA'],
613+
['+16509036313', 'FR', 'US', 'Mountain View, CA'],
614+
['+16509036313', 'FR', 'GB', 'États-Unis'],
615+
['+16509036313', 'FR', 'FR', 'États-Unis'],
616+
['+16509036313', 'FR', 'XX', 'États-Unis'],
617+
618+
['+33381251234', 'FR', null, 'Besançon'],
619+
['+33381251234', 'FR', 'FR', 'Besançon'],
620+
['+33381251234', 'FR', 'US', 'France'],
621+
['+33381251234', 'FR', 'XX', 'France'],
622+
['+33381251234', 'EN', null, 'Besançon'],
623+
['+33381251234', 'EN', 'FR', 'Besançon'],
624+
['+33381251234', 'EN', 'US', 'France'],
625+
['+33381251234', 'EN', 'XX', 'France'],
626+
627+
['+33328201234', 'FR', null, 'Dunkerque'],
628+
['+33328201234', 'FR', 'FR', 'Dunkerque'],
629+
['+33328201234', 'FR', 'US', 'France'],
630+
['+33328201234', 'FR', 'XX', 'France'],
631+
['+33328201234', 'GB', null, 'Dunkirk'],
632+
['+33328201234', 'XX', null, 'Dunkirk'],
633+
634+
['+41229097000', 'FR', null, 'Genève'],
635+
['+41229097000', 'FR', 'CH', 'Genève'],
636+
['+41229097000', 'FR', 'US', 'Suisse'],
637+
['+41229097000', 'XX', null, 'Geneva'],
638+
639+
['+37328000000', 'XX', null, null],
640+
];
641+
}
595642
}

0 commit comments

Comments
 (0)