[Validator] Changed component to use the Intl component

This commit is contained in:
Bernhard Schussek 2013-03-15 12:44:20 +01:00
parent 0c1fe395e5
commit 01d0ee8b78
8 changed files with 22 additions and 30 deletions

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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'
));

View File

@ -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'
));

View File

@ -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'
));

View File

@ -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');
}
}

View File

@ -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.*"
},