add projet
This commit is contained in:
Executable
+31
@@ -0,0 +1,31 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user