normalization of phonenumber

This commit is contained in:
Julien Fastré 2022-03-02 14:10:48 +01:00
parent 2a9f1dc238
commit f4f488dad1
4 changed files with 68 additions and 3 deletions

View File

@ -251,7 +251,7 @@ final class PhonenumberHelper implements PhoneNumberHelperInterface
$item = $this->cachePool->getItem('pnum_' . $filtered);
if ($item->isHit()) {
//return $item->get();
return $item->get();
}
try {

View File

@ -0,0 +1,60 @@
<?php
namespace Chill\MainBundle\Serializer\Normalizer;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberUtil;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Serializer\Exception\CircularReferenceException;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class PhonenumberNormalizer implements NormalizerInterface, DenormalizerInterface
{
private PhoneNumberUtil $phoneNumberUtil;
private string $defaultCarrierCode;
public function __construct(ParameterBagInterface $parameterBag)
{
$this->defaultCarrierCode = $parameterBag->get('chill_main')['phone_helper']['default_carrier_code'];
$this->phoneNumberUtil = PhoneNumberUtil::getInstance();
}
public function normalize($object, string $format = null, array $context = []): string
{
return $this->phoneNumberUtil->formatOutOfCountryCallingNumber($object, $this->defaultCarrierCode);
}
public function supportsNormalization($data, string $format = null)
{
return $data instanceof PhoneNumber;
}
/**
* {@inheritdoc}
*
* @throws UnexpectedValueException
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
try {
return $this->phoneNumberUtil->parse($data, $this->defaultCarrierCode);
} catch (NumberParseException $e) {
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return $type === 'libphonenumber\PhoneNumber';
}
}

View File

@ -372,6 +372,7 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
/**
* The person's mobile phone number.
*
* @PhonenumberConstraint(type="mobile")
* @ORM\Column(type="phone_number")
*/
private ?PhoneNumber $mobilenumber;
@ -425,6 +426,9 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
* The person's phonenumber.
*
* @ORM\Column(type="phone_number")
* @PhonenumberConstraint(
* type="landline",
* )
*/
private ?PhoneNumber $phonenumber;

View File

@ -21,6 +21,7 @@ use Chill\PersonBundle\Repository\PersonRepository;
use DateTime;
use DateTimeImmutable;
use Doctrine\Common\Collections\Collection;
use libphonenumber\PhoneNumber;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
@ -111,12 +112,12 @@ class PersonJsonNormalizer implements DenormalizerAwareInterface, NormalizerAwar
break;
case 'phonenumber':
$person->setPhonenumber($this->phoneNumberHelper->denormalize($data[$item]));
$person->setPhonenumber($this->denormalizer->denormalize($data[$item], PhoneNumber::class, $format, $context));
break;
case 'mobilenumber':
$person->setMobilenumber($this->phoneNumberHelper->denormalize($data[$item]));
$person->setMobilenumber($this->denormalizer->denormalize($data[$item], PhoneNumber::class, $format, $context));
break;