Skip to content
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
104 changes: 98 additions & 6 deletions src/Extensions/PhoneExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}',
Expand All @@ -28,22 +28,54 @@ 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
{
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
{
Expand All @@ -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
{
Expand All @@ -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: '-');
}
}
14 changes: 7 additions & 7 deletions tests/Unit/Extensions/PhoneExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand All @@ -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
);
}
Expand All @@ -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
);
}
Expand All @@ -201,7 +201,7 @@ public function testIndicatoredCellPhoneNumber(): void
$number = $this->faker->indicatoredCellPhoneNumber();

$this->assertMatchesRegularExpression(
'/^00\d{4}$/',
'/^\+00\d{4}$/',
$number
);
}
Expand All @@ -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
);
}
Expand All @@ -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
);
}
Expand All @@ -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
);
}
Expand Down