Skip to content

Commit

Permalink
enhance(phone-number): Add full country name to the phone number array (
Browse files Browse the repository at this point in the history
#57)

* chore(phone-number): Sort phone number array by ascending
* chore(docs): Use `toArray()` output for usage example
* fix(phone-number): Add missing generic `number` item to array
  • Loading branch information
Log1x authored Nov 3, 2022
1 parent cd990a5 commit f8b70fe
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ Download the release `.zip` and install into `wp-content/plugins`.

Pretty straight forward usage. You can optionally set a default country.

Calling the field will return an [arrayable](https://github.com/Log1x/acf-phone-number/blob/master/src/PhoneNumber.php#L225-L246) object containing everything you need about your number:
Calling the field will return an [arrayable](https://github.com/Log1x/acf-phone-number/blob/master/src/PhoneNumber.php#L254-L272) object containing everything you need about your number:

```php
{
+"number": "+1 405-867-5309"
+"country": "US"
+"uri": "tel:+14058675309"
+"e164": "+14058675309"
+"rfc3966": "tel:+1-405-867-5309"
+"national": "(405) 867-5309"
+"international": "+1 405-867-5309"
+"carrier": ""
+"location": "Oklahoma"
+"timezone": array:1 [▼
^ array:10 [
"carrier" => ""
"country" => "United States"
"e164" => "+14058675309"
"international" => "+1 405-867-5309"
"location" => "Oklahoma"
"national" => "(405) 867-5309"
"number" => "(405) 867-5309"
"rfc3966" => "tel:+1-405-867-5309"
"timezone" => array:1 [
0 => "America/Chicago"
]
}
"uri" => "tel:+14058675309"
]
```

### ACF Composer
Expand Down
2 changes: 1 addition & 1 deletion plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: Advanced Custom Fields: Phone Number
* Plugin URI: https://github.com/log1x/acf-phone-number
* Description: A real ACF phone number field.
* Version: 1.2.0
* Version: 1.2.1
* Author: Brandon Nifong
* Author URI: https://github.com/log1x
*/
Expand Down
36 changes: 31 additions & 5 deletions src/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class PhoneNumber
*/
protected $number;

/**
* The phone number country.
*
* @return string
*/
protected $country;

/**
* Create a new phone number instance.
*
Expand Down Expand Up @@ -58,7 +65,10 @@ public function parse($value = null)
}

try {
$this->number = $this->instance->parse($value['number'], strtoupper($value['country']));
$this->number = $this->instance->parse(
$value['number'],
$this->country = strtoupper($value['country'])
);
} catch (NumberParseException $e) {
//
}
Expand Down Expand Up @@ -196,6 +206,20 @@ public function timezone()
->getTimeZonesForNumber($this->number);
}

/**
* Retrieve the country for the current phone number.
*
* @return string
*/
public function country()
{
if (! $this->isValid()) {
return '';
}

return Locale::getDisplayRegion('-' . $this->country, 'en');
}

/**
* Determine whether the current phone number is valid.
*
Expand Down Expand Up @@ -234,14 +258,16 @@ public function toArray()
}

return [
'uri' => $this->uri(),
'carrier' => $this->carrier(),
'country' => $this->country(),
'e164' => $this->e164(),
'rfc3966' => $this->rfc3966(),
'national' => $this->national(),
'international' => $this->international(),
'carrier' => $this->carrier(),
'location' => $this->location(),
'national' => $this->national(),
'number' => $this->national(),
'rfc3966' => $this->rfc3966(),
'timezone' => $this->timezone(),
'uri' => $this->uri(),
];
}

Expand Down

0 comments on commit f8b70fe

Please sign in to comment.