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

Fixes #925: Invalid E.164 format for pt_PT PhoneNumber Provider #926

Open
wants to merge 3 commits into
base: 2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 102 additions & 26 deletions src/Provider/pt_PT/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,63 @@

class PhoneNumber extends \Faker\Provider\PhoneNumber
{
/**
* Phone country code.
*/
public const COUNTRY_CODE = '+351';

/**
* Mobile Service Codes
*/
public const MOBILE_SERVICE_CODE = [
91,
92,
93,
96,
];

/**
* Geographic Area Codes
*/
public const AREA_CODE = [
21,
22,
23,
24,
25,
26,
27,
28,
29,
];

/**
* Geographic Area and Mobile Service Codes
*/
public const AREA_AND_MOBILE_SERVICE_CODE = [
...self::AREA_CODE,
...self::MOBILE_SERVICE_CODE,
];

/**
* @see http://en.wikipedia.org/wiki/Telephone_numbers_in_Portugal
*/
protected static $formats = [
'+351 91#######',
'+351 92#######',
'+351 93#######',
'+351 96#######',
'+351 21#######',
'+351 22#######',
'+351 23#######',
'+351 24#######',
'+351 25#######',
'+351 26#######',
'+351 27#######',
'+351 28#######',
'+351 29#######',
'91#######',
'92#######',
'93#######',
'96#######',
'21#######',
'22#######',
'23#######',
'24#######',
'25#######',
'26#######',
'27#######',
'28#######',
'29#######',
'{{countryCode}} {{areaAndMobileServiceCode}}#######',
'{{mobileServiceCode}}#######',
'{{areaCode}}#######',
];

protected static $e164Formats = [
'{{countryCode}}{{areaAndMobileServiceCode}}#######',
];

protected static $e164MobileFormat = [
'{{countryCode}}{{mobileServiceCode}}#######',
];

protected static $e164LandlineFormat = [
'{{countryCode}}{{areaCode}}#######',
];

protected static $mobileNumberPrefixes = [
Expand All @@ -47,4 +74,53 @@ public static function mobileNumber()
{
return static::numerify(static::randomElement(static::$mobileNumberPrefixes));
}

public static function areaAndMobileServiceCode()
{
return static::randomElement(self::AREA_AND_MOBILE_SERVICE_CODE);
}

public static function areaCode()
{
return static::randomElement(self::AREA_CODE);
}

public static function mobileServiceCode()
{
return static::randomElement(self::MOBILE_SERVICE_CODE);
}

/**
* Returns the phone country code.
*
* @return string
*/
public static function countryCode()
{
return self::COUNTRY_CODE;
}

/**
* Returns a mobile number in E.164 format.
*
* Example: +35193XXXXXXX
*
* @return string
*/
public function e164MobileNumber()
{
return static::numerify($this->generator->parse(static::randomElement(static::$e164MobileFormat)));
}

/**
* Returns a landline number in E.164 format.
*
* Example: +35121XXXXXXX
*
* @return string
*/
public function e164LandlineNumber()
{
return static::numerify($this->generator->parse(static::randomElement(static::$e164LandlineFormat)));
}
}
19 changes: 17 additions & 2 deletions test/Provider/pt_PT/PhoneNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,29 @@ final class PhoneNumberTest extends TestCase
{
public function testPhoneNumberReturnsPhoneNumberWithOrWithoutPrefix(): void
{
self::assertMatchesRegularExpression('/^(9[1,2,3,6][0-9]{7})|(2[0-9]{8})|(\+351 [2][0-9]{8})|(\+351 9[1,2,3,6][0-9]{7})/', $this->faker->phoneNumber());
self::assertMatchesRegularExpression('/^(?:\+351 )?(?:9[1,2,3,6][0-9]{7}|2[1-9][0-9]{7})$/', $this->faker->phoneNumber());
}

public function testMobileNumberReturnsMobileNumberWithOrWithoutPrefix(): void
public function testMobileNumberReturnsMobileNumberWithoutPrefix(): void
{
self::assertMatchesRegularExpression('/^(9[1,2,3,6][0-9]{7})/', $this->faker->mobileNumber());
}

public function testE164PhoneNumberReturnsE164MobileOrLandlineNumber(): void
{
self::assertMatchesRegularExpression('/^\+351(?:9[1,2,3,6][0-9]{7}|2[1-9][0-9]{7})$/', $this->faker->e164PhoneNumber());
}

public function testE164MobileNumberReturnsE164MobileNumber(): void
{
self::assertMatchesRegularExpression('/^\+3519[1,2,3,6][0-9]{7}$/', $this->faker->e164MobileNumber());
}

public function testE164LandlineNumberReturnsE164LandlineNumber(): void
{
self::assertMatchesRegularExpression('/^\+3512[1-9][0-9]{7}$/', $this->faker->e164LandlineNumber());
}

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