forked from misd-service-development/phone-number-bundle
-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow Symfony 6 #96
Merged
Merged
allow Symfony 6 #96
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f4bd90a
add support for Symfony 6
dmaicher cc786e3
remove LegacyPhoneNumberNormalizerTrait
dmaicher a46a61b
revert PhoneNumberNormalizer changes
dmaicher aa38f51
require symfony/serializer ^6.0.1
dmaicher 43a6137
add return type hints to silence deprecation warnings
dmaicher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||
} | ||
} |
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 | ||
dmaicher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stability requirement can be removed once Symfony 6.0.1 was released