Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix e164PhoneNumber to only generate valid phone numbers with valid country codes #264

Merged
merged 11 commits into from
Jan 27, 2021
227 changes: 223 additions & 4 deletions src/Faker/Provider/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,227 @@ class PhoneNumber extends Base
{
protected static $formats = ['###-###-###'];

/**
* @see https://github.com/giggsey/libphonenumber-for-php/blob/master/src/CountryCodeToRegionCodeMap.php
*/
protected static $e164Formats = [
Copy link

@krsriq krsriq Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why the calling codes for de_AT and de_CH are missing while de_DE and fr_FR are included?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default array is only copied from the linked source above.
There are even more countries missing but I think that this list is big enough for testing and every country provider can define the specific ones.

'+1##########',
'+7##########',
'+20##########',
'+27##########',
'+30##########',
'+31##########',
'+32##########',
'+33##########',
'+34##########',
'+36##########',
'+39##########',
'+40##########',
'+41##########',
'+43##########',
'+44##########',
'+45##########',
'+46##########',
'+47##########',
'+48##########',
'+49##########',
'+51##########',
'+52##########',
'+53##########',
'+54##########',
'+55##########',
'+56##########',
'+57##########',
'+58##########',
'+60##########',
'+61##########',
'+62##########',
'+63##########',
'+64##########',
'+65##########',
'+66##########',
'+81##########',
'+82##########',
'+84##########',
'+86##########',
'+90##########',
'+91##########',
'+92##########',
'+93##########',
'+94##########',
'+95##########',
'+98##########',
'+211##########',
'+212##########',
'+213##########',
'+216##########',
'+218##########',
'+220##########',
'+221##########',
'+222##########',
'+223##########',
'+224##########',
'+225##########',
'+226##########',
'+227##########',
'+228##########',
'+229##########',
'+230##########',
'+231##########',
'+232##########',
'+233##########',
'+234##########',
'+235##########',
'+236##########',
'+237##########',
'+238##########',
'+239##########',
'+240##########',
'+241##########',
'+242##########',
'+243##########',
'+244##########',
'+245##########',
'+246##########',
'+247##########',
'+248##########',
'+249##########',
'+250##########',
'+251##########',
'+252##########',
'+253##########',
'+254##########',
'+255##########',
'+256##########',
'+257##########',
'+258##########',
'+260##########',
'+261##########',
'+262##########',
'+263##########',
'+264##########',
'+265##########',
'+266##########',
'+267##########',
'+268##########',
'+269##########',
'+290##########',
'+291##########',
'+297##########',
'+298##########',
'+299##########',
'+350##########',
'+351##########',
'+352##########',
'+353##########',
'+354##########',
'+355##########',
'+356##########',
'+357##########',
'+358##########',
'+359##########',
'+370##########',
'+371##########',
'+372##########',
'+373##########',
'+374##########',
'+375##########',
'+376##########',
'+377##########',
'+378##########',
'+380##########',
'+381##########',
'+382##########',
'+383##########',
'+385##########',
'+386##########',
'+387##########',
'+389##########',
'+420##########',
'+421##########',
'+423##########',
'+500##########',
'+501##########',
'+502##########',
'+503##########',
'+504##########',
'+505##########',
'+506##########',
'+507##########',
'+508##########',
'+509##########',
'+590##########',
'+591##########',
'+592##########',
'+593##########',
'+594##########',
'+595##########',
'+596##########',
'+597##########',
'+598##########',
'+599##########',
'+670##########',
'+672##########',
'+673##########',
'+674##########',
'+675##########',
'+676##########',
'+677##########',
'+678##########',
'+679##########',
'+680##########',
'+681##########',
'+682##########',
'+683##########',
'+685##########',
'+686##########',
'+687##########',
'+688##########',
'+689##########',
'+690##########',
'+691##########',
'+692##########',
'+800##########',
'+808##########',
'+850##########',
'+852##########',
'+853##########',
'+855##########',
'+856##########',
'+870##########',
'+878##########',
'+880##########',
'+881##########',
'+882##########',
'+883##########',
'+886##########',
'+888##########',
'+960##########',
'+961##########',
'+962##########',
'+963##########',
'+964##########',
'+965##########',
'+966##########',
'+967##########',
'+968##########',
'+970##########',
'+971##########',
'+972##########',
'+973##########',
'+974##########',
'+975##########',
'+976##########',
'+977##########',
'+979##########',
'+992##########',
'+993##########',
'+994##########',
'+995##########',
'+996##########',
'+998##########',
];

/**
* @example '555-123-546'
*/
Expand All @@ -17,15 +238,13 @@ public function phoneNumber()
}

/**
* @example +27113456789
* @example +11134567890
*
* @return string
*/
public function e164PhoneNumber()
{
$formats = ['+%############'];

return static::numerify($this->generator->parse(static::randomElement($formats)));
return static::numerify($this->generator->parse(static::randomElement(static::$e164Formats)));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Faker/Provider/de_AT/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber
'+43 #### ####',
'+43 #### ####-##',
];

protected static $e164Formats = [
'+43##########',
];
}
4 changes: 4 additions & 0 deletions src/Faker/Provider/de_CH/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber
'079#######',
];

protected static $e164Formats = [
'+41##########',
];

/**
* Return a Swiss mobile phone number.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Faker/Provider/de_DE/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber
'(0####) ######',
'(0####) #####',
];

protected static $e164Formats = [
'+49##########',
];
}
4 changes: 4 additions & 0 deletions src/Faker/Provider/en_GB/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber
'07### ### ###',
];

protected static $e164Formats = [
'+44##########',
];

/**
* Return a en_GB mobile phone number
*
Expand Down
4 changes: 4 additions & 0 deletions src/Faker/Provider/en_US/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber
'{{areaCode}}.{{exchangeCode}}.#### x#####',
];

protected static $e164Formats = [
'+1##########',
];

/**
* @see https://en.wikipedia.org/wiki/Toll-free_telephone_number#United_States
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Faker/Provider/fr_FR/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber
'08{{phoneNumber08}}',
];

protected static $e164Formats = [
'+33##########',
];

public function phoneNumber07()
{
$phoneNumber = $this->phoneNumber07WithSeparator();
Expand Down
9 changes: 6 additions & 3 deletions test/Faker/Provider/PhoneNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
*/
final class PhoneNumberTest extends TestCase
{
public function testPhoneNumberFormat()
public function testE164PhoneNumberFormat()
{
$number = $this->faker->e164PhoneNumber();
self::assertMatchesRegularExpression('/^\+[0-9]{11,}$/', $number);
for ($i = 0; $i < 1000; ++$i) {
$number = $this->faker->e164PhoneNumber();
self::assertMatchesRegularExpression('/^\+[1-9]\d{1,14}$/', $number);
self::assertLessThanOrEqual(16, strlen($number)); // plus-sign and max. 15 digits incl. CC-prefix
}
}

public function testImeiReturnsValidNumber()
Expand Down
8 changes: 8 additions & 0 deletions test/Faker/Provider/de_AT/PhoneNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public function testPhoneNumberFormat()
self::assertMatchesRegularExpression('/^06\d{2} \d{7}|\+43 \d{4} \d{4}(-\d{2})?$/', $number);
}

public function testE164PhoneNumberFormat()
{
for ($i = 0; $i < 10; ++$i) {
$number = $this->faker->e164PhoneNumber();
self::assertMatchesRegularExpression('/^\+43\d{1,13}$/', $number);
}
}

protected function getProviders(): iterable
{
yield new PhoneNumber($this->faker);
Expand Down
8 changes: 8 additions & 0 deletions test/Faker/Provider/de_CH/PhoneNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public function testMobileNumber()
self::assertMatchesRegularExpression('/^07[56789] ?\d{3} ?\d{2} ?\d{2}$/', $this->faker->mobileNumber());
}

public function testE164PhoneNumberFormat()
{
for ($i = 0; $i < 10; ++$i) {
$number = $this->faker->e164PhoneNumber();
self::assertMatchesRegularExpression('/^\+41\d{1,13}$/', $number);
}
}

protected function getProviders(): iterable
{
yield new PhoneNumber($this->faker);
Expand Down
25 changes: 25 additions & 0 deletions test/Faker/Provider/de_DE/PhoneNumberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Faker\Test\Provider\de_DE;

use Faker\Provider\de_DE\PhoneNumber;
use Faker\Test\TestCase;

/**
* @group legacy
*/
final class PhoneNumberTest extends TestCase
{
public function testE164PhoneNumberFormat()
{
for ($i = 0; $i < 10; ++$i) {
$number = $this->faker->e164PhoneNumber();
self::assertMatchesRegularExpression('/^\+49\d{1,13}$/', $number);
}
}

protected function getProviders(): iterable
{
yield new PhoneNumber($this->faker);
}
}
Loading