4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Definition\Builder
;
14 use Symfony\Component\Config\Definition\Exception\UnsetKeyException
;
17 * This class builds an if expression.
19 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
20 * @author Christophe Coevoet <stof@notk.org>
28 public function __construct(NodeDefinition
$node)
34 * Marks the expression as being always used.
38 public function always(\Closure
$then = null)
40 $this->ifPart
= function ($v) { return true; };
43 $this->thenPart
= $then;
50 * Sets a closure to use as tests.
52 * The default one tests if the value is true.
56 public function ifTrue(\Closure
$closure = null)
58 if (null === $closure) {
59 $closure = function ($v) { return true === $v; };
62 $this->ifPart
= $closure;
68 * Tests if the value is a string.
72 public function ifString()
74 $this->ifPart
= function ($v) { return is_string($v); };
80 * Tests if the value is null.
84 public function ifNull()
86 $this->ifPart
= function ($v) { return null === $v; };
92 * Tests if the value is an array.
96 public function ifArray()
98 $this->ifPart
= function ($v) { return is_array($v); };
104 * Tests if the value is in an array.
108 public function ifInArray(array $array)
110 $this->ifPart
= function ($v) use ($array) { return in_array($v, $array, true); };
116 * Tests if the value is not in an array.
120 public function ifNotInArray(array $array)
122 $this->ifPart
= function ($v) use ($array) { return !in_array($v, $array, true); };
128 * Sets the closure to run if the test pass.
132 public function then(\Closure
$closure)
134 $this->thenPart
= $closure;
140 * Sets a closure returning an empty array.
144 public function thenEmptyArray()
146 $this->thenPart
= function ($v) { return array(); };
152 * Sets a closure marking the value as invalid at validation time.
154 * if you want to add the value of the node in your message just use a %s placeholder.
156 * @param string $message
160 * @throws \InvalidArgumentException
162 public function thenInvalid($message)
164 $this->thenPart
= function ($v) use ($message) { throw new \
InvalidArgumentException(sprintf($message, json_encode($v))); };
170 * Sets a closure unsetting this key of the array at validation time.
174 * @throws UnsetKeyException
176 public function thenUnset()
178 $this->thenPart
= function ($v) { throw new UnsetKeyException('Unsetting key'); };
184 * Returns the related node.
186 * @return NodeDefinition|ArrayNodeDefinition|VariableNodeDefinition
188 * @throws \RuntimeException
190 public function end()
192 if (null === $this->ifPart
) {
193 throw new \
RuntimeException('You must specify an if part.');
195 if (null === $this->thenPart
) {
196 throw new \
RuntimeException('You must specify a then part.');
203 * Builds the expressions.
205 * @param ExprBuilder[] $expressions An array of ExprBuilder instances to build
209 public static function buildExpressions(array $expressions)
211 foreach ($expressions as $k => $expr) {
212 if ($expr instanceof self
) {
214 $then = $expr->thenPart
;
215 $expressions[$k] = function ($v) use ($if, $then) {
216 return $if($v) ?
$then($v) : $v;