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>
31 * @param NodeDefinition $node The related node
33 public function __construct(NodeDefinition $node)
39 * Marks the expression as being always used.
41 * @param \Closure $then
45 public function always(\Closure $then = null)
47 $this->ifPart = function ($v) { return true; };
50 $this->thenPart = $then;
57 * Sets a closure to use as tests.
59 * The default one tests if the value is true.
61 * @param \Closure $closure
65 public function ifTrue(\Closure $closure = null)
67 if (null === $closure) {
68 $closure = function ($v) { return true === $v; };
71 $this->ifPart = $closure;
77 * Tests if the value is a string.
81 public function ifString()
83 $this->ifPart = function ($v) { return is_string($v); };
89 * Tests if the value is null.
93 public function ifNull()
95 $this->ifPart = function ($v) { return null === $v; };
101 * Tests if the value is empty.
103 * @return ExprBuilder
105 public function ifEmpty()
107 $this->ifPart = function ($v) { return empty($v); };
113 * Tests if the value is an array.
117 public function ifArray()
119 $this->ifPart = function ($v) { return is_array($v); };
125 * Tests if the value is in an array.
127 * @param array $array
131 public function ifInArray(array $array)
133 $this->ifPart = function ($v) use ($array) { return in_array($v, $array, true); };
139 * Tests if the value is not in an array.
141 * @param array $array
145 public function ifNotInArray(array $array)
147 $this->ifPart = function ($v) use ($array) { return !in_array($v, $array, true); };
153 * Sets the closure to run if the test pass.
155 * @param \Closure $closure
159 public function then(\Closure $closure)
161 $this->thenPart = $closure;
167 * Sets a closure returning an empty array.
171 public function thenEmptyArray()
173 $this->thenPart = function ($v) { return array(); };
179 * Sets a closure marking the value as invalid at validation time.
181 * if you want to add the value of the node in your message just use a %s placeholder.
183 * @param string $message
187 * @throws \InvalidArgumentException
189 public function thenInvalid($message)
191 $this->thenPart = function ($v) use ($message) {throw new \InvalidArgumentException(sprintf($message, json_encode($v))); };
197 * Sets a closure unsetting this key of the array at validation time.
201 * @throws UnsetKeyException
203 public function thenUnset()
205 $this->thenPart = function ($v) { throw new UnsetKeyException('Unsetting key'); };
211 * Returns the related node.
213 * @return NodeDefinition|ArrayNodeDefinition|VariableNodeDefinition
215 * @throws \RuntimeException
217 public function end()
219 if (null === $this->ifPart) {
220 throw new \RuntimeException('You must specify an if part.');
222 if (null === $this->thenPart) {
223 throw new \RuntimeException('You must specify a then part.');
230 * Builds the expressions.
232 * @param ExprBuilder[] $expressions An array of ExprBuilder instances to build
236 public static function buildExpressions(array $expressions)
238 foreach ($expressions as $k => $expr) {
239 if ($expr instanceof self) {
241 $then = $expr->thenPart;
242 $expressions[$k] = function ($v) use ($if, $then) {
243 return $if($v) ? $then($v) : $v;