فهرست منبع

Added the support of the validation in the Builder

Christophe Coevoet 14 سال پیش
والد
کامیت
077d1921b3

+ 53 - 0
src/Symfony/Component/DependencyInjection/Configuration/Builder/ExprBuilder.php

@@ -6,6 +6,7 @@ namespace Symfony\Component\DependencyInjection\Configuration\Builder;
  * This class builds an if expression.
  *
  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ * @author Christophe Coevoet <stof@notk.org>
  */
 class ExprBuilder
 {
@@ -23,6 +24,18 @@ class ExprBuilder
         $this->parent = $parent;
     }
 
+    /**
+     * Mark the expression as being always used.
+     *
+     * @return Symfony\Component\DependencyInjection\Configuration\Builder\ExprBuilder
+     */
+    public function always()
+    {
+        $this->ifPart = function($v) { return true; };
+
+        return $this;
+    }
+
     /**
      * Sets a closure to use as tests.
      *
@@ -78,6 +91,20 @@ class ExprBuilder
         return $this;
     }
 
+    /**
+     * Tests if the value is in an array.
+     *
+     * @param array $array
+     *
+     * @return Symfony\Component\DependencyInjection\Configuration\Builder\ExprBuilder
+     */
+    public function ifInArray(array $array)
+    {
+        $this->ifPart = function($v) use ($array) { return in_array($v, $array, true); };
+
+        return $this;
+    }
+
     /**
      * Sets the closure to run if the test pass.
      *
@@ -104,6 +131,32 @@ class ExprBuilder
         return $this;
     }
 
+    /**
+     * Sets a closure marking the value as invalid at validation time.
+     *
+     * @param string $message
+     *
+     * @return Symfony\Component\DependencyInjection\Configuration\Builder\ExprBuilder
+     */
+    public function thenInvalid($message)
+    {
+        $this->thenPart = function ($v) use ($message) {throw new \InvalidArgumentException($message); };
+
+        return $this;
+    }
+
+    /**
+     * Sets a closure unsetting this key of the array at validation time.
+     *
+     * @return Symfony\Component\DependencyInjection\Configuration\Builder\ExprBuilder
+     */
+    public function thenUnset()
+    {
+        $this->thenPart = function ($v) { throw new UnsetKeyException('Unsetting key'); };
+
+        return $this;
+    }
+
     /**
      * Returns the parent node
      *

+ 29 - 0
src/Symfony/Component/DependencyInjection/Configuration/Builder/NodeBuilder.php

@@ -19,6 +19,7 @@ class NodeBuilder
     public $children;
     public $prototype;
     public $normalization;
+    public $validation;
     public $merge;
     public $finalization;
     public $defaultValue;
@@ -393,6 +394,34 @@ class NodeBuilder
         return $this;
     }
 
+    /**
+     * Gets the builder for validation rules.
+     *
+     * @return Symfony\Component\DependencyInjection\Configuration\Builder\ValidationBuilder
+     */
+    protected function validation()
+    {
+        if (null === $this->validation) {
+            $this->validation = new ValidationBuilder($this);
+        }
+
+        return $this->validation;
+    }
+
+    /**
+     * Sets an expression to run for the validation.
+     *
+     * The expression receives the value of the node and must return it. It can
+     * modify it.
+     * An exception should be thrown when the node is not valid.
+     *
+     * @return Symfony\Component\DependencyInjection\Configuration\Builder\ExprBuilder
+     */
+    public function validate()
+    {
+        return $this->validation()->rule();
+    }
+
     /**
      * Returns the parent node.
      *

+ 12 - 0
src/Symfony/Component/DependencyInjection/Configuration/Builder/TreeBuilder.php

@@ -129,6 +129,12 @@ class TreeBuilder
         $configNode->addEquivalentValue(true, $node->trueEquivalent);
         $configNode->addEquivalentValue(false, $node->falseEquivalent);
         $configNode->setRequired($node->required);
+
+        if (null !== $node->validation) {
+            $configNode->setFinalValidationClosures(
+                $this->buildExpressions($node->validation->rules)
+            );
+        }
     }
 
     /**
@@ -185,6 +191,12 @@ class TreeBuilder
             $configNode->setDefaultValue($node->defaultValue);
         }
 
+        if (null !== $node->validation) {
+            $configNode->setFinalValidationClosures(
+                $this->buildExpressions($node->validation->rules)
+            );
+        }
+
         return $configNode;
     }
 

+ 44 - 0
src/Symfony/Component/DependencyInjection/Configuration/Builder/ValidationBuilder.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace Symfony\Component\DependencyInjection\Configuration\Builder;
+
+/**
+ * This class builds validation conditions.
+ *
+ * @author Christophe Coevoet <stof@notk.org>
+ */
+class ValidationBuilder
+{
+    public $parent;
+    public $rules;
+
+    /**
+     * Constructor
+     *
+     * @param Symfony\Component\DependencyInjection\Configuration\Builder\NodeBuilder $parent
+     */
+    public function __construct($parent)
+    {
+        $this->parent = $parent;
+
+        $this->rules = array();
+    }
+
+    /**
+     * Registers a closure to run as normalization or an expression builder to build it if null is provided.
+     *
+     * @param \Closure $closure
+     *
+     * @return Symfony\Component\DependencyInjection\Configuration\Builder\ExprBuilder|Symfony\Component\DependencyInjection\Configuration\Builder\ValidationBuilder
+     */
+    public function rule(\Closure $closure = null)
+    {
+        if (null !== $closure) {
+            $this->rules[] = $closure;
+
+            return $this;
+        }
+
+        return $this->rules[] = new ExprBuilder($this->parent);
+    }
+}