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 an array.
105 public function ifArray()
107 $this->ifPart = function ($v) { return is_array($v); };
113 * Tests if the value is in an array.
115 * @param array $array
119 public function ifInArray(array $array)
121 $this->ifPart = function ($v) use ($array) { return in_array($v, $array, true); };
127 * Tests if the value is not in an array.
129 * @param array $array
133 public function ifNotInArray(array $array)
135 $this->ifPart = function ($v) use ($array) { return !in_array($v, $array, true); };
141 * Sets the closure to run if the test pass.
143 * @param \Closure $closure
147 public function then(\Closure $closure)
149 $this->thenPart = $closure;
155 * Sets a closure returning an empty array.
159 public function thenEmptyArray()
161 $this->thenPart = function ($v) { return array(); };
167 * Sets a closure marking the value as invalid at validation time.
169 * if you want to add the value of the node in your message just use a %s placeholder.
171 * @param string $message
175 * @throws \InvalidArgumentException
177 public function thenInvalid($message)
179 $this->thenPart = function ($v) use ($message) {throw new \InvalidArgumentException(sprintf($message, json_encode($v))); };
185 * Sets a closure unsetting this key of the array at validation time.
189 * @throws UnsetKeyException
191 public function thenUnset()
193 $this->thenPart = function ($v) { throw new UnsetKeyException('Unsetting key'); };
199 * Returns the related node.
201 * @return NodeDefinition|ArrayNodeDefinition|VariableNodeDefinition
203 * @throws \RuntimeException
205 public function end()
207 if (null === $this->ifPart) {
208 throw new \RuntimeException('You must specify an if part.');
210 if (null === $this->thenPart) {
211 throw new \RuntimeException('You must specify a then part.');
218 * Builds the expressions.
220 * @param ExprBuilder[] $expressions An array of ExprBuilder instances to build
224 public static function buildExpressions(array $expressions)
226 foreach ($expressions as $k => $expr) {
227 if ($expr instanceof self) {
229 $then = $expr->thenPart;
230 $expressions[$k] = function ($v) use ($if, $then) {
231 return $if($v) ? $then($v) : $v;