diff --git a/CHANGELOG.md b/CHANGELOG.md index df374c85..48e7155d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [3.6.2] - 2022-02-24 + +### Fixed + +- Fix option format while using the validation constraint as attribute + ## [3.6.1] - 2021-12-29 - Added return types diff --git a/src/Validator/Constraints/PhoneNumber.php b/src/Validator/Constraints/PhoneNumber.php index b2dd8356..4d24a481 100644 --- a/src/Validator/Constraints/PhoneNumber.php +++ b/src/Validator/Constraints/PhoneNumber.php @@ -49,21 +49,23 @@ class PhoneNumber extends Constraint /** * {@inheritdoc} * - * @param string|array|null $format + * @param int|array|null $format Specify the format (\libphonenumber\PhoneNumberFormat::*) + * or options (an associative array) * @param string|array|null $type */ public function __construct($format = null, $type = null, string $defaultRegion = null, string $regionPath = null, string $message = null, array $groups = null, $payload = null, array $options = []) { if (\is_array($format)) { + @trigger_error('Usage of the argument $format to specify options is deprecated and will be removed in 4.0. Use "$option" argument instead.', \E_USER_DEPRECATED); $options = array_merge($format, $options); - } elseif (null !== $format) { - $options['value'] = $format; + } else { + $phoneFormat = $format; } parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; - $this->format = $format ?? $this->format; + $this->format = $phoneFormat ?? $this->format; $this->type = $type ?? $this->type; $this->defaultRegion = $defaultRegion ?? $this->defaultRegion; $this->regionPath = $regionPath ?? $this->regionPath; diff --git a/tests/Validator/Constraints/PhoneNumberTest.php b/tests/Validator/Constraints/PhoneNumberTest.php index ceb348e8..f1ec5ce1 100644 --- a/tests/Validator/Constraints/PhoneNumberTest.php +++ b/tests/Validator/Constraints/PhoneNumberTest.php @@ -11,6 +11,7 @@ namespace Misd\PhoneNumberBundle\Tests\Validator\Constraints; +use libphonenumber\PhoneNumberFormat; use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber; use PHPUnit\Framework\TestCase; @@ -32,44 +33,39 @@ public function testProperties() /** * @dataProvider messageProvider */ - public function testMessage($message, $type, $expectedMessage) + public function testMessage($message, $type, $format, $expectedMessage) { - $phoneNumber = new PhoneNumber(); - - if (null !== $message) { - $phoneNumber->message = $message; - } - if (null !== $type) { - $phoneNumber->type = $type; - } - + $phoneNumber = new PhoneNumber($format, $type, null, null, $message); $this->assertSame($expectedMessage, $phoneNumber->getMessage()); + $this->assertSame($format, $phoneNumber->format); } /** * 0 => Message (optional) * 1 => Type (optional) - * 2 => Expected message. + * 2 => Format (optional) + * 3 => Expected message. */ public function messageProvider() { return [ - [null, null, 'This value is not a valid phone number.'], - [null, 'fixed_line', 'This value is not a valid fixed-line number.'], - [null, 'mobile', 'This value is not a valid mobile number.'], - [null, 'pager', 'This value is not a valid pager number.'], - [null, 'personal_number', 'This value is not a valid personal number.'], - [null, 'premium_rate', 'This value is not a valid premium-rate number.'], - [null, 'shared_cost', 'This value is not a valid shared-cost number.'], - [null, 'toll_free', 'This value is not a valid toll-free number.'], - [null, 'uan', 'This value is not a valid UAN.'], - [null, 'voip', 'This value is not a valid VoIP number.'], - [null, 'voicemail', 'This value is not a valid voicemail access number.'], - [null, ['fixed_line', 'voip'], 'This value is not a valid number.'], - [null, ['uan', 'fixed_line'], 'This value is not a valid number.'], - ['foo', null, 'foo'], - ['foo', 'fixed_line', 'foo'], - ['foo', 'mobile', 'foo'], + [null, null, null, 'This value is not a valid phone number.'], + [null, 'fixed_line', null, 'This value is not a valid fixed-line number.'], + [null, 'mobile', null, 'This value is not a valid mobile number.'], + [null, 'pager', null, 'This value is not a valid pager number.'], + [null, 'personal_number', null, 'This value is not a valid personal number.'], + [null, 'premium_rate', null, 'This value is not a valid premium-rate number.'], + [null, 'shared_cost', null, 'This value is not a valid shared-cost number.'], + [null, 'toll_free', null, 'This value is not a valid toll-free number.'], + [null, 'uan', null, 'This value is not a valid UAN.'], + [null, 'voip', null, 'This value is not a valid VoIP number.'], + [null, 'voicemail', null, 'This value is not a valid voicemail access number.'], + [null, ['fixed_line', 'voip'], null, 'This value is not a valid number.'], + [null, ['uan', 'fixed_line'], null, 'This value is not a valid number.'], + ['foo', null, null, 'foo'], + ['foo', 'fixed_line', null, 'foo'], + ['foo', 'mobile', null, 'foo'], + [null, null, PhoneNumberFormat::E164, 'This value is not a valid phone number.'], ]; } } diff --git a/tests/Validator/Constraints/PhoneNumberValidatorTest.php b/tests/Validator/Constraints/PhoneNumberValidatorTest.php index d417455d..e4abdf9f 100644 --- a/tests/Validator/Constraints/PhoneNumberValidatorTest.php +++ b/tests/Validator/Constraints/PhoneNumberValidatorTest.php @@ -11,6 +11,7 @@ namespace Misd\PhoneNumberBundle\Tests\Validator\Constraints; +use libphonenumber\PhoneNumberFormat; use libphonenumber\PhoneNumberUtil; use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber; use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumberValidator; @@ -50,21 +51,9 @@ protected function setUp(): void /** * @dataProvider validateProvider */ - public function testValidate($value, $violates, $type = null, $defaultRegion = null, $regionPath = null) + public function testValidate($value, $violates, $type = null, $defaultRegion = null, $regionPath = null, $format = null) { - $constraint = new PhoneNumber(); - - if (null !== $type) { - $constraint->type = $type; - } - - if (null !== $defaultRegion) { - $constraint->defaultRegion = $defaultRegion; - } - - if (null !== $regionPath) { - $constraint->regionPath = $regionPath; - } + $constraint = new PhoneNumber($format, $type, $defaultRegion, $regionPath); if (true === $violates) { $constraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class); @@ -162,6 +151,8 @@ public function validateProvider() ['foo', true], ['+441234567890', true, 'mobile', null, 'regionPath'], ['+33606060606', false, 'mobile', null, 'regionPath'], + ['+33606060606', false, 'mobile', null, null, PhoneNumberFormat::E164], + ['2015555555', true, null, null, null, PhoneNumberFormat::E164], ]; }