PhoneFormatUS
Which layout a US phone number arrives in, and which shape it comes back in.
The same phone number can be written many ways: '+15552345678', '555.234.5678',
'(555) 234-5678'. PhoneFormatUS names those layouts, and it is used on both sides
of a call: as the format parameter it says which layout the input must match, and
as the output parameter it says which shape the cleaned number is returned in.
A specific member on the input side means "exactly this layout, nothing else".
Two terms you will meet below: NANP is the North American Numbering Plan, the
ten-digit numbering system the US and Canada share; E.164 is the international
standard shape — a +, the country code, then the digits.
Members
| Member | Meaning |
|---|---|
Any |
On input: any of the four exact layouts below, and nothing else. On output: a number that arrived in an exact layout is returned unchanged, and a loosely formatted one comes back as E164. The default output shape. |
E164 |
Country code and digits: '+15552345678'. |
Digits |
Bare national digits: '5552345678'. |
National |
The US display form: '(555) 234-5678'. |
Dashed |
The dashed form: '555-234-5678'. |
Loose |
Input only: any mix of digits, spaces, dots, dashes, parentheses and a leading '+1' or '1', with a trailing extension such as 'x12' dropped. The default input layout, since real data rarely arrives in one exact shape. |
Where it is used
Phone on CkUS takes it twice: format is the layout the
input must match (default Loose), and output is the shape to return (default
Any). PhoneMask on the same class takes an output parameter too — there it
defaults to National, so a masked number always looks like a display phone number.
Quick look
use SafeCoder\CommonKit\CkUS;
use SafeCoder\CommonKit\Enums\PhoneFormatUS;
CkUS::Phone('1 (555) 234-5678'); // -> '+15552345678' (Loose in, E.164 out)
CkUS::Phone(
input: '555.234.5678',
output: PhoneFormatUS::National
); // -> '(555) 234-5678'
CkUS::Phone(
input: '555-234-5678',
format: PhoneFormatUS::E164
); // -> '' (the input had to be E.164, and is not)