32 lines
739 B
PHP
Executable File
32 lines
739 B
PHP
Executable File
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
class PhoneFormatterService
|
|
{
|
|
/**
|
|
* Normalize a phone number by stripping non-digits and formatting US numbers.
|
|
*/
|
|
public function formatPhoneNumber(string $phone): string
|
|
{
|
|
$digits = preg_replace('/\D+/', '', $phone);
|
|
if ($digits === null || $digits === '') {
|
|
return '';
|
|
}
|
|
|
|
if (strlen($digits) === 10) {
|
|
return sprintf('(%s) %s-%s', substr($digits, 0, 3), substr($digits, 3, 3), substr($digits, 6));
|
|
}
|
|
|
|
return $digits;
|
|
}
|
|
|
|
/**
|
|
* Determine if a phone string contains any digits.
|
|
*/
|
|
public function hasDigits(string $phone): bool
|
|
{
|
|
return preg_match('/\d/', $phone) === 1;
|
|
}
|
|
}
|