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
248 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
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
<?php | ||
|
||
namespace Misd\PhoneNumberBundle\Serializer\Normalizer; | ||
|
||
use libphonenumber\PhoneNumberFormat; | ||
use libphonenumber\PhoneNumberUtil; | ||
|
||
/** | ||
* @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; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
<?php | ||
|
||
namespace Misd\PhoneNumberBundle\Serializer\Normalizer; | ||
|
||
use libphonenumber\NumberParseException; | ||
use libphonenumber\PhoneNumber; | ||
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 | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function normalize($object, $format = null, array $context = []) | ||
{ | ||
return $this->phoneNumberUtil->format($object, $this->format); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsNormalization($data, $format = null) | ||
{ | ||
return $data instanceof PhoneNumber; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws UnexpectedValueException | ||
*/ | ||
public function denormalize($data, $class, $format = null, array $context = []) | ||
{ | ||
if (null === $data) { | ||
return; | ||
} | ||
|
||
try { | ||
return $this->phoneNumberUtil->parse($data, $this->region); | ||
} catch (NumberParseException $e) { | ||
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDenormalization($data, $type, $format = null) | ||
{ | ||
return 'libphonenumber\PhoneNumber' === $type && \is_string($data); | ||
} | ||
} |
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,60 @@ | ||
<?php | ||
|
||
namespace Misd\PhoneNumberBundle\Serializer\Normalizer; | ||
|
||
use libphonenumber\NumberParseException; | ||
use libphonenumber\PhoneNumber; | ||
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 | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null | ||
{ | ||
return $this->phoneNumberUtil->format($object, $this->format); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsNormalization(mixed $data, string $format = null): bool | ||
{ | ||
return $data instanceof PhoneNumber; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws UnexpectedValueException | ||
*/ | ||
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed | ||
{ | ||
if (null === $data) { | ||
return null; | ||
} | ||
|
||
try { | ||
return $this->phoneNumberUtil->parse($data, $this->region); | ||
} catch (NumberParseException $e) { | ||
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDenormalization(mixed $data, string $type, string $format = null): bool | ||
{ | ||
return 'libphonenumber\PhoneNumber' === $type && \is_string($data); | ||
} | ||
} |
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
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.