composer package updates
[openemr.git] / vendor / symfony / config / Definition / Builder / ExprBuilder.php
blobf83f1c209b03edab85eaf0897943ac3b4af111b0
1 <?php
3 /*
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;
16 /**
17 * This class builds an if expression.
19 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
20 * @author Christophe Coevoet <stof@notk.org>
22 class ExprBuilder
24 protected $node;
25 public $ifPart;
26 public $thenPart;
28 public function __construct(NodeDefinition $node)
30 $this->node = $node;
33 /**
34 * Marks the expression as being always used.
36 * @return $this
38 public function always(\Closure $then = null)
40 $this->ifPart = function ($v) { return true; };
42 if (null !== $then) {
43 $this->thenPart = $then;
46 return $this;
49 /**
50 * Sets a closure to use as tests.
52 * The default one tests if the value is true.
54 * @return $this
56 public function ifTrue(\Closure $closure = null)
58 if (null === $closure) {
59 $closure = function ($v) { return true === $v; };
62 $this->ifPart = $closure;
64 return $this;
67 /**
68 * Tests if the value is a string.
70 * @return $this
72 public function ifString()
74 $this->ifPart = function ($v) { return is_string($v); };
76 return $this;
79 /**
80 * Tests if the value is null.
82 * @return $this
84 public function ifNull()
86 $this->ifPart = function ($v) { return null === $v; };
88 return $this;
91 /**
92 * Tests if the value is an array.
94 * @return $this
96 public function ifArray()
98 $this->ifPart = function ($v) { return is_array($v); };
100 return $this;
104 * Tests if the value is in an array.
106 * @return $this
108 public function ifInArray(array $array)
110 $this->ifPart = function ($v) use ($array) { return in_array($v, $array, true); };
112 return $this;
116 * Tests if the value is not in an array.
118 * @return $this
120 public function ifNotInArray(array $array)
122 $this->ifPart = function ($v) use ($array) { return !in_array($v, $array, true); };
124 return $this;
128 * Sets the closure to run if the test pass.
130 * @return $this
132 public function then(\Closure $closure)
134 $this->thenPart = $closure;
136 return $this;
140 * Sets a closure returning an empty array.
142 * @return $this
144 public function thenEmptyArray()
146 $this->thenPart = function ($v) { return array(); };
148 return $this;
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
158 * @return $this
160 * @throws \InvalidArgumentException
162 public function thenInvalid($message)
164 $this->thenPart = function ($v) use ($message) { throw new \InvalidArgumentException(sprintf($message, json_encode($v))); };
166 return $this;
170 * Sets a closure unsetting this key of the array at validation time.
172 * @return $this
174 * @throws UnsetKeyException
176 public function thenUnset()
178 $this->thenPart = function ($v) { throw new UnsetKeyException('Unsetting key'); };
180 return $this;
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.');
199 return $this->node;
203 * Builds the expressions.
205 * @param ExprBuilder[] $expressions An array of ExprBuilder instances to build
207 * @return array
209 public static function buildExpressions(array $expressions)
211 foreach ($expressions as $k => $expr) {
212 if ($expr instanceof self) {
213 $if = $expr->ifPart;
214 $then = $expr->thenPart;
215 $expressions[$k] = function ($v) use ($if, $then) {
216 return $if($v) ? $then($v) : $v;
221 return $expressions;