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 empty.
96 public function ifEmpty()
98 $this->ifPart = function ($v) { return empty($v); };
104 * Tests if the value is an array.
108 public function ifArray()
110 $this->ifPart = function ($v) { return \is_array($v); };
116 * Tests if the value is in an array.
120 public function ifInArray(array $array)
122 $this->ifPart = function ($v) use ($array) { return \in_array($v, $array, true); };
128 * Tests if the value is not in an array.
132 public function ifNotInArray(array $array)
134 $this->ifPart = function ($v) use ($array) { return !\in_array($v, $array, true); };
140 * Transforms variables of any type into an array.
144 public function castToArray()
146 $this->ifPart = function ($v) { return !\is_array($v); };
147 $this->thenPart = function ($v) { return array($v); };
153 * Sets the closure to run if the test pass.
157 public function then(\Closure $closure)
159 $this->thenPart = $closure;
165 * Sets a closure returning an empty array.
169 public function thenEmptyArray()
171 $this->thenPart = function ($v) { return array(); };
177 * Sets a closure marking the value as invalid at validation time.
179 * if you want to add the value of the node in your message just use a %s placeholder.
181 * @param string $message
185 * @throws \InvalidArgumentException
187 public function thenInvalid($message)
189 $this->thenPart = function ($v) use ($message) { throw new \InvalidArgumentException(sprintf($message, json_encode($v))); };
195 * Sets a closure unsetting this key of the array at validation time.
199 * @throws UnsetKeyException
201 public function thenUnset()
203 $this->thenPart = function ($v) { throw new UnsetKeyException('Unsetting key'); };
209 * Returns the related node.
211 * @return NodeDefinition|ArrayNodeDefinition|VariableNodeDefinition
213 * @throws \RuntimeException
215 public function end()
217 if (null === $this->ifPart) {
218 throw new \RuntimeException('You must specify an if part.');
220 if (null === $this->thenPart) {
221 throw new \RuntimeException('You must specify a then part.');
228 * Builds the expressions.
230 * @param ExprBuilder[] $expressions An array of ExprBuilder instances to build
234 public static function buildExpressions(array $expressions)
236 foreach ($expressions as $k => $expr) {
237 if ($expr instanceof self) {
239 $then = $expr->thenPart;
240 $expressions[$k] = function ($v) use ($if, $then) {
241 return $if($v) ? $then($v) : $v;