diff --git a/src/Provider/pt_PT/PhoneNumber.php b/src/Provider/pt_PT/PhoneNumber.php index 948ba94d92..08b741fbc3 100644 --- a/src/Provider/pt_PT/PhoneNumber.php +++ b/src/Provider/pt_PT/PhoneNumber.php @@ -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 = [ @@ -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))); + } } 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);