Skip to content

Commit

Permalink
Added the support of the validation in the Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
stof authored and fabpot committed Feb 16, 2011
1 parent 89dbb04 commit 077d192
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
Expand Up @@ -6,6 +6,7 @@
* This class builds an if expression.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
* @author Christophe Coevoet <stof@notk.org>
*/
class ExprBuilder
{
Expand All @@ -23,6 +24,18 @@ public function __construct($parent)
$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.
*
Expand Down Expand Up @@ -78,6 +91,20 @@ public function ifArray()
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.
*
Expand All @@ -104,6 +131,32 @@ public function thenEmptyArray()
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
*
Expand Down
Expand Up @@ -19,6 +19,7 @@ class NodeBuilder
public $children;
public $prototype;
public $normalization;
public $validation;
public $merge;
public $finalization;
public $defaultValue;
Expand Down Expand Up @@ -393,6 +394,34 @@ public function performNoDeepMerging()
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.
*
Expand Down
Expand Up @@ -129,6 +129,12 @@ protected function configureScalarNode(ScalarNode $configNode, NodeBuilder $node
$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)
);
}
}

/**
Expand Down Expand Up @@ -185,6 +191,12 @@ protected function createArrayConfigNode(NodeBuilder $node)
$configNode->setDefaultValue($node->defaultValue);
}

if (null !== $node->validation) {
$configNode->setFinalValidationClosures(
$this->buildExpressions($node->validation->rules)
);
}

return $configNode;
}

Expand Down
@@ -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);
}
}

0 comments on commit 077d192

Please sign in to comment.