vendor/symfony/src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\Config\Definition\Builder;
       
    13 
       
    14 use Symfony\Component\Config\Definition\NodeInterface;
       
    15 
       
    16 /**
       
    17  * This class provides a fluent interface for defining a node.
       
    18  *
       
    19  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
       
    20  */
       
    21 abstract class NodeDefinition implements NodeParentInterface
       
    22 {
       
    23     protected $name;
       
    24     protected $normalization;
       
    25     protected $validation;
       
    26     protected $defaultValue;
       
    27     protected $default;
       
    28     protected $required;
       
    29     protected $merge;
       
    30     protected $allowEmptyValue;
       
    31     protected $nullEquivalent;
       
    32     protected $trueEquivalent;
       
    33     protected $falseEquivalent;
       
    34     protected $parent;
       
    35 
       
    36     /**
       
    37      * Constructor
       
    38      *
       
    39      * @param string                $name   The name of the node
       
    40      * @param NodeParentInterface   $parent The parent
       
    41      */
       
    42     public function __construct($name, NodeParentInterface $parent = null)
       
    43     {
       
    44         $this->parent = $parent;
       
    45         $this->name = $name;
       
    46         $this->default = false;
       
    47         $this->required = false;
       
    48         $this->trueEquivalent = true;
       
    49         $this->falseEquivalent = false;
       
    50     }
       
    51 
       
    52     /**
       
    53      * Sets the parent node
       
    54      *
       
    55      * @param NodeParentInterface $parent The parent
       
    56      */
       
    57     public function setParent(NodeParentInterface $parent)
       
    58     {
       
    59         $this->parent = $parent;
       
    60 
       
    61         return $this;
       
    62     }
       
    63 
       
    64     /**
       
    65      * Returns the parent node.
       
    66      *
       
    67      * @return NodeParentInterface The builder of the parent node
       
    68      */
       
    69     public function end()
       
    70     {
       
    71         return $this->parent;
       
    72     }
       
    73 
       
    74     /**
       
    75      * Creates the node.
       
    76      *
       
    77      * @param Boolean $forceRootNode Whether to force this node as the root node
       
    78      *
       
    79      * @return NodeInterface
       
    80      */
       
    81     public function getNode($forceRootNode = false)
       
    82     {
       
    83         if ($forceRootNode)
       
    84         {
       
    85             $this->parent = null;
       
    86         }
       
    87 
       
    88         if (null !== $this->normalization) {
       
    89             $this->normalization->before = ExprBuilder::buildExpressions($this->normalization->before);
       
    90         }
       
    91 
       
    92         if (null !== $this->validation) {
       
    93             $this->validation->rules = ExprBuilder::buildExpressions($this->validation->rules);
       
    94         }
       
    95 
       
    96         return $this->createNode();
       
    97     }
       
    98 
       
    99     /**
       
   100      * Sets the default value.
       
   101      *
       
   102      * @param mixed $value The default value
       
   103      *
       
   104      * @return NodeDefinition
       
   105      */
       
   106     public function defaultValue($value)
       
   107     {
       
   108         $this->default = true;
       
   109         $this->defaultValue = $value;
       
   110 
       
   111         return $this;
       
   112     }
       
   113 
       
   114     /**
       
   115      * Sets the node as required.
       
   116      *
       
   117      * @return NodeDefinition
       
   118      */
       
   119     public function isRequired()
       
   120     {
       
   121         $this->required = true;
       
   122 
       
   123         return $this;
       
   124     }
       
   125 
       
   126     /**
       
   127      * Sets the equivalent value used when the node contains null.
       
   128      *
       
   129      * @param mixed $value
       
   130      *
       
   131      * @return NodeDefinition
       
   132      */
       
   133     public function treatNullLike($value)
       
   134     {
       
   135         $this->nullEquivalent = $value;
       
   136 
       
   137         return $this;
       
   138     }
       
   139 
       
   140     /**
       
   141      * Sets the equivalent value used when the node contains true.
       
   142      *
       
   143      * @param mixed $value
       
   144      *
       
   145      * @return NodeDefinition
       
   146      */
       
   147     public function treatTrueLike($value)
       
   148     {
       
   149         $this->trueEquivalent = $value;
       
   150 
       
   151         return $this;
       
   152     }
       
   153 
       
   154     /**
       
   155      * Sets the equivalent value used when the node contains false.
       
   156      *
       
   157      * @param mixed $value
       
   158      *
       
   159      * @return NodeDefinition
       
   160      */
       
   161     public function treatFalseLike($value)
       
   162     {
       
   163         $this->falseEquivalent = $value;
       
   164 
       
   165         return $this;
       
   166     }
       
   167 
       
   168     /**
       
   169      * Sets null as the default value.
       
   170      *
       
   171      * @return NodeDefinition
       
   172      */
       
   173     public function defaultNull()
       
   174     {
       
   175         return $this->defaultValue(null);
       
   176     }
       
   177 
       
   178     /**
       
   179      * Sets true as the default value.
       
   180      *
       
   181      * @return NodeDefinition
       
   182      */
       
   183     public function defaultTrue()
       
   184     {
       
   185         return $this->defaultValue(true);
       
   186     }
       
   187 
       
   188     /**
       
   189      * Sets false as the default value.
       
   190      *
       
   191      * @return NodeDefinition
       
   192      */
       
   193     public function defaultFalse()
       
   194     {
       
   195         return $this->defaultValue(false);
       
   196     }
       
   197 
       
   198     /**
       
   199      * Gets the builder for normalization rules.
       
   200      *
       
   201      * @return NormalizationBuilder
       
   202      */
       
   203     protected function normalization()
       
   204     {
       
   205         if (null === $this->normalization) {
       
   206             $this->normalization = new NormalizationBuilder($this);
       
   207         }
       
   208 
       
   209         return $this->normalization;
       
   210     }
       
   211 
       
   212     /**
       
   213      * Sets an expression to run before the normalization.
       
   214      *
       
   215      * @return ExprBuilder
       
   216      */
       
   217     public function beforeNormalization()
       
   218     {
       
   219         return $this->normalization()->before();
       
   220     }
       
   221 
       
   222     /**
       
   223      * Denies the node value being empty.
       
   224      *
       
   225      * @return NodeDefinition
       
   226      */
       
   227     public function cannotBeEmpty()
       
   228     {
       
   229         $this->allowEmptyValue = false;
       
   230 
       
   231         return $this;
       
   232     }
       
   233 
       
   234     /**
       
   235      * Gets the builder for validation rules.
       
   236      *
       
   237      * @return ValidationBuilder
       
   238      */
       
   239     protected function validation()
       
   240     {
       
   241         if (null === $this->validation) {
       
   242             $this->validation = new ValidationBuilder($this);
       
   243         }
       
   244 
       
   245         return $this->validation;
       
   246     }
       
   247 
       
   248     /**
       
   249      * Sets an expression to run for the validation.
       
   250      *
       
   251      * The expression receives the value of the node and must return it. It can
       
   252      * modify it.
       
   253      * An exception should be thrown when the node is not valid.
       
   254      *
       
   255      * @return ExprBuilder
       
   256      */
       
   257     public function validate()
       
   258     {
       
   259         return $this->validation()->rule();
       
   260     }
       
   261 
       
   262     /**
       
   263      * Gets the builder for merging rules.
       
   264      *
       
   265      * @return MergeBuilder
       
   266      */
       
   267     protected function merge()
       
   268     {
       
   269         if (null === $this->merge) {
       
   270             $this->merge = new MergeBuilder($this);
       
   271         }
       
   272 
       
   273         return $this->merge;
       
   274     }
       
   275 
       
   276     /**
       
   277      * Sets whether the node can be overwritten.
       
   278      *
       
   279      * @param Boolean $deny Whether the overwriting is forbidden or not
       
   280      *
       
   281      * @return NodeDefinition
       
   282      */
       
   283     public function cannotBeOverwritten($deny = true)
       
   284     {
       
   285         $this->merge()->denyOverwrite($deny);
       
   286 
       
   287         return $this;
       
   288     }
       
   289 
       
   290     /**
       
   291      * Instantiate and configure the node according to this definition
       
   292      *
       
   293      * @return NodeInterface $node The node instance
       
   294      */
       
   295     abstract protected function createNode();
       
   296 
       
   297 }