Procházet zdrojové kódy

[Validator] Custom built constraints can now be used in the loaders

Bernhard Schussek před 15 roky
rodič
revize
a747987625

+ 21 - 0
src/Symfony/Components/Validator/Mapping/Loader/FileLoader.php

@@ -22,4 +22,25 @@ abstract class FileLoader implements LoaderInterface
 
         $this->file = $file;
     }
+
+    /**
+     * Creates a new constraint instance for the given constraint name
+     *
+     * @param string $name    The constraint name. Either a constraint relative
+     *                        to the default constraint namespace, or a fully
+     *                        qualified class name
+     * @param array $options  The constraint options
+     *
+     * @return Constraint
+     */
+    protected function newConstraint($name, $options)
+    {
+        if (strpos($name, '\\') !== false && class_exists($name)) {
+            $className = (string)$name;
+        } else {
+            $className = 'Symfony\\Components\\Validator\\Constraints\\'.$name;
+        }
+
+        return new $className($options);
+    }
 }

+ 1 - 3
src/Symfony/Components/Validator/Mapping/Loader/XmlFileLoader.php

@@ -63,8 +63,6 @@ class XmlFileLoader extends FileLoader
         $constraints = array();
 
         foreach ($nodes as $node) {
-            $className = 'Symfony\\Components\\Validator\\Constraints\\'.$node['name'];
-
             if (count($node) > 0) {
                 if (count($node->value) > 0) {
                     $options = $this->parseValues($node->value);
@@ -81,7 +79,7 @@ class XmlFileLoader extends FileLoader
                 $options = null;
             }
 
-            $constraints[] = new $className($options);
+            $constraints[] = $this->newConstraint($node['name'], $options);
         }
 
         return $constraints;

+ 1 - 2
src/Symfony/Components/Validator/Mapping/Loader/YamlFileLoader.php

@@ -68,14 +68,13 @@ class YamlFileLoader extends FileLoader
 
         foreach ($nodes as $name => $childNodes) {
             if (is_numeric($name) && is_array($childNodes) && count($childNodes) == 1) {
-                $className = 'Symfony\\Components\\Validator\\Constraints\\'.key($childNodes);
                 $options = current($childNodes);
 
                 if (is_array($options)) {
                     $options = $this->parseNodes($options);
                 }
 
-                $values[] = new $className($options);
+                $values[] = $this->newConstraint(key($childNodes), $options);
             } else {
                 if (is_array($childNodes)) {
                     $childNodes = $this->parseNodes($childNodes);

+ 1 - 0
tests/Symfony/Tests/Components/Validator/Fixtures/Entity.php

@@ -8,6 +8,7 @@ require_once __DIR__.'/EntityInterface.php';
 /**
  * @Validation({
  *   @NotNull,
+ *   @Symfony\Tests\Components\Validator\Fixtures\ConstraintA,
  *   @Min(3),
  *   @Choice({"A", "B"}),
  *   @All({@NotNull, @Min(3)}),

+ 3 - 0
tests/Symfony/Tests/Components/Validator/Mapping/Loader/AnnotationLoaderTest.php

@@ -3,6 +3,7 @@
 namespace Symfony\Tests\Components\Validator\Mapping\Loader;
 
 require_once __DIR__.'/../../Fixtures/Entity.php';
+require_once __DIR__.'/../../Fixtures/ConstraintA.php';
 
 use Symfony\Components\Validator\Constraints\All;
 use Symfony\Components\Validator\Constraints\Collection;
@@ -11,6 +12,7 @@ use Symfony\Components\Validator\Constraints\Min;
 use Symfony\Components\Validator\Constraints\Choice;
 use Symfony\Components\Validator\Mapping\ClassMetadata;
 use Symfony\Components\Validator\Mapping\Loader\AnnotationLoader;
+use Symfony\Tests\Components\Validator\Fixtures\ConstraintA;
 
 class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
 {
@@ -39,6 +41,7 @@ class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
 
         $expected = new ClassMetadata('Symfony\Tests\Components\Validator\Fixtures\Entity');
         $expected->addConstraint(new NotNull());
+        $expected->addConstraint(new ConstraintA());
         $expected->addConstraint(new Min(3));
         $expected->addConstraint(new Choice(array('A', 'B')));
         $expected->addConstraint(new All(array(new NotNull(), new Min(3))));

+ 3 - 0
tests/Symfony/Tests/Components/Validator/Mapping/Loader/XmlFileLoaderTest.php

@@ -3,6 +3,7 @@
 namespace Symfony\Tests\Components\Validator\Mapping\Loader;
 
 require_once __DIR__.'/../../Fixtures/Entity.php';
+require_once __DIR__.'/../../Fixtures/ConstraintA.php';
 
 use Symfony\Components\Validator\Constraints\All;
 use Symfony\Components\Validator\Constraints\Collection;
@@ -11,6 +12,7 @@ use Symfony\Components\Validator\Constraints\Min;
 use Symfony\Components\Validator\Constraints\Choice;
 use Symfony\Components\Validator\Mapping\ClassMetadata;
 use Symfony\Components\Validator\Mapping\Loader\XmlFileLoader;
+use Symfony\Tests\Components\Validator\Fixtures\ConstraintA;
 
 class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
 {
@@ -39,6 +41,7 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
 
         $expected = new ClassMetadata('Symfony\Tests\Components\Validator\Fixtures\Entity');
         $expected->addConstraint(new NotNull());
+        $expected->addConstraint(new ConstraintA());
         $expected->addConstraint(new Min(3));
         $expected->addConstraint(new Choice(array('A', 'B')));
         $expected->addConstraint(new All(array(new NotNull(), new Min(3))));

+ 3 - 0
tests/Symfony/Tests/Components/Validator/Mapping/Loader/YamlFileLoaderTest.php

@@ -3,6 +3,7 @@
 namespace Symfony\Tests\Components\Validator\Mapping\Loader;
 
 require_once __DIR__.'/../../Fixtures/Entity.php';
+require_once __DIR__.'/../../Fixtures/ConstraintA.php';
 
 use Symfony\Components\Validator\Constraints\All;
 use Symfony\Components\Validator\Constraints\Collection;
@@ -11,6 +12,7 @@ use Symfony\Components\Validator\Constraints\Min;
 use Symfony\Components\Validator\Constraints\Choice;
 use Symfony\Components\Validator\Mapping\ClassMetadata;
 use Symfony\Components\Validator\Mapping\Loader\YamlFileLoader;
+use Symfony\Tests\Components\Validator\Fixtures\ConstraintA;
 
 class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
 {
@@ -39,6 +41,7 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
 
         $expected = new ClassMetadata('Symfony\Tests\Components\Validator\Fixtures\Entity');
         $expected->addConstraint(new NotNull());
+        $expected->addConstraint(new ConstraintA());
         $expected->addConstraint(new Min(3));
         $expected->addConstraint(new Choice(array('A', 'B')));
         $expected->addConstraint(new All(array(new NotNull(), new Min(3))));

+ 3 - 0
tests/Symfony/Tests/Components/Validator/Mapping/Loader/constraint-mapping.xml

@@ -10,6 +10,9 @@
 
     <!-- Constraint without value -->
     <constraint name="NotNull" />
+
+    <!-- Custom constraint -->
+    <constraint name="Symfony\Tests\Components\Validator\Fixtures\ConstraintA" />
     
     <!-- Constraint with single value -->
     <constraint name="Min">3</constraint>

+ 2 - 0
tests/Symfony/Tests/Components/Validator/Mapping/Loader/constraint-mapping.yml

@@ -2,6 +2,8 @@ Symfony\Tests\Components\Validator\Fixtures\Entity:
   constraints:
     # Constraint without value
     - NotNull: ~
+    # Custom constraint
+    - Symfony\Tests\Components\Validator\Fixtures\ConstraintA: ~
     # Constraint with single value
     - Min: 3
     # Constraint with multiple values