forked from misd-service-development/phone-number-bundle
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
261 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/Serializer/Normalizer/CommonPhoneNumberNormalizerTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Misd\PhoneNumberBundle\Serializer\Normalizer; | ||
|
||
use libphonenumber\NumberParseException; | ||
use libphonenumber\PhoneNumber; | ||
use libphonenumber\PhoneNumberFormat; | ||
use libphonenumber\PhoneNumberUtil; | ||
use Symfony\Component\Serializer\Exception\UnexpectedValueException; | ||
|
||
/** | ||
* @internal | ||
* | ||
* Do not use directly. Just used for achieving compatibility with Symfony < 6 and >= 6. | ||
*/ | ||
trait CommonPhoneNumberNormalizerTrait | ||
{ | ||
/** | ||
* Region code. | ||
* | ||
* @var string | ||
*/ | ||
private $region; | ||
|
||
/** | ||
* Display format. | ||
* | ||
* @var int | ||
*/ | ||
private $format; | ||
|
||
/** | ||
* Display format. | ||
* | ||
* @var PhoneNumberUtil | ||
*/ | ||
private $phoneNumberUtil; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param PhoneNumberUtil $phoneNumberUtil phone number utility | ||
* @param string $region region code | ||
* @param int $format display format | ||
*/ | ||
public function __construct(PhoneNumberUtil $phoneNumberUtil, $region = PhoneNumberUtil::UNKNOWN_REGION, $format = PhoneNumberFormat::E164) | ||
{ | ||
$this->phoneNumberUtil = $phoneNumberUtil; | ||
$this->region = $region; | ||
$this->format = $format; | ||
} | ||
|
||
private function doNormalize($object, $format = null, array $context = []) | ||
{ | ||
return $this->phoneNumberUtil->format($object, $this->format); | ||
} | ||
|
||
private function doSupportsNormalization($data, $format = null) | ||
{ | ||
return $data instanceof PhoneNumber; | ||
} | ||
|
||
private function doDenormalize($data, $class, $format = null, array $context = []) | ||
{ | ||
if (null === $data) { | ||
return null; | ||
} | ||
|
||
try { | ||
return $this->phoneNumberUtil->parse($data, $this->region); | ||
} catch (NumberParseException $e) { | ||
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
private function doSupportsDenormalization($data, $type, $format = null) | ||
{ | ||
return 'libphonenumber\PhoneNumber' === $type && \is_string($data); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/Serializer/Normalizer/LegacyPhoneNumberNormalizerTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Misd\PhoneNumberBundle\Serializer\Normalizer; | ||
|
||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
use Symfony\Component\Serializer\Exception\UnexpectedValueException; | ||
|
||
/** | ||
* @internal | ||
* | ||
* Do not use directly. Just used for achieving compatibility with Symfony < 6 and >= 6. | ||
*/ | ||
trait LegacyPhoneNumberNormalizerTrait | ||
{ | ||
use CommonPhoneNumberNormalizerTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function normalize($object, $format = null, array $context = []) | ||
{ | ||
return $this->doNormalize($object, $format, $context); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsNormalization($data, $format = null) | ||
{ | ||
return $this->doSupportsNormalization($data, $format); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws UnexpectedValueException | ||
*/ | ||
public function denormalize($data, $class, $format = null, array $context = []) | ||
{ | ||
return $this->doDenormalize($data, $class, $format, $context); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDenormalization($data, $type, $format = null) | ||
{ | ||
return $this->doSupportsDenormalization($data, $type, $format); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Misd\PhoneNumberBundle\Serializer\Normalizer; | ||
|
||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
use Symfony\Component\Serializer\Exception\UnexpectedValueException; | ||
|
||
/** | ||
* @internal | ||
* | ||
* Do not use directly. Just used for achieving compatibility with Symfony < 6 and >= 6. | ||
*/ | ||
trait PhoneNumberNormalizerTrait | ||
{ | ||
use CommonPhoneNumberNormalizerTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null | ||
{ | ||
return $this->doNormalize($object, $format, $context); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsNormalization(mixed $data, string $format = null): bool | ||
{ | ||
return $this->doSupportsNormalization($data, $format); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws UnexpectedValueException | ||
*/ | ||
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed | ||
{ | ||
return $this->doDenormalize($data, $type, $format, $context); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDenormalization(mixed $data, string $type, string $format = null): bool | ||
{ | ||
return $this->doSupportsDenormalization($data, $type, $format); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.