diff --git a/src/Extensions/PhoneExtension.php b/src/Extensions/PhoneExtension.php index 8c67f2b..e60c230 100644 --- a/src/Extensions/PhoneExtension.php +++ b/src/Extensions/PhoneExtension.php @@ -10,7 +10,7 @@ class PhoneExtension extends Extension public string $defaultPrefix = ''; - public string $indicator = '00'; + public string $indicator = '+00'; protected array $landlineFormats = [ '{d}{d}{separator}{d}{d}{separator}{d}{d}{separator}{d}{d}{separator}{d}{d}', @@ -28,15 +28,17 @@ public function phoneNumber(?string $format = null, string $separator = '', ?str $format = $this->pickArrayRandomElement(rand(0, 1) ? $this->landlineFormats : $this->cellFormats); } - while (($pos = strpos($format, '{separator}')) !== false) { - $format = substr_replace($format, $separator, $pos, 11); - } - if (is_null($prefix)) { $prefix = $this->defaultPrefix; } - return $prefix.$this->formatString($format); + $format = $prefix.$format; + + while (($pos = strpos($format, '{separator}')) !== false) { + $format = substr_replace($format, $separator, $pos, 11); + } + + return $this->formatString($format); } public function indicatoredPhoneNumber(?string $format = null, string $separator = ''): string @@ -44,6 +46,36 @@ public function indicatoredPhoneNumber(?string $format = null, string $separator return $this->phoneNumber(format: $format, separator: $separator, prefix: $this->indicator); } + public function spacedPhoneNumber(?string $format = null, ?string $prefix = null): string + { + return $this->phoneNumber(format: $format, separator: ' ', prefix: $prefix); + } + + public function spacedIndicatoredPhoneNumber(?string $format = null): string + { + return $this->indicatoredPhoneNumber(format: $format, separator: ' '); + } + + public function dottedPhoneNumber(?string $format = null, ?string $prefix = null): string + { + return $this->phoneNumber(format: $format, separator: '.', prefix: $prefix); + } + + public function dottedIndicatoredPhoneNumber(?string $format = null): string + { + return $this->indicatoredPhoneNumber(format: $format, separator: '.'); + } + + public function dashedPhoneNumber(?string $format = null, ?string $prefix = null): string + { + return $this->phoneNumber(format: $format, separator: '-', prefix: $prefix); + } + + public function dashedIndicatoredPhoneNumber(?string $format = null): string + { + return $this->indicatoredPhoneNumber(format: $format, separator: '-'); + } + // Cell phone numbers public function cellPhoneNumber(string $separator = '', ?string $prefix = null): string { @@ -55,6 +87,36 @@ public function indicatoredCellPhoneNumber(string $separator = ''): string return $this->cellPhoneNumber(separator: $separator, prefix: $this->indicator); } + public function spacedCellPhoneNumber(?string $prefix = null): string + { + return $this->cellPhoneNumber(separator: ' ', prefix: $prefix); + } + + public function spacedIndicatoredCellPhoneNumber(): string + { + return $this->spacedCellPhoneNumber(prefix: $this->indicator); + } + + public function dottedCellPhoneNumber(?string $prefix = null): string + { + return $this->cellPhoneNumber(separator: '.', prefix: $prefix); + } + + public function dottedIndicatoredCellPhoneNumber(): string + { + return $this->indicatoredCellPhoneNumber(separator: '.'); + } + + public function dashedCellPhoneNumber(?string $prefix = null): string + { + return $this->cellPhoneNumber(separator: '-', prefix: $prefix); + } + + public function dashedIndicatoredCellPhoneNumber(): string + { + return $this->indicatoredCellPhoneNumber(separator: '-'); + } + // Landline phone numbers public function landlinePhoneNumber(string $separator = '', ?string $prefix = null): string { @@ -65,4 +127,34 @@ public function indicatoredLandlinePhoneNumber(string $separator = ''): string { return $this->landlinePhoneNumber(separator: $separator, prefix: $this->indicator); } + + public function spacedLandlinePhoneNumber(?string $prefix = null): string + { + return $this->landlinePhoneNumber(separator: ' ', prefix: $prefix); + } + + public function spacedIndicatoredLandlinePhoneNumber(): string + { + return $this->spacedLandlinePhoneNumber(prefix: $this->indicator); + } + + public function dottedLandlinePhoneNumber(?string $prefix = null): string + { + return $this->landlinePhoneNumber(separator: '.', prefix: $prefix); + } + + public function dottedIndicatoredLandlinePhoneNumber(): string + { + return $this->indicatoredLandlinePhoneNumber(separator: '.'); + } + + public function dashedLandlinePhoneNumber(?string $prefix = null): string + { + return $this->landlinePhoneNumber(separator: '-', prefix: $prefix); + } + + public function dashedIndicatoredLandlinePhoneNumber(): string + { + return $this->indicatoredLandlinePhoneNumber(separator: '-'); + } } diff --git a/tests/Unit/Extensions/PhoneExtensionTest.php b/tests/Unit/Extensions/PhoneExtensionTest.php index ba18376..5c66a14 100644 --- a/tests/Unit/Extensions/PhoneExtensionTest.php +++ b/tests/Unit/Extensions/PhoneExtensionTest.php @@ -165,7 +165,7 @@ public function testIndicatoredPhoneNumber(): void $number = $this->faker->indicatoredPhoneNumber(); $this->assertMatchesRegularExpression( - '/^00(\d{4}|\d{8}|\d{10})$/', + '/^\+00(\d{4}|\d{8}|\d{10})$/', $number ); } @@ -177,7 +177,7 @@ public function testIndicatoredPhoneNumberWithFormat(): void $number = $this->faker->indicatoredPhoneNumber(format: '{d}{d}{d}{d}{d}{d}'); $this->assertMatchesRegularExpression( - '/^00(\d{6})$/', + '/^\+00(\d{6})$/', $number ); } @@ -189,7 +189,7 @@ public function testIndicatoredPhoneNumberWithSeparator(): void $number = $this->faker->indicatoredPhoneNumber(format: '{d}{d}{separator}{d}{d}{separator}{d}{d}', separator: '-'); $this->assertMatchesRegularExpression( - '/^00(\d{2}-\d{2}-\d{2})$/', + '/^\+00(\d{2}-\d{2}-\d{2})$/', $number ); } @@ -201,7 +201,7 @@ public function testIndicatoredCellPhoneNumber(): void $number = $this->faker->indicatoredCellPhoneNumber(); $this->assertMatchesRegularExpression( - '/^00\d{4}$/', + '/^\+00\d{4}$/', $number ); } @@ -213,7 +213,7 @@ public function testIndicatoredCellPhoneNumberWithSeparator(): void $number = $this->faker->indicatoredCellPhoneNumber('.'); $this->assertMatchesRegularExpression( - '/^00\d{2}\.\d{2}$/', + '/^\+00\d{2}\.\d{2}$/', $number ); } @@ -225,7 +225,7 @@ public function testIndicatoredLandlinePhoneNumber(): void $number = $this->faker->indicatoredLandlinePhoneNumber(); $this->assertMatchesRegularExpression( - '/^00(\d{8}|\d{10})$/', + '/^\+00(\d{8}|\d{10})$/', $number ); } @@ -237,7 +237,7 @@ public function testIndicatoredLandlinePhoneNumberWithSeparator(): void $number = $this->faker->indicatoredLandlinePhoneNumber('.'); $this->assertMatchesRegularExpression( - '/^00\d{2}\.\d{2}\.\d{2}\.\d{2}(\.\d{2})?$/', + '/^\+00\d{2}\.\d{2}\.\d{2}\.\d{2}(\.\d{2})?$/', $number ); }