Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / config / Definition / Builder / ExprBuilder.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Config\Definition\Builder;
13
14 use Symfony\Component\Config\Definition\Exception\UnsetKeyException;
15
16 /**
17  * This class builds an if expression.
18  *
19  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
20  * @author Christophe Coevoet <stof@notk.org>
21  */
22 class ExprBuilder
23 {
24     protected $node;
25     public $ifPart;
26     public $thenPart;
27
28     /**
29      * Constructor.
30      *
31      * @param NodeDefinition $node The related node
32      */
33     public function __construct(NodeDefinition $node)
34     {
35         $this->node = $node;
36     }
37
38     /**
39      * Marks the expression as being always used.
40      *
41      * @param \Closure $then
42      *
43      * @return $this
44      */
45     public function always(\Closure $then = null)
46     {
47         $this->ifPart = function ($v) { return true; };
48
49         if (null !== $then) {
50             $this->thenPart = $then;
51         }
52
53         return $this;
54     }
55
56     /**
57      * Sets a closure to use as tests.
58      *
59      * The default one tests if the value is true.
60      *
61      * @param \Closure $closure
62      *
63      * @return $this
64      */
65     public function ifTrue(\Closure $closure = null)
66     {
67         if (null === $closure) {
68             $closure = function ($v) { return true === $v; };
69         }
70
71         $this->ifPart = $closure;
72
73         return $this;
74     }
75
76     /**
77      * Tests if the value is a string.
78      *
79      * @return $this
80      */
81     public function ifString()
82     {
83         $this->ifPart = function ($v) { return is_string($v); };
84
85         return $this;
86     }
87
88     /**
89      * Tests if the value is null.
90      *
91      * @return $this
92      */
93     public function ifNull()
94     {
95         $this->ifPart = function ($v) { return null === $v; };
96
97         return $this;
98     }
99
100     /**
101      * Tests if the value is empty.
102      *
103      * @return ExprBuilder
104      */
105     public function ifEmpty()
106     {
107         $this->ifPart = function ($v) { return empty($v); };
108
109         return $this;
110     }
111
112     /**
113      * Tests if the value is an array.
114      *
115      * @return $this
116      */
117     public function ifArray()
118     {
119         $this->ifPart = function ($v) { return is_array($v); };
120
121         return $this;
122     }
123
124     /**
125      * Tests if the value is in an array.
126      *
127      * @param array $array
128      *
129      * @return $this
130      */
131     public function ifInArray(array $array)
132     {
133         $this->ifPart = function ($v) use ($array) { return in_array($v, $array, true); };
134
135         return $this;
136     }
137
138     /**
139      * Tests if the value is not in an array.
140      *
141      * @param array $array
142      *
143      * @return $this
144      */
145     public function ifNotInArray(array $array)
146     {
147         $this->ifPart = function ($v) use ($array) { return !in_array($v, $array, true); };
148
149         return $this;
150     }
151
152     /**
153      * Sets the closure to run if the test pass.
154      *
155      * @param \Closure $closure
156      *
157      * @return $this
158      */
159     public function then(\Closure $closure)
160     {
161         $this->thenPart = $closure;
162
163         return $this;
164     }
165
166     /**
167      * Sets a closure returning an empty array.
168      *
169      * @return $this
170      */
171     public function thenEmptyArray()
172     {
173         $this->thenPart = function ($v) { return array(); };
174
175         return $this;
176     }
177
178     /**
179      * Sets a closure marking the value as invalid at validation time.
180      *
181      * if you want to add the value of the node in your message just use a %s placeholder.
182      *
183      * @param string $message
184      *
185      * @return $this
186      *
187      * @throws \InvalidArgumentException
188      */
189     public function thenInvalid($message)
190     {
191         $this->thenPart = function ($v) use ($message) {throw new \InvalidArgumentException(sprintf($message, json_encode($v))); };
192
193         return $this;
194     }
195
196     /**
197      * Sets a closure unsetting this key of the array at validation time.
198      *
199      * @return $this
200      *
201      * @throws UnsetKeyException
202      */
203     public function thenUnset()
204     {
205         $this->thenPart = function ($v) { throw new UnsetKeyException('Unsetting key'); };
206
207         return $this;
208     }
209
210     /**
211      * Returns the related node.
212      *
213      * @return NodeDefinition|ArrayNodeDefinition|VariableNodeDefinition
214      *
215      * @throws \RuntimeException
216      */
217     public function end()
218     {
219         if (null === $this->ifPart) {
220             throw new \RuntimeException('You must specify an if part.');
221         }
222         if (null === $this->thenPart) {
223             throw new \RuntimeException('You must specify a then part.');
224         }
225
226         return $this->node;
227     }
228
229     /**
230      * Builds the expressions.
231      *
232      * @param ExprBuilder[] $expressions An array of ExprBuilder instances to build
233      *
234      * @return array
235      */
236     public static function buildExpressions(array $expressions)
237     {
238         foreach ($expressions as $k => $expr) {
239             if ($expr instanceof self) {
240                 $if = $expr->ifPart;
241                 $then = $expr->thenPart;
242                 $expressions[$k] = function ($v) use ($if, $then) {
243                     return $if($v) ? $then($v) : $v;
244                 };
245             }
246         }
247
248         return $expressions;
249     }
250 }