PHP code example of simonschaufi / typo3-phone

1. Go to this page and download the library: Download simonschaufi/typo3-phone library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simonschaufi / typo3-phone example snippets


use SimonSchaufi\TYPO3Phone\Validation\Validator\PhoneValidator;

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);
	}
}

use SimonSchaufi\TYPO3Phone\Validation\Validator\PhoneValidator;

$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
}

$phoneValidator->setOptions([
    'international' => true,
]);

$phoneValidator->setOptions([
    'types' => ['mobile'],
]);

$phoneValidator->setOptions([
    'lenient' => true,
]);

use SimonSchaufi\TYPO3Phone\Exceptions\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
	}
}

use SimonSchaufi\TYPO3Phone\PhoneNumber;

(string) new PhoneNumber('+3212/34.56.78');     // +3212345678
(string) new PhoneNumber('012 34 56 78', 'BE'); // +3212345678

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

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

use SimonSchaufi\TYPO3Phone\PhoneNumber;

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

$phone->equals('012/34.56.78', 'BE')       // true
$phone->equals('+32 12 34 56 78')          // true
$phone->equals($anotherPhoneObject)        // true/false

$phone->notEquals('045 67 89 10', 'BE')    // true
$phone->notEquals('+32 45 67 89 10')       // true
$phone->notEquals($anotherPhoneObject)     // true/false