From 01d0ee8b78fed24d53ffb507ba039eec31a33f90 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Fri, 15 Mar 2013 12:44:20 +0100 Subject: [PATCH] [Validator] Changed component to use the Intl component --- .../Validator/Constraints/CountryValidator.php | 3 ++- .../Validator/Constraints/LanguageValidator.php | 3 ++- .../Validator/Constraints/LocaleValidator.php | 4 +++- .../Tests/Constraints/CountryValidatorTest.php | 8 -------- .../Tests/Constraints/LanguageValidatorTest.php | 8 -------- .../Tests/Constraints/LocaleValidatorTest.php | 8 -------- .../Tests/Constraints/LocalizedTestCase.php | 14 +++++++++++++- src/Symfony/Component/Validator/composer.json | 4 ++-- 8 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/CountryValidator.php b/src/Symfony/Component/Validator/Constraints/CountryValidator.php index 1d49954964..3c93d374ed 100644 --- a/src/Symfony/Component/Validator/Constraints/CountryValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CountryValidator.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Intl\Intl; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; @@ -39,7 +40,7 @@ class CountryValidator extends ConstraintValidator $value = (string) $value; - if (!in_array($value, \Symfony\Component\Locale\Locale::getCountries())) { + if (!in_array($value, array_keys(Intl::getRegionBundle()->getCountryNames(\Locale::getDefault())))) { $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); } } diff --git a/src/Symfony/Component/Validator/Constraints/LanguageValidator.php b/src/Symfony/Component/Validator/Constraints/LanguageValidator.php index 5aed18a84f..b90bd4fcd7 100644 --- a/src/Symfony/Component/Validator/Constraints/LanguageValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LanguageValidator.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Intl\Intl; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; @@ -39,7 +40,7 @@ class LanguageValidator extends ConstraintValidator $value = (string) $value; - if (!in_array($value, \Symfony\Component\Locale\Locale::getLanguages())) { + if (!in_array($value, array_keys(Intl::getLanguageBundle()->getLanguageNames(\Locale::getDefault())))) { $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); } } diff --git a/src/Symfony/Component/Validator/Constraints/LocaleValidator.php b/src/Symfony/Component/Validator/Constraints/LocaleValidator.php index 5cacdb4d19..3182e64441 100644 --- a/src/Symfony/Component/Validator/Constraints/LocaleValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LocaleValidator.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Intl\Intl; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; @@ -38,8 +39,9 @@ class LocaleValidator extends ConstraintValidator } $value = (string) $value; + $locales = Intl::getLocaleBundle()->getLocaleNames(\Locale::getDefault()); - if (!in_array($value, \Symfony\Component\Locale\Locale::getLocales())) { + if (!isset($locales[$value])) { $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php index ec617447ad..2ff89aa718 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php @@ -63,10 +63,6 @@ class CountryValidatorTest extends LocalizedTestCase */ public function testValidCountries($country) { - if (!class_exists('Symfony\Component\Locale\Locale')) { - $this->markTestSkipped('The "Locale" component is not available'); - } - $this->context->expects($this->never()) ->method('addViolation'); @@ -87,10 +83,6 @@ class CountryValidatorTest extends LocalizedTestCase */ public function testInvalidCountries($country) { - if (!class_exists('Symfony\Component\Locale\Locale')) { - $this->markTestSkipped('The "Locale" component is not available'); - } - $constraint = new Country(array( 'message' => 'myMessage' )); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php index 49151d6302..bd36da0bb1 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php @@ -63,10 +63,6 @@ class LanguageValidatorTest extends LocalizedTestCase */ public function testValidLanguages($language) { - if (!class_exists('Symfony\Component\Locale\Locale')) { - $this->markTestSkipped('The "Locale" component is not available'); - } - $this->context->expects($this->never()) ->method('addViolation'); @@ -87,10 +83,6 @@ class LanguageValidatorTest extends LocalizedTestCase */ public function testInvalidLanguages($language) { - if (!class_exists('Symfony\Component\Locale\Locale')) { - $this->markTestSkipped('The "Locale" component is not available'); - } - $constraint = new Language(array( 'message' => 'myMessage' )); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php index 167c49da16..0a4285a504 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php @@ -63,10 +63,6 @@ class LocaleValidatorTest extends LocalizedTestCase */ public function testValidLocales($locale) { - if (!class_exists('Symfony\Component\Locale\Locale')) { - $this->markTestSkipped('The "Locale" component is not available'); - } - $this->context->expects($this->never()) ->method('addViolation'); @@ -89,10 +85,6 @@ class LocaleValidatorTest extends LocalizedTestCase */ public function testInvalidLocales($locale) { - if (!class_exists('Symfony\Component\Locale\Locale')) { - $this->markTestSkipped('The "Locale" component is not available'); - } - $constraint = new Locale(array( 'message' => 'myMessage' )); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LocalizedTestCase.php b/src/Symfony/Component/Validator/Tests/Constraints/LocalizedTestCase.php index a71c0890e3..b6fe3f58c2 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LocalizedTestCase.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LocalizedTestCase.php @@ -11,12 +11,24 @@ namespace Symfony\Component\Validator\Tests\Constraints; +use Symfony\Component\Intl\Intl; + abstract class LocalizedTestCase extends \PHPUnit_Framework_TestCase { protected function setUp() { - if (!extension_loaded('intl')) { + parent::setUp(); + + if (!class_exists('Symfony\Component\Intl\Intl')) { + $this->markTestSkipped('The "Intl" component is not available'); + } + + if (!Intl::isExtensionLoaded()) { $this->markTestSkipped('The "intl" extension is not available'); } + + Intl::setDataSource(Intl::STUB); + + \Locale::setDefault('en'); } } diff --git a/src/Symfony/Component/Validator/composer.json b/src/Symfony/Component/Validator/composer.json index 4355e21418..65cd3f93a3 100644 --- a/src/Symfony/Component/Validator/composer.json +++ b/src/Symfony/Component/Validator/composer.json @@ -21,14 +21,14 @@ }, "require-dev": { "symfony/http-foundation": "~2.1", - "symfony/locale": "~2.0", + "symfony/intl": "~2.3", "symfony/yaml": "~2.0", "symfony/config": ">=2.2,<2.4-dev" }, "suggest": { "doctrine/common": "~2.2", "symfony/http-foundation": "2.2.*", - "symfony/locale": "2.2.*", + "symfony/intl": "2.3.*", "symfony/yaml": "2.2.*", "symfony/config": "2.2.*" },