Skip to content

Commit

Permalink
Merge pull request #10 from erichowey/v2
Browse files Browse the repository at this point in the history
V2
  • Loading branch information
erichowey authored Oct 12, 2021
2 parents 5dae2d6 + 12a8be4 commit cc2acab
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 266 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ etc...
- Dot Format `212.555.0123`
- Hyphen Format `212-555-0123`
- National Format `(212) 555-0123`
- National Format Plus One `1 (212) 555-0123`
- International Format `+1 212 555 0123`
- 10 Digit `2125550123`
- 11 Digit `12325550123`
Expand All @@ -40,6 +41,7 @@ $number = NanpNumberFormatter::format("(212) 555-0123");

echo $number->e164; // +12125550123
echo $number->nationalFormat; // (212) 555-0123
echo $number->nationalFormatPlusOne; // 1 (212) 555-0123
echo $number->internationalFormat; // +1 212 555 0123
echo $number->dotFormat; // 212.555.0123
echo $number->hyphenFormat; // 212-555-0123
Expand All @@ -62,9 +64,15 @@ $number = NanpNumberFormatter::format("(212) 555-****", true);
```

## Errors
The `NanpNumberFormatter` object has the following parameters: `(bool) isValid` and `(string) errorMessage`. This tool
does not throw exceptions, however, the `isValid` parameter will be `false` if it determines that the given number
is invalid.
If an invalid or non nanp number is attempted to be formatted, a `NanpNumberFormatterException` will be thrown. They can
be handled with the typical try/catch pattern:
```php
try {
$number = NanpNumberFormatter::format("1234");
} catch (NanpNumberFormatterException $e) {
echo $e->getMessage(); // 1234 is less than 10 characters
}
```

## Contributing
Contributions are welcome. Criticism is even more welcome. You're welcome to submit a PR or open an issue. Please
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
}
],
"require": {
"php" : "^7.2 || ^8.0"
"php" : "^7.4 || ^8.0"
},
"require-dev": {
"phpunit/phpunit" : "^8.0 || ^9.0"
"phpunit/phpunit" : "^9.0"
},
"autoload": {
"psr-4": {
"Erichowey\\NanpNumberFormatter\\": "src"
"Erichowey\\NanpNumberFormatter\\": ["src", "exceptions"]
}
},
"autoload-dev": {
Expand Down
10 changes: 10 additions & 0 deletions exceptions/NanpNumberFormatterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Erichowey\NanpNumberFormatter;

use Exception;

class NanpNumberFormatterException extends Exception
{

}
113 changes: 38 additions & 75 deletions src/NanpNumberFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,21 @@

namespace Erichowey\NanpNumberFormatter;

use function PHPUnit\Framework\isNull;

class NanpNumberFormatter
{
public $errorMessage;
public $isValid;
public $e164;
public $npa;
public $nxx;
public $line;
public $dotFormat;
public $hyphenFormat;
public $nationalFormat;
public $nationalFormatPlusOne;
public $internationalFormat;
public $tendigit;
public $elevendigit;
public $uri;


function __construct()
{
$this->errorMessage = "";
$this->isValid = false;
$this->e164 = "Invalid";
$this->npa = "Invalid";
$this->nxx = "Invalid";
$this->line = "Invalid";
$this->dotFormat = "Invalid";
$this->hyphenFormat = "Invalid";
$this->nationalFormat = "Invalid";
$this->internationalFormat = "Invalid";
$this->tendigit = "Invalid";
$this->elevendigit = "Invalid";
$this->uri = "Invalid";
}

/**
* Remaps all letters to the corresponding telephone keypad numbers
*
Expand All @@ -52,28 +31,22 @@ private function lettersToNumbers(string $number)
return strtr($number, $from, $to);
}

/**
* This takes a number of different nanp number inputs and returns
* a variety of formatted nanp numbers.
*
* @param mixed $number The phone number to be formatted
* @param bool $wildcards
* @return $this
*/
/**
* @param $number
* @param bool $wildcards
* @return $this
* @throws NanpNumberFormatterException
*/
public function parse($number, bool $wildcards = false)
{
// If $number is empty then return invalid
if (empty($number)) {
$this->isValid = false;
$this->errorMessage = "The number parameter is required";
return $this;
throw new NanpNumberFormatterException('The number parameter is required');
}

// If the string is less then 10 characters
if (strlen($number) < 10) {
$this->isValid = false;
$this->errorMessage = $number . " is less than 10 characters";
return $this;
throw new NanpNumberFormatterException($number.' is less than 10 characters');
}

// Convert all letters to numbers
Expand All @@ -87,29 +60,25 @@ public function parse($number, bool $wildcards = false)
$characterPattern = '/^[- 0-9().+]*$/';
}

if (!preg_match($characterPattern, $number)) {
$this->isValid = false;
$this->errorMessage = $number . " contains invalid characters";
return $this;
if (! preg_match($characterPattern, $number)) {
throw new NanpNumberFormatterException($number.' contains invalid characters');
}

// Remove (, ), -, ., +, and spaces
$number = str_replace(" ", "", $number);
$number = str_replace("(", "", $number);
$number = str_replace(")", "", $number);
$number = str_replace("-", "", $number);
$number = str_replace(".", "", $number);
$number = str_replace(' ', '', $number);
$number = str_replace('(', '', $number);
$number = str_replace(')', '', $number);
$number = str_replace('-', '', $number);
$number = str_replace('.', '', $number);

// If the number starts with '+', then '1' must follow
if (substr($number, 0, 1) === "+" && substr($number, 0, 2) !== '+1') {
$this->isValid = false;
$this->errorMessage = 'Only "+1" phone numbers are allowed: '. $number;
return $this;
if (substr($number, 0, 1) === '+' && substr($number, 0, 2) !== '+1') {
throw new NanpNumberFormatterException('Only "+1" phone numbers are allowed: '.$number);
}

// If the first number starts with '1', then append a '+'
if (substr($number, 0, 1) === "1") {
$number = "+" . $number;
if (substr($number, 0, 1) === '1') {
$number = '+'.$number;
}

if ($wildcards === true) {
Expand All @@ -121,14 +90,12 @@ public function parse($number, bool $wildcards = false)
}

if (preg_match($firstNumberPattern, $number)) {
$number = "+1" . $number;
$number = '+1'.$number;
}

// The number should now be in the e164 format which is 12 characters
if (strlen($number) !== 12) {
$this->isValid = false;
$this->errorMessage = 'The number should be a valid 10,11 or 12 digit e164 NANP number: ' . $number;
return $this;
throw new NanpNumberFormatterException('The number should be a valid 10,11 or 12 digit e164 NANP number: '.$number);
}

if ($wildcards === true) {
Expand All @@ -139,36 +106,32 @@ public function parse($number, bool $wildcards = false)
$numberPattern = '/^\+1[2-9][0-9]{2}[2-9][0-9]{6}$/';
}

if (!preg_match($numberPattern, $number)) {
$this->isValid = false;
$this->errorMessage = 'The number needs to match the +1NXXNXXXXXX pattern: ' . $number;
return $this;
if (! preg_match($numberPattern, $number)) {
throw new NanpNumberFormatterException('The number needs to match the +1NXXNXXXXXX pattern: '.$number);
}

$this->isValid = true;
$this->e164 = $number;
$this->npa = substr($number, 2, 3);
$this->nxx = substr($number, 5, 3);
$this->line = substr($number, 8, 4);
$this->dotFormat = $this->npa . "." . $this->nxx . "." . $this->line;
$this->hyphenFormat = $this->npa . "-" . $this->nxx . "-" . $this->line;
$this->nationalFormat = "(" . $this->npa . ") " . $this->nxx . "-" . $this->line;
$this->internationalFormat = "+1" . " " . $this->npa . " " . $this->nxx . " " . $this->line;
$this->tendigit = $this->npa . $this->nxx . $this->line;
$this->elevendigit = "1" . $this->npa . $this->nxx . $this->line;
$this->uri = "tel:" . $this->e164;
$this->dotFormat = $this->npa.'.'.$this->nxx.'.'.$this->line;
$this->hyphenFormat = $this->npa.'-'.$this->nxx.'-'.$this->line;
$this->nationalFormat = '('.$this->npa.') '.$this->nxx.'-'.$this->line;
$this->nationalFormatPlusOne = '1 '.$this->nationalFormat;
$this->internationalFormat = '+1'.' '.$this->npa.' '.$this->nxx.' '.$this->line;
$this->tendigit = $this->npa.$this->nxx.$this->line;
$this->elevendigit = '1'.$this->npa.$this->nxx.$this->line;
$this->uri = 'tel:'.$this->e164;

return $this;
}

/**
* This takes a number of different nanp number inputs and returns
* a variety of formatted nanp numbers.
*
* @param mixed $number The phone number to be formatted
* @param bool $wildcards
* @return static
*/
/**
* @param $number
* @param bool $wildcards
* @return static
* @throws NanpNumberFormatterException
*/
public static function format($number, bool $wildcards = false)
{
$self = new static;
Expand Down
Loading

0 comments on commit cc2acab

Please sign in to comment.