TYPO3 Phone Number Validation and Formatting (typo3_phone)

Adds phone number functionality to TYPO3 based on the PHP port of Google's libphonenumber API by giggsey.

Installation

composer require simonschaufi/typo3-phone

Usage

For each action (here updateAction) you want to validate your object (in our case an Address with two properties "phone" and "fax") add the following code in your controller:

use SimonSchaufi\TYPO3Phone\Validation\Validator\PhoneValidator;
use TYPO3\CMS\Core\Utility\GeneralUtility;

public function initializeUpdateAction(): void
{
	if ($this->request->hasArgument('address') && $this->request->getArgument('address')) {
		/** @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver */
		$addressValidator = $this->validatorResolver->getBaseValidatorConjunction(Address::class);

		$validators = $addressValidator->getValidators();
		$validators->rewind();
		$validator = $validators->current();

		$phoneValidator = GeneralUtility::makeInstance(PhoneValidator::class, [
			// If the user enters a number prefixed with "+" then the country can be guessed.
			// If not, the following countries listed in the array will be checked against
			'countries' => ['AUTO', 'DE'],
		]);
		$validator->addPropertyValidator('phone', $phoneValidator);
		$validator->addPropertyValidator('fax', $phoneValidator);
	}
}

Alternatively you can instantiate the validator anywhere in your code like this:

use SimonSchaufi\TYPO3Phone\Validation\Validator\PhoneValidator;
use TYPO3\CMS\Core\Utility\GeneralUtility;

$phoneValidator = GeneralUtility::makeInstance(PhoneValidator::class, [
	// If the user enters a number prefixed with "+" then the country can be guessed.
	// If not, the following countries listed in the array will be checked against
	'countries' => ['AUTO', 'DE'],
	'types' => ['MOBILE']
]);

$result = $phoneValidator->validate('+3212345678');

if ($result->hasErrors()) {
	// Error handling
}

You instruct the validator to detect which country the number belongs to using the AUTO keyword (and optionally any fallback countries).

For any fallbacks, use the ISO 3166-1 alpha-2 compliant country codes, feel free to add as many country codes as you like.

The validator will try to extract the country from the number itself and then check if the number is valid for that country. If the country could not be guessed it will be validated using the fallback countries if provided. Note that country guessing will only work when phone numbers are entered in international format (prefixed with a + sign, e.g. +32 ....). Leading double zeros will NOT be parsed correctly as this isn't an established consistency.

If you don't want to use the extbase validator and instead a more low level approach, use the following code:

Info: In my case the Address Object has a property "country" that is of type \SJBR\StaticInfoTables\Domain\Model\Country

use SimonSchaufi\TYPO3Phone\Exception\NumberParseException;
use SimonSchaufi\TYPO3Phone\PhoneNumber;

if (!empty($address->getPhone())) {
	try {
		$phoneNumber = PhoneNumber::make($address->getPhone(), [$address->getCountry()->getIsoCodeA2()])->formatInternational();
		$address->setPhone($phoneNumber);
	} catch (NumberParseException $exception) {
		// Error handling
	}
}

Utility PhoneNumber class

A phone number can be wrapped in the SimonSchaufi\TYPO3Phone\PhoneNumber class to enhance it with useful utility methods. It's safe to directly reference these objects in views or when saving to the database as they will degrade gracefully to the E164 format.

use SimonSchaufi\TYPO3Phone\PhoneNumber;

(string) PhoneNumber::make('+3212/34.56.78');              // +3212345678
(string) PhoneNumber::make('012 34 56 78', 'BE');          // +3212345678
(string) PhoneNumber::make('012345678')->ofCountry('BE');  // +3212345678

Formatting

A PhoneNumber can be formatted in various ways:

use SimonSchaufi\TYPO3Phone\PhoneNumber;

PhoneNumber::make('012 34 56 78', 'BE')->format($format);       // See libphonenumber\PhoneNumberFormat
PhoneNumber::make('012 34 56 78', 'BE')->formatE164();          // +3212345678
PhoneNumber::make('012 34 56 78', 'BE')->formatInternational(); // +32 12 34 56 78
PhoneNumber::make('012 34 56 78', 'BE')->formatRFC3966();       // +32-12-34-56-78
PhoneNumber::make('012/34.56.78', 'BE')->formatNational();      // 012 34 56 78

// Formats so the number can be called straight from the provided country.
PhoneNumber::make('012 34 56 78', 'BE')->formatForCountry('BE'); // 012 34 56 78
PhoneNumber::make('012 34 56 78', 'BE')->formatForCountry('NL'); // 00 32 12 34 56 78
PhoneNumber::make('012 34 56 78', 'BE')->formatForCountry('US'); // 011 32 12 34 56 78

// Formats so the number can be clicked on and called straight from the provided country using a cellphone.
PhoneNumber::make('012 34 56 78', 'BE')->formatForMobileDialingInCountry('BE'); // 012345678
PhoneNumber::make('012 34 56 78', 'BE')->formatForMobileDialingInCountry('NL'); // +3212345678
PhoneNumber::make('012 34 56 78', 'BE')->formatForMobileDialingInCountry('US'); // +3212345678

Number information

Get some information about the phone number:

use SimonSchaufi\TYPO3Phone\PhoneNumber;

PhoneNumber::make('012 34 56 78', 'BE')->getType();              // 'fixed_line'
PhoneNumber::make('012 34 56 78', 'BE')->isOfType('fixed_line'); // true
PhoneNumber::make('012 34 56 78', 'BE')->getCountry();           // 'BE'
PhoneNumber::make('012 34 56 78', 'BE')->isOfCountry('BE');      // true
PhoneNumber::make('+32 12 34 56 78')->isOfCountry('BE');         // true

Giving thanks

This extension is heavily inspired by https://github.com/Propaganistas/Laravel-Phone. Thank you

I can't live without caffeine

Each cup of coffee helps me to be more productive and 
motivate me to maintain my open source projects.
Thank you for supporting me!