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')) {
		$addressValidator = $this->validatorResolver->getBaseValidatorConjunction(Address::class);

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

		/** @var PhoneValidator $phoneValidator */
		$phoneValidator = $this->validatorResolver->createValidator(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' => ['DE'],
			'international' => true,
		]);

		$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);
$phoneValidator->setOptions([
	// 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' => ['DE'],
	'types' => ['mobile'],
	'international' => true,
]);

$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 = (new PhoneNumber($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) new PhoneNumber('+3212/34.56.78');     // +3212345678
(string) new PhoneNumber('012 34 56 78', 'BE'); // +3212345678

Formatting

A PhoneNumber can be formatted in various ways:

use SimonSchaufi\TYPO3Phone\PhoneNumber;

$phone = new PhoneNumber('012/34.56.78', 'BE');

$phone->format($format);       // See libphonenumber\PhoneNumberFormat
$phone->formatE164();          // +3212345678
$phone->formatInternational(); // +32 12 34 56 78
$phone->formatRFC3966();       // +32-12-34-56-78
$phone->formatNational();      // 012 34 56 78

// Formats so the number can be called straight from the provided country.
$phone->formatForCountry('BE'); // 012 34 56 78
$phone->formatForCountry('NL'); // 00 32 12 34 56 78
$phone->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.
$phone->formatForMobileDialingInCountry('BE'); // 012345678
$phone->formatForMobileDialingInCountry('NL'); // +3212345678
$phone->formatForMobileDialingInCountry('US'); // +3212345678

Number information

Get some information about the phone number:

use SimonSchaufi\TYPO3Phone\PhoneNumber;

$phone = new PhoneNumber('012 34 56 78', 'BE');

$phone->getType();              // 'fixed_line'
$phone->isOfType('fixed_line'); // true
$phone->getCountry();           // 'BE'
$phone->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!