You are here

public function Formatter::format in Telephone Formatter 8

Formats telephone number into massaged one based on predefined format.

Parameters

string $input: Input phone number.

string $format: Format option.

null|string $region: Country code.

Return value

string Formatted string.

Overrides FormatterInterface::format

File

src/Formatter.php, line 40

Class

Formatter
Formatter service.

Namespace

Drupal\telephone_formatter

Code

public function format($input, $format, $region = NULL) {

  // Parse to object.
  $number = $this->phoneUtils
    ->parse($input, $region);

  // Ensure number is valid.
  if (!$this->phoneUtils
    ->isValidNumber($number)) {
    throw new \InvalidArgumentException('Number is invalid.');
  }

  // Format phone number.
  $value = $this->phoneUtils
    ->format($number, $format);
  return $value;
}