From 2570540b2156c1d26b8e427a09bb546fae53100f Mon Sep 17 00:00:00 2001 From: Head0nF1re <77078775+Head0nF1re@users.noreply.github.com> Date: Fri, 13 Dec 2024 05:52:56 +0000 Subject: [PATCH 1/3] Fixes FakerPHP/Faker#925 - Override $e164Formats in order to provide locale specific phone numbers (mobile and landline) in E.164 format. - Add constants / static properties for common formatting codes (country, area and mobile service). - Add methods 'e164MobileNumber' and 'e164LandlineNumber' to provide mobile-only and landline-only phone numbers, respectively. --- src/Provider/pt_PT/PhoneNumber.php | 139 +++++++++++++++++++++++------ 1 file changed, 113 insertions(+), 26 deletions(-) diff --git a/src/Provider/pt_PT/PhoneNumber.php b/src/Provider/pt_PT/PhoneNumber.php index 948ba94d92..7e1af00d01 100644 --- a/src/Provider/pt_PT/PhoneNumber.php +++ b/src/Provider/pt_PT/PhoneNumber.php @@ -4,36 +4,74 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber { + /** + * Returns the pt_PT phone country code. + */ + const COUNTRY_CODE = '+351'; + + /** + * pt_PT Mobile Service Codes + */ + protected static $mobileServiceCode = [ + 91, + 92, + 93, + 96, + ]; + + /** + * pt_PT Geographic Area Codes + */ + protected static $areaCode = [ + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28, + 29, + ]; + + /** + * pt_PT Geographic Area and Mobile Service Codes + */ + protected static $areaOrMobileServiceCode = [ + 91, + 92, + 93, + 96, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28, + 29, + ]; + /** * @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}} {{areaOrMobileServiceCode}}#######', + '{{mobileServiceCode}}#######', + '{{areaCode}}#######', + ]; + + protected static $e164Formats = [ + '{{countryCode}}{{areaOrMobileServiceCode}}#######', + ]; + + protected static $e164MobileFormat = [ + '{{countryCode}}{{mobileServiceCode}}#######', + ]; + + protected static $e164LandlineFormat = [ + '{{countryCode}}{{areaCode}}#######', ]; protected static $mobileNumberPrefixes = [ @@ -47,4 +85,53 @@ public static function mobileNumber() { return static::numerify(static::randomElement(static::$mobileNumberPrefixes)); } + + public static function areaOrMobileServiceCode() + { + return self::randomElement(static::$areaOrMobileServiceCode); + } + + public static function areaCode() + { + return self::randomElement(static::$areaCode); + } + + public static function mobileServiceCode() + { + return self::randomElement(static::$mobileServiceCode); + } + + /** + * Returns the pt_PT phone country code. + * + * @return string + */ + public static function countryCode() + { + return self::COUNTRY_CODE; + } + + /** + * Returns a pt_PT 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 pt_PT landline number in E.164 format. + * + * Example: +35121XXXXXXX + * + * @return string + */ + public function e164LandlineNumber() + { + return static::numerify($this->generator->parse(static::randomElement(static::$e164LandlineFormat))); + } } From 3591ca3b1667f3b1afc6d5d61f20dc4aba901325 Mon Sep 17 00:00:00 2001 From: Head0nF1re <77078775+Head0nF1re@users.noreply.github.com> Date: Fri, 13 Dec 2024 19:04:50 +0000 Subject: [PATCH 2/3] Merge AREA_OR_MOBILE_SERVICE_CODE - Add consts in order to be able to merge AREA_CODE and MOBILE_SERVICE_CODE. --- src/Provider/pt_PT/PhoneNumber.php | 31 ++++++++++-------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/Provider/pt_PT/PhoneNumber.php b/src/Provider/pt_PT/PhoneNumber.php index 7e1af00d01..b9962388f5 100644 --- a/src/Provider/pt_PT/PhoneNumber.php +++ b/src/Provider/pt_PT/PhoneNumber.php @@ -7,12 +7,12 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber /** * Returns the pt_PT phone country code. */ - const COUNTRY_CODE = '+351'; + public const COUNTRY_CODE = '+351'; /** * pt_PT Mobile Service Codes */ - protected static $mobileServiceCode = [ + public const MOBILE_SERVICE_CODE = [ 91, 92, 93, @@ -22,7 +22,7 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber /** * pt_PT Geographic Area Codes */ - protected static $areaCode = [ + public const AREA_CODE = [ 21, 22, 23, @@ -37,20 +37,9 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber /** * pt_PT Geographic Area and Mobile Service Codes */ - protected static $areaOrMobileServiceCode = [ - 91, - 92, - 93, - 96, - 21, - 22, - 23, - 24, - 25, - 26, - 27, - 28, - 29, + public const AREA_OR_MOBILE_SERVICE_CODE = [ + ...self::MOBILE_SERVICE_CODE, + ...self::AREA_CODE, ]; /** @@ -88,17 +77,17 @@ public static function mobileNumber() public static function areaOrMobileServiceCode() { - return self::randomElement(static::$areaOrMobileServiceCode); + return self::randomElement(static::AREA_OR_MOBILE_SERVICE_CODE); } public static function areaCode() { - return self::randomElement(static::$areaCode); + return self::randomElement(static::AREA_CODE); } public static function mobileServiceCode() { - return self::randomElement(static::$mobileServiceCode); + return self::randomElement(static::MOBILE_SERVICE_CODE); } /** @@ -134,4 +123,4 @@ public function e164LandlineNumber() { return static::numerify($this->generator->parse(static::randomElement(static::$e164LandlineFormat))); } -} +} \ No newline at end of file From 59cbcb552c6bed1fd3b25298c37ca57518bb0139 Mon Sep 17 00:00:00 2001 From: Head0nF1re <77078775+Head0nF1re@users.noreply.github.com> Date: Sat, 8 Feb 2025 18:28:36 +0000 Subject: [PATCH 3/3] Add Regex test cases for pt_PT PhoneNumber #925 - Add tests for e164PhoneNumber, e164MobileNumber and e164LandlineNumber. - Simplify regex for phoneNumber. Note: - Regex for phoneNumber was duplicating some information. It also validated for landline numbers starting at 20 even tho it wasn't present in the provider $formats. - Regex for country code, mobile number and landline number could be placed in a const variable. The problem is: there's some issues with interpolating self/static variables and concatenation would probably be hard to read. --- src/Provider/pt_PT/PhoneNumber.php | 36 ++++++++++++------------- test/Provider/pt_PT/PhoneNumberTest.php | 19 +++++++++++-- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/Provider/pt_PT/PhoneNumber.php b/src/Provider/pt_PT/PhoneNumber.php index b9962388f5..08b741fbc3 100644 --- a/src/Provider/pt_PT/PhoneNumber.php +++ b/src/Provider/pt_PT/PhoneNumber.php @@ -5,12 +5,12 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber { /** - * Returns the pt_PT phone country code. + * Phone country code. */ public const COUNTRY_CODE = '+351'; /** - * pt_PT Mobile Service Codes + * Mobile Service Codes */ public const MOBILE_SERVICE_CODE = [ 91, @@ -20,7 +20,7 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber ]; /** - * pt_PT Geographic Area Codes + * Geographic Area Codes */ public const AREA_CODE = [ 21, @@ -35,24 +35,24 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber ]; /** - * pt_PT Geographic Area and Mobile Service Codes + * Geographic Area and Mobile Service Codes */ - public const AREA_OR_MOBILE_SERVICE_CODE = [ - ...self::MOBILE_SERVICE_CODE, + 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 = [ - '{{countryCode}} {{areaOrMobileServiceCode}}#######', + '{{countryCode}} {{areaAndMobileServiceCode}}#######', '{{mobileServiceCode}}#######', '{{areaCode}}#######', ]; protected static $e164Formats = [ - '{{countryCode}}{{areaOrMobileServiceCode}}#######', + '{{countryCode}}{{areaAndMobileServiceCode}}#######', ]; protected static $e164MobileFormat = [ @@ -75,23 +75,23 @@ public static function mobileNumber() return static::numerify(static::randomElement(static::$mobileNumberPrefixes)); } - public static function areaOrMobileServiceCode() + public static function areaAndMobileServiceCode() { - return self::randomElement(static::AREA_OR_MOBILE_SERVICE_CODE); + return static::randomElement(self::AREA_AND_MOBILE_SERVICE_CODE); } public static function areaCode() { - return self::randomElement(static::AREA_CODE); + return static::randomElement(self::AREA_CODE); } public static function mobileServiceCode() { - return self::randomElement(static::MOBILE_SERVICE_CODE); + return static::randomElement(self::MOBILE_SERVICE_CODE); } /** - * Returns the pt_PT phone country code. + * Returns the phone country code. * * @return string */ @@ -101,8 +101,8 @@ public static function countryCode() } /** - * Returns a pt_PT mobile number in E.164 format. - * + * Returns a mobile number in E.164 format. + * * Example: +35193XXXXXXX * * @return string @@ -113,8 +113,8 @@ public function e164MobileNumber() } /** - * Returns a pt_PT landline number in E.164 format. - * + * Returns a landline number in E.164 format. + * * Example: +35121XXXXXXX * * @return string @@ -123,4 +123,4 @@ public function e164LandlineNumber() { return static::numerify($this->generator->parse(static::randomElement(static::$e164LandlineFormat))); } -} \ No newline at end of file +} diff --git a/test/Provider/pt_PT/PhoneNumberTest.php b/test/Provider/pt_PT/PhoneNumberTest.php index 0014da4ce0..35b0d16974 100644 --- a/test/Provider/pt_PT/PhoneNumberTest.php +++ b/test/Provider/pt_PT/PhoneNumberTest.php @@ -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);