From cce88035c01b90ae85b659185fce9233d1dfd829 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Sun, 9 Apr 2017 17:48:10 -0300 Subject: [PATCH 1/2] Add `canonicalize` option for `Locale` validator --- .../Validator/Constraints/Locale.php | 14 +++++++ .../Validator/Constraints/LocaleValidator.php | 11 +++++ .../Tests/Constraints/LocaleValidatorTest.php | 41 +++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/src/Symfony/Component/Validator/Constraints/Locale.php b/src/Symfony/Component/Validator/Constraints/Locale.php index 5aa7070402e2a..6f91b6fdc230a 100644 --- a/src/Symfony/Component/Validator/Constraints/Locale.php +++ b/src/Symfony/Component/Validator/Constraints/Locale.php @@ -28,4 +28,18 @@ class Locale extends Constraint ); public $message = 'This value is not a valid locale.'; + + public $canonicalize = false; + + /** + * {@inheritdoc} + */ + public function __construct($options = null) + { + if (isset($options['canonicalize'])) { + $this->canonicalize = $options['canonicalize']; + } + + parent::__construct($options); + } } diff --git a/src/Symfony/Component/Validator/Constraints/LocaleValidator.php b/src/Symfony/Component/Validator/Constraints/LocaleValidator.php index 93eab8cb7e757..ef2a901a5eadd 100644 --- a/src/Symfony/Component/Validator/Constraints/LocaleValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LocaleValidator.php @@ -41,6 +41,9 @@ public function validate($value, Constraint $constraint) } $value = (string) $value; + if ($constraint->canonicalize) { + $value = \Locale::canonicalize($value); + } $locales = Intl::getLocaleBundle()->getLocaleNames(); $aliases = Intl::getLocaleBundle()->getAliases(); @@ -51,4 +54,12 @@ public function validate($value, Constraint $constraint) ->addViolation(); } } + + /** + * {@inheritdoc} + */ + public function getDefaultOption() + { + return 'canonicalize'; + } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php index 29409e61f52f7..ae418566accbd 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php @@ -90,4 +90,45 @@ public function getInvalidLocales() array('foobar'), ); } + + /** + * @dataProvider getUncanonicalizedLocales + */ + public function testInvalidLocalesWithoutCanonicalization($locale) + { + $constraint = new Locale(array( + 'message' => 'myMessage', + )); + + $this->validator->validate($locale, $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$locale.'"') + ->setCode(Locale::NO_SUCH_LOCALE_ERROR) + ->assertRaised(); + } + + /** + * @dataProvider getUncanonicalizedLocales + */ + public function testValidLocalesWithCanonicalization($locale) + { + $constraint = new Locale(array( + 'message' => 'myMessage', + 'canonicalize' => true, + )); + + $this->validator->validate($locale, $constraint); + + $this->assertNoViolation(); + } + + public function getUncanonicalizedLocales() + { + return array( + array('en-US'), + array('es-AR'), + array('fr_FR.utf8'), + ); + } } From b07d5a74fcb4a235acf7ba886b58c517c09ce1cf Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 6 Feb 2018 15:47:15 -0300 Subject: [PATCH 2/2] Rebased against `master` --- .../Component/Validator/Constraints/Locale.php | 13 ------------- .../Validator/Constraints/LocaleValidator.php | 8 -------- .../Tests/Constraints/LocaleValidatorTest.php | 6 +++--- 3 files changed, 3 insertions(+), 24 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Locale.php b/src/Symfony/Component/Validator/Constraints/Locale.php index 6f91b6fdc230a..0ee4bf65b8548 100644 --- a/src/Symfony/Component/Validator/Constraints/Locale.php +++ b/src/Symfony/Component/Validator/Constraints/Locale.php @@ -28,18 +28,5 @@ class Locale extends Constraint ); public $message = 'This value is not a valid locale.'; - public $canonicalize = false; - - /** - * {@inheritdoc} - */ - public function __construct($options = null) - { - if (isset($options['canonicalize'])) { - $this->canonicalize = $options['canonicalize']; - } - - parent::__construct($options); - } } diff --git a/src/Symfony/Component/Validator/Constraints/LocaleValidator.php b/src/Symfony/Component/Validator/Constraints/LocaleValidator.php index ef2a901a5eadd..bf73a560c443d 100644 --- a/src/Symfony/Component/Validator/Constraints/LocaleValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LocaleValidator.php @@ -54,12 +54,4 @@ public function validate($value, Constraint $constraint) ->addViolation(); } } - - /** - * {@inheritdoc} - */ - public function getDefaultOption() - { - return 'canonicalize'; - } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php index ae418566accbd..7ebe1cdc8fa24 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php @@ -94,7 +94,7 @@ public function getInvalidLocales() /** * @dataProvider getUncanonicalizedLocales */ - public function testInvalidLocalesWithoutCanonicalization($locale) + public function testInvalidLocalesWithoutCanonicalization(string $locale) { $constraint = new Locale(array( 'message' => 'myMessage', @@ -111,7 +111,7 @@ public function testInvalidLocalesWithoutCanonicalization($locale) /** * @dataProvider getUncanonicalizedLocales */ - public function testValidLocalesWithCanonicalization($locale) + public function testValidLocalesWithCanonicalization(string $locale) { $constraint = new Locale(array( 'message' => 'myMessage', @@ -123,7 +123,7 @@ public function testValidLocalesWithCanonicalization($locale) $this->assertNoViolation(); } - public function getUncanonicalizedLocales() + public function getUncanonicalizedLocales(): iterable { return array( array('en-US'),