mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-07-01 14:36:13 +00:00
add function to format phonenumber using twilio (if configured)
This commit is contained in:
parent
87a01e4ab3
commit
241815e687
@ -87,4 +87,4 @@ Master branch
|
|||||||
- fix errors in pagination
|
- fix errors in pagination
|
||||||
- fix search: usage of parenthesis
|
- fix search: usage of parenthesis
|
||||||
- add DQL function REPLACE for replacing in strings: "REPLACE(string, 'from', 'to')"
|
- add DQL function REPLACE for replacing in strings: "REPLACE(string, 'from', 'to')"
|
||||||
|
- add function to format phonenumber
|
||||||
|
@ -53,6 +53,7 @@ class PhonenumberHelper
|
|||||||
protected $cachePool;
|
protected $cachePool;
|
||||||
|
|
||||||
const LOOKUP_URI = 'https://lookups.twilio.com/v1/PhoneNumbers/%s';
|
const LOOKUP_URI = 'https://lookups.twilio.com/v1/PhoneNumbers/%s';
|
||||||
|
const FORMAT_URI = 'https://lookups.twilio.com/v1/PhoneNumbers/%s';
|
||||||
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
@ -121,6 +122,64 @@ class PhonenumberHelper
|
|||||||
return \in_array($validation, [ 'landline', 'voip' ]);
|
return \in_array($validation, [ 'landline', 'voip' ]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function format($phonenumber)
|
||||||
|
{
|
||||||
|
return $this->performTwilioFormat($phonenumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function performTwilioFormat($phonenumber)
|
||||||
|
{
|
||||||
|
if (FALSE === $this->isPhonenumberValidationConfigured()) {
|
||||||
|
return $phonenumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter only number
|
||||||
|
$filtered = \preg_replace("/[^0-9]/", "", $phonenumber);
|
||||||
|
|
||||||
|
$item = $this->cachePool->getItem('pnum_format_nat_'.$filtered);
|
||||||
|
|
||||||
|
if ($item->isHit()) {
|
||||||
|
return $item->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$response = $this->twilioClient->get(sprintf(self::FORMAT_URI, '+'.$filtered), [
|
||||||
|
'http_errors' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
} catch (ClientException $e) {
|
||||||
|
$this->logger->error("[phonenumber helper] Could not format number "
|
||||||
|
. "due to client error", [
|
||||||
|
"message" => $e->getResponseBodySummary($e->getResponse()),
|
||||||
|
"status_code" => $e->getResponse()->getStatusCode(),
|
||||||
|
"phonenumber" => $phonenumber
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $phonenumber;
|
||||||
|
} catch (ServerException $e) {
|
||||||
|
$this->logger->error("[phonenumber helper] Could not format number "
|
||||||
|
. "due to server error", [
|
||||||
|
"message" => $e->getResponseBodySummary($e->getResponse()),
|
||||||
|
"status_code" => $e->getResponse()->getStatusCode(),
|
||||||
|
"phonenumber" => $phonenumber
|
||||||
|
]);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$format = \json_decode($response->getBody())->national_format;
|
||||||
|
|
||||||
|
$item
|
||||||
|
->set($format)
|
||||||
|
// expires after 3d
|
||||||
|
->expiresAfter(3600 * 24 * 3)
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->cachePool->save($item);
|
||||||
|
|
||||||
|
return $format;
|
||||||
|
}
|
||||||
|
|
||||||
protected function performTwilioLookup($phonenumber)
|
protected function performTwilioLookup($phonenumber)
|
||||||
{
|
{
|
||||||
if (FALSE === $this->isPhonenumberValidationConfigured()) {
|
if (FALSE === $this->isPhonenumberValidationConfigured()) {
|
||||||
|
52
Phonenumber/Templating.php
Normal file
52
Phonenumber/Templating.php
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2019 Champs Libres Cooperative <info@champs-libres.coop>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
namespace Chill\MainBundle\Phonenumber;
|
||||||
|
|
||||||
|
use Twig\Extension\AbstractExtension;
|
||||||
|
use Twig\TwigFilter;
|
||||||
|
use Chill\MainBundle\Phonenumber\PhonenumberHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class Templating extends AbstractExtension
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var PhonenumberHelper
|
||||||
|
*/
|
||||||
|
protected $phonenumberHelper;
|
||||||
|
|
||||||
|
public function __construct(PhonenumberHelper $phonenumberHelper)
|
||||||
|
{
|
||||||
|
$this->phonenumberHelper = $phonenumberHelper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFilters()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
new TwigFilter('chill_format_phonenumber', [$this, 'formatPhonenumber'])
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function formatPhonenumber($phonenumber)
|
||||||
|
{
|
||||||
|
return $this->phonenumberHelper->format($phonenumber);
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,12 @@ services:
|
|||||||
$config: '%chill_main.phone_helper%'
|
$config: '%chill_main.phone_helper%'
|
||||||
$cachePool: '@cache.user_data'
|
$cachePool: '@cache.user_data'
|
||||||
|
|
||||||
|
Chill\MainBundle\Phonenumber\Templating:
|
||||||
|
arguments:
|
||||||
|
$phonenumberHelper: '@Chill\MainBundle\Phonenumber\PhonenumberHelper'
|
||||||
|
tags:
|
||||||
|
- { name: twig.extension }
|
||||||
|
|
||||||
Chill\MainBundle\Validation\Validator\ValidPhonenumber:
|
Chill\MainBundle\Validation\Validator\ValidPhonenumber:
|
||||||
arguments:
|
arguments:
|
||||||
$logger: '@Psr\Log\LoggerInterface'
|
$logger: '@Psr\Log\LoggerInterface'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user