Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
ExprBuilder.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\Config\Definition\Builder;
013
014 use Symfony\Component\Config\Definition\Exception\UnsetKeyException;
015
016 /**
017 * This class builds an if expression.
018 *
019 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
020 * @author Christophe Coevoet <stof@notk.org>
021 */
022 class ExprBuilder
023 {
024 protected $node;
025 public $ifPart;
026 public $thenPart;
027
028 public function __construct(NodeDefinition $node)
029 {
030 $this->node = $node;
031 }
032
033 /**
034 * Marks the expression as being always used.
035 *
036 * @return $this
037 */
038 public function always(\Closure $then = null)
039 {
040 $this->ifPart = function ($v) { return true; };
041
042 if (null !== $then) {
043 $this->thenPart = $then;
044 }
045
046 return $this;
047 }
048
049 /**
050 * Sets a closure to use as tests.
051 *
052 * The default one tests if the value is true.
053 *
054 * @return $this
055 */
056 public function ifTrue(\Closure $closure = null)
057 {
058 if (null === $closure) {
059 $closure = function ($v) { return true === $v; };
060 }
061
062 $this->ifPart = $closure;
063
064 return $this;
065 }
066
067 /**
068 * Tests if the value is a string.
069 *
070 * @return $this
071 */
072 public function ifString()
073 {
074 $this->ifPart = function ($v) { return \is_string($v); };
075
076 return $this;
077 }
078
079 /**
080 * Tests if the value is null.
081 *
082 * @return $this
083 */
084 public function ifNull()
085 {
086 $this->ifPart = function ($v) { return null === $v; };
087
088 return $this;
089 }
090
091 /**
092 * Tests if the value is empty.
093 *
094 * @return ExprBuilder
095 */
096 public function ifEmpty()
097 {
098 $this->ifPart = function ($v) { return empty($v); };
099
100 return $this;
101 }
102
103 /**
104 * Tests if the value is an array.
105 *
106 * @return $this
107 */
108 public function ifArray()
109 {
110 $this->ifPart = function ($v) { return \is_array($v); };
111
112 return $this;
113 }
114
115 /**
116 * Tests if the value is in an array.
117 *
118 * @return $this
119 */
120 public function ifInArray(array $array)
121 {
122 $this->ifPart = function ($v) use ($array) { return \in_array($v, $array, true); };
123
124 return $this;
125 }
126
127 /**
128 * Tests if the value is not in an array.
129 *
130 * @return $this
131 */
132 public function ifNotInArray(array $array)
133 {
134 $this->ifPart = function ($v) use ($array) { return !\in_array($v, $array, true); };
135
136 return $this;
137 }
138
139 /**
140 * Transforms variables of any type into an array.
141 *
142 * @return $this
143 */
144 public function castToArray()
145 {
146 $this->ifPart = function ($v) { return !\is_array($v); };
147 $this->thenPart = function ($v) { return [$v]; };
148
149 return $this;
150 }
151
152 /**
153 * Sets the closure to run if the test pass.
154 *
155 * @return $this
156 */
157 public function then(\Closure $closure)
158 {
159 $this->thenPart = $closure;
160
161 return $this;
162 }
163
164 /**
165 * Sets a closure returning an empty array.
166 *
167 * @return $this
168 */
169 public function thenEmptyArray()
170 {
171 $this->thenPart = function ($v) { return []; };
172
173 return $this;
174 }
175
176 /**
177 * Sets a closure marking the value as invalid at processing time.
178 *
179 * if you want to add the value of the node in your message just use a %s placeholder.
180 *
181 * @param string $message
182 *
183 * @return $this
184 *
185 * @throws \InvalidArgumentException
186 */
187 public function thenInvalid($message)
188 {
189 $this->thenPart = function ($v) use ($message) { throw new \InvalidArgumentException(sprintf($message, json_encode($v))); };
190
191 return $this;
192 }
193
194 /**
195 * Sets a closure unsetting this key of the array at processing time.
196 *
197 * @return $this
198 *
199 * @throws UnsetKeyException
200 */
201 public function thenUnset()
202 {
203 $this->thenPart = function ($v) { throw new UnsetKeyException('Unsetting key.'); };
204
205 return $this;
206 }
207
208 /**
209 * Returns the related node.
210 *
211 * @return NodeDefinition|ArrayNodeDefinition|VariableNodeDefinition
212 *
213 * @throws \RuntimeException
214 */
215 public function end()
216 {
217 if (null === $this->ifPart) {
218 throw new \RuntimeException('You must specify an if part.');
219 }
220 if (null === $this->thenPart) {
221 throw new \RuntimeException('You must specify a then part.');
222 }
223
224 return $this->node;
225 }
226
227 /**
228 * Builds the expressions.
229 *
230 * @param ExprBuilder[] $expressions An array of ExprBuilder instances to build
231 *
232 * @return array
233 */
234 public static function buildExpressions(array $expressions)
235 {
236 foreach ($expressions as $k => $expr) {
237 if ($expr instanceof self) {
238 $if = $expr->ifPart;
239 $then = $expr->thenPart;
240 $expressions[$k] = function ($v) use ($if, $then) {
241 return $if($v) ? $then($v) : $v;
242 };
243 }
244 }
245
246 return $expressions;
247 }
248 }
249