Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
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 /**
029 * Constructor.
030 *
031 * @param NodeDefinition $node The related node
032 */
033 public function __construct(NodeDefinition $node)
034 {
035 $this->node = $node;
036 }
037
038 /**
039 * Marks the expression as being always used.
040 *
041 * @param \Closure $then
042 *
043 * @return ExprBuilder
044 */
045 public function always(\Closure $then = null)
046 {
047 $this->ifPart = function ($v) { return true; };
048
049 if (null !== $then) {
050 $this->thenPart = $then;
051 }
052
053 return $this;
054 }
055
056 /**
057 * Sets a closure to use as tests.
058 *
059 * The default one tests if the value is true.
060 *
061 * @param \Closure $closure
062 *
063 * @return ExprBuilder
064 */
065 public function ifTrue(\Closure $closure = null)
066 {
067 if (null === $closure) {
068 $closure = function ($v) { return true === $v; };
069 }
070
071 $this->ifPart = $closure;
072
073 return $this;
074 }
075
076 /**
077 * Tests if the value is a string.
078 *
079 * @return ExprBuilder
080 */
081 public function ifString()
082 {
083 $this->ifPart = function ($v) { return is_string($v); };
084
085 return $this;
086 }
087
088 /**
089 * Tests if the value is null.
090 *
091 * @return ExprBuilder
092 */
093 public function ifNull()
094 {
095 $this->ifPart = function ($v) { return null === $v; };
096
097 return $this;
098 }
099
100 /**
101 * Tests if the value is an array.
102 *
103 * @return ExprBuilder
104 */
105 public function ifArray()
106 {
107 $this->ifPart = function ($v) { return is_array($v); };
108
109 return $this;
110 }
111
112 /**
113 * Tests if the value is in an array.
114 *
115 * @param array $array
116 *
117 * @return ExprBuilder
118 */
119 public function ifInArray(array $array)
120 {
121 $this->ifPart = function ($v) use ($array) { return in_array($v, $array, true); };
122
123 return $this;
124 }
125
126 /**
127 * Tests if the value is not in an array.
128 *
129 * @param array $array
130 *
131 * @return ExprBuilder
132 */
133 public function ifNotInArray(array $array)
134 {
135 $this->ifPart = function ($v) use ($array) { return !in_array($v, $array, true); };
136
137 return $this;
138 }
139
140 /**
141 * Sets the closure to run if the test pass.
142 *
143 * @param \Closure $closure
144 *
145 * @return ExprBuilder
146 */
147 public function then(\Closure $closure)
148 {
149 $this->thenPart = $closure;
150
151 return $this;
152 }
153
154 /**
155 * Sets a closure returning an empty array.
156 *
157 * @return ExprBuilder
158 */
159 public function thenEmptyArray()
160 {
161 $this->thenPart = function ($v) { return array(); };
162
163 return $this;
164 }
165
166 /**
167 * Sets a closure marking the value as invalid at validation time.
168 *
169 * if you want to add the value of the node in your message just use a %s placeholder.
170 *
171 * @param string $message
172 *
173 * @return ExprBuilder
174 *
175 * @throws \InvalidArgumentException
176 */
177 public function thenInvalid($message)
178 {
179 $this->thenPart = function ($v) use ($message) {throw new \InvalidArgumentException(sprintf($message, json_encode($v))); };
180
181 return $this;
182 }
183
184 /**
185 * Sets a closure unsetting this key of the array at validation time.
186 *
187 * @return ExprBuilder
188 *
189 * @throws UnsetKeyException
190 */
191 public function thenUnset()
192 {
193 $this->thenPart = function ($v) { throw new UnsetKeyException('Unsetting key'); };
194
195 return $this;
196 }
197
198 /**
199 * Returns the related node.
200 *
201 * @return NodeDefinition
202 *
203 * @throws \RuntimeException
204 */
205 public function end()
206 {
207 if (null === $this->ifPart) {
208 throw new \RuntimeException('You must specify an if part.');
209 }
210 if (null === $this->thenPart) {
211 throw new \RuntimeException('You must specify a then part.');
212 }
213
214 return $this->node;
215 }
216
217 /**
218 * Builds the expressions.
219 *
220 * @param ExprBuilder[] $expressions An array of ExprBuilder instances to build
221 *
222 * @return array
223 */
224 public static function buildExpressions(array $expressions)
225 {
226 foreach ($expressions as $k => $expr) {
227 if ($expr instanceof self) {
228 $if = $expr->ifPart;
229 $then = $expr->thenPart;
230 $expressions[$k] = function ($v) use ($if, $then) {
231 return $if($v) ? $then($v) : $v;
232 };
233 }
234 }
235
236 return $expressions;
237 }
238 }
239