class Formatter in Telephone Formatter 8
Formatter service.
@package Drupal\telephone_formatter
Hierarchy
- class \Drupal\telephone_formatter\Formatter implements FormatterInterface
Expanded class hierarchy of Formatter
1 file declares its use of Formatter
- FormatterTest.php in tests/
src/ Unit/ FormatterTest.php
1 string reference to 'Formatter'
1 service uses Formatter
File
- src/
Formatter.php, line 13
Namespace
Drupal\telephone_formatterView source
class Formatter implements FormatterInterface {
/**
* Phone utils from libphonenumber lib.
*
* @var \libphonenumber\PhoneNumberUtil
*/
protected $phoneUtils;
/**
* Language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Validator constructor.
*/
public function __construct(LanguageManagerInterface $languageManager) {
$this->languageManager = $languageManager;
$this->phoneUtils = PhoneNumberUtil::getInstance();
}
/**
* {@inheritdoc}
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Formatter:: |
protected | property | Language manager. | |
Formatter:: |
protected | property | Phone utils from libphonenumber lib. | |
Formatter:: |
public | function |
Formats telephone number into massaged one based on predefined format. Overrides FormatterInterface:: |
|
Formatter:: |
public | function | Validator constructor. |