Преглед изворни кода

[Validator] Implemented Locale constraint

Bernhard Schussek пре 14 година
родитељ
комит
52ecffe51b

+ 1 - 2
src/Symfony/Component/Validator/Constraints/CountryValidator.php

@@ -14,7 +14,6 @@ namespace Symfony\Component\Validator\Constraints;
 use Symfony\Component\Validator\Constraint;
 use Symfony\Component\Validator\ConstraintValidator;
 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
-use Symfony\Component\Locale\Locale;
 
 /**
  * Validates whether a value is a valid country code
@@ -35,7 +34,7 @@ class CountryValidator extends ConstraintValidator
 
         $value = (string)$value;
 
-        if (!in_array($value, Locale::getCountries())) {
+        if (!in_array($value, \Symfony\Component\Locale\Locale::getCountries())) {
             $this->setMessage($constraint->message, array('{{ value }}' => $value));
 
             return false;

+ 1 - 2
src/Symfony/Component/Validator/Constraints/LanguageValidator.php

@@ -14,7 +14,6 @@ namespace Symfony\Component\Validator\Constraints;
 use Symfony\Component\Validator\Constraint;
 use Symfony\Component\Validator\ConstraintValidator;
 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
-use Symfony\Component\Locale\Locale;
 
 /**
  * Validates whether a value is a valid language code
@@ -35,7 +34,7 @@ class LanguageValidator extends ConstraintValidator
 
         $value = (string)$value;
 
-        if (!in_array($value, Locale::getLanguages())) {
+        if (!in_array($value, \Symfony\Component\Locale\Locale::getLanguages())) {
             $this->setMessage($constraint->message, array('{{ value }}' => $value));
 
             return false;

+ 17 - 0
src/Symfony/Component/Validator/Constraints/Locale.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace Symfony\Component\Validator\Constraints;
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+class Locale extends \Symfony\Component\Validator\Constraint
+{
+    public $message = 'This value is not a valid locale';
+}

+ 45 - 0
src/Symfony/Component/Validator/Constraints/LocaleValidator.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace Symfony\Component\Validator\Constraints;
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+use Symfony\Component\Validator\Constraint;
+use Symfony\Component\Validator\ConstraintValidator;
+use Symfony\Component\Validator\Exception\UnexpectedTypeException;
+
+/**
+ * Validates whether a value is a valid locale code
+ *
+ * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
+ */
+class LocaleValidator extends ConstraintValidator
+{
+    public function isValid($value, Constraint $constraint)
+    {
+        if (null === $value || '' === $value) {
+            return true;
+        }
+
+        if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString()'))) {
+            throw new UnexpectedTypeException($value, 'string');
+        }
+
+        $value = (string)$value;
+
+        if (!in_array($value, \Symfony\Component\Locale\Locale::getLocales())) {
+            $this->setMessage($constraint->message, array('{{ value }}' => $value));
+
+            return false;
+        }
+
+        return true;
+    }
+}

+ 80 - 0
tests/Symfony/Tests/Component/Validator/Constraints/LocaleValidatorTest.php

@@ -0,0 +1,80 @@
+<?php
+
+namespace Symfony\Tests\Component\Validator;
+
+use Symfony\Component\Validator\Constraints\Locale;
+use Symfony\Component\Validator\Constraints\LocaleValidator;
+
+class LocaleValidatorTest extends \PHPUnit_Framework_TestCase
+{
+    protected $validator;
+
+    protected function setUp()
+    {
+        $this->validator = new LocaleValidator();
+    }
+
+    public function testNullIsValid()
+    {
+        $this->assertTrue($this->validator->isValid(null, new Locale()));
+    }
+
+    public function testEmptyStringIsValid()
+    {
+        $this->assertTrue($this->validator->isValid('', new Locale()));
+    }
+
+    public function testExpectsStringCompatibleType()
+    {
+        $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
+
+        $this->validator->isValid(new \stdClass(), new Locale());
+    }
+
+    /**
+     * @dataProvider getValidLocales
+     */
+    public function testValidLocales($date)
+    {
+        $this->assertTrue($this->validator->isValid($date, new Locale()));
+    }
+
+    public function getValidLocales()
+    {
+        return array(
+            array('en'),
+            array('en_US'),
+            array('my'),
+            array('zh_Hans'),
+        );
+    }
+
+    /**
+     * @dataProvider getInvalidLocales
+     */
+    public function testInvalidLocales($date)
+    {
+        $this->assertFalse($this->validator->isValid($date, new Locale()));
+    }
+
+    public function getInvalidLocales()
+    {
+        return array(
+            array('EN'),
+            array('foobar'),
+        );
+    }
+
+    public function testMessageIsSet()
+    {
+        $constraint = new Locale(array(
+            'message' => 'myMessage'
+        ));
+
+        $this->assertFalse($this->validator->isValid('foobar', $constraint));
+        $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
+        $this->assertEquals($this->validator->getMessageParameters(), array(
+            '{{ value }}' => 'foobar',
+        ));
+    }
+}