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

Overview

TYPO3 Phone

Latest Stable Version Total Downloads License TYPO3 TYPO3

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:

['AUTO', 'DE'], ]); $validator->addPropertyValidator('phone', $phoneValidator); $validator->addPropertyValidator('fax', $phoneValidator); } }">
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:

['AUTO', 'DE'], 'types' => ['MOBILE'] ]); $result = $phoneValidator->validate('+3212345678'); if ($result->hasErrors()) { // Error handling }">
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\Exceptions\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

You might also like...
Asset Component is a port of Laravel 3 Asset for Orchestra Platform.

Asset Component is a port of Laravel 3 Asset for Orchestra Platform. The component main functionality is to allow asset declaration to be handle dynamically and asset dependencies can be resolve directly from the container. It however is not intended to becoma an asset pipeline package for Laravel, for such purpose we would recommend to use Grunt or Gulp.

Magewire is a Laravel Livewire port for Magento 2.

Magewire is a Laravel Livewire port for Magento 2. The goal is to make it fun and easy to build modern, reactive and dynamic interfaces, without leaving the comfort of Magento's core layout and template systems. Magewire can be the missing piece when you intend to build dynamic and reactive features, but don't require or feel comfortable working with a full JavaScript framework like Vue or React.

Laravel package to convert English numbers to Bangla number or Bangla text, Bangla month name and Bangla Money Format

Number to Bangla Number, Word or Month Name in Laravel | Get Wordpress Plugin Laravel package to convert English numbers to Bangla number or Bangla te

Laravel package to convert English numbers to Bangla number or Bangla text, Bangla month name and Bangla Money Format

Number to Bangla Number, Word or Month Name in Laravel | Get Wordpress Plugin Laravel package to convert English numbers to Bangla number or Bangla te

Log executed Laravel SQL queries and their line number and more

A lightweight laravel package for logging executed SQL queries, line number and more

Adds a way to write php and run it directly in Laravels' Artisan Tinker.
Adds a way to write php and run it directly in Laravels' Artisan Tinker.

Adds a way to write php in PhpStorm/IDEA and run it directly as if through laravel artisan tinker - allowing you to quickly run a piece of code with a

Livewire component that brings Spotlight/Alfred-like functionality to your Laravel application.
Livewire component that brings Spotlight/Alfred-like functionality to your Laravel application.

About LivewireUI Spotlight LivewireUI Spotlight is a Livewire component that provides Spotlight/Alfred-like functionality to your Laravel application.

A Simple MOFH clientarea for free like infinityfree and minimal functionality

Project Hustal Project Hustal is a free of cost MOFH clientarea for account management and support services. It have easy to use features and a much l

Useful blade components and functionality for most Laravel projects.

laravel-base Note: Package is still in early stages of development, so functionality is subject to change. LaravelBase is a package I've created to pr

Comments
  • [FEATURE] Add TYPO3 10 compatibility

    [FEATURE] Add TYPO3 10 compatibility

    Pull Request

    Prerequisites

    • [x] Changes have been tested on TYPO3 v9.5 LTS
    • [x] Changes have been tested on TYPO3 v10.4 LTS
    • [ ] Changes have been tested on TYPO3 dev-master
    • [x] Changes have been tested on PHP 7.2.x
    • [x] Changes have been tested on PHP 7.3.x
    • [x] Changes have been tested on PHP 7.4.x
    • [ ] Changes have been tested on PHP 8.0.x
    • [x] Changes have been checked for CGL compliance php-cs-fixer fix

    Description

    Add TYPO3 10 compatibility

    opened by simonschaufi 0
Releases(v2.0.2)
Owner
Simon Schaufelberger
💻 Laravel and TYPO3 Freelancer ~~~~ 🏕 World traveler and PHP Coder ~~~~ 👥 Developer of anschlussfinder.net. ~~ 🕓 Core developer of Kimai time-tracking
Simon Schaufelberger
Phone number functionality for Laravel

Laravel Phone Adds phone number functionality to Laravel and Lumen based on the PHP port of Google's libphonenumber API by giggsey. Table of Contents

null 2.1k Dec 31, 2022
Laravel Dutch Phone Number Validator

Laravel Dutch Phone Number Validator Validate if the given phone number is a valid Dutch phone number Table of Contents Installation Usage Translation

Tim Wassenburg 0 May 30, 2022
Integrates libphonenumber into your Symfony application

PhoneNumberBundle This bundle is a fork of misd-service-development/phone-number-bundle. As this project doesn't look maintained anymore, we decided t

Olivier Dolbeau 161 Dec 23, 2022
A Laravel package that adds a simple image functionality to any Laravel model

Laraimage A Laravel package that adds a simple image functionality to any Laravel model Introduction Laraimage served four use cases when using images

Hussein Feras 52 Jul 17, 2022
A laravel package for sending otp to mobile phone

A laravel package for sending otp to mobile phone Installation composer require gabbyti/bulksmsnigeria-otp Vendor Publish This package includes a conf

Gabriel 3 May 10, 2021
This package provides convenient methods for making token code, sending and verifying mobile phone verification requests.

Laravel Mobile Verification Introduction Many web applications require users to verify their mobile phone numbers before using the application. Rather

M.Fouladgar 347 Dec 25, 2022
A package for Myanmar Font, Phone and other Myanmar tools using Laravel Macro

Laravel Myanmar Tools A package for Myanmar Font, Phone and other Myanmar tools using Laravel Macro. Installation composer require pyaesoneaung/larave

Pyae Sone Aung 22 Dec 20, 2022
Example of using TALL stack to select country phone code.

Select country phone code using TALL stack About Example of using TALL stack to select country phone code. Each item represents a country. Each item h

Fernando Chagas 3 Jul 27, 2022
PHP port of Underscore.js

Underscore.php Underscore.php is a PHP port of Underscore.js. In addition to porting Underscore's functionality, Underscore.php includes matching unit

Brian Haveri 1.2k Dec 25, 2022
This is a Laravel port of the local_time gem from Basecamp.

This is a Laravel port of the local_time gem from Basecamp. It makes it easy to display date and time to users in their local time. Its Blade componen

Tony Messias 56 Oct 7, 2022