ソースを参照

[Validator] Implemented Language constraint

Bernhard Schussek 14 年 前
コミット
993257a83e

+ 17 - 0
src/Symfony/Component/Validator/Constraints/Language.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 Language extends \Symfony\Component\Validator\Constraint
+{
+    public $message = 'This value is not a valid language';
+}

+ 46 - 0
src/Symfony/Component/Validator/Constraints/LanguageValidator.php

@@ -0,0 +1,46 @@
+<?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;
+use Symfony\Component\Locale\Locale;
+
+/**
+ * Validates whether a value is a valid language code
+ *
+ * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
+ */
+class LanguageValidator extends ConstraintValidator
+{
+    public function isValid($value, Constraint $constraint)
+    {
+        if ($value === null || $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, Locale::getLanguages())) {
+            $this->setMessage($constraint->message, array('{{ value }}' => $value));
+
+            return false;
+        }
+
+        return true;
+    }
+}

+ 79 - 0
tests/Symfony/Tests/Component/Validator/Constraints/LanguageValidatorTest.php

@@ -0,0 +1,79 @@
+<?php
+
+namespace Symfony\Tests\Component\Validator;
+
+use Symfony\Component\Validator\Constraints\Language;
+use Symfony\Component\Validator\Constraints\LanguageValidator;
+
+class LanguageValidatorTest extends \PHPUnit_Framework_TestCase
+{
+    protected $validator;
+
+    protected function setUp()
+    {
+        $this->validator = new LanguageValidator();
+    }
+
+    public function testNullIsValid()
+    {
+        $this->assertTrue($this->validator->isValid(null, new Language()));
+    }
+
+    public function testEmptyStringIsValid()
+    {
+        $this->assertTrue($this->validator->isValid('', new Language()));
+    }
+
+    public function testExpectsStringCompatibleType()
+    {
+        $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
+
+        $this->validator->isValid(new \stdClass(), new Language());
+    }
+
+    /**
+     * @dataProvider getValidLanguages
+     */
+    public function testValidLanguages($date)
+    {
+        $this->assertTrue($this->validator->isValid($date, new Language()));
+    }
+
+    public function getValidLanguages()
+    {
+        return array(
+            array('en'),
+            array('en_US'),
+            array('my'),
+        );
+    }
+
+    /**
+     * @dataProvider getInvalidLanguages
+     */
+    public function testInvalidLanguages($date)
+    {
+        $this->assertFalse($this->validator->isValid($date, new Language()));
+    }
+
+    public function getInvalidLanguages()
+    {
+        return array(
+            array('EN'),
+            array('foobar'),
+        );
+    }
+
+    public function testMessageIsSet()
+    {
+        $constraint = new Language(array(
+            'message' => 'myMessage'
+        ));
+
+        $this->assertFalse($this->validator->isValid('foobar', $constraint));
+        $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
+        $this->assertEquals($this->validator->getMessageParameters(), array(
+            '{{ value }}' => 'foobar',
+        ));
+    }
+}