|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of Twig. |
|
|
5 |
* |
|
|
6 |
* (c) 2009 Fabien Potencier |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
class Twig_Extension_Core extends Twig_Extension |
|
|
12 |
{ |
|
|
13 |
/** |
|
|
14 |
* Returns the token parser instance to add to the existing list. |
|
|
15 |
* |
|
|
16 |
* @return array An array of Twig_TokenParser instances |
|
|
17 |
*/ |
|
|
18 |
public function getTokenParsers() |
|
|
19 |
{ |
|
|
20 |
return array( |
|
|
21 |
new Twig_TokenParser_For(), |
|
|
22 |
new Twig_TokenParser_If(), |
|
|
23 |
new Twig_TokenParser_Extends(), |
|
|
24 |
new Twig_TokenParser_Include(), |
|
|
25 |
new Twig_TokenParser_Block(), |
|
|
26 |
new Twig_TokenParser_Use(), |
|
|
27 |
new Twig_TokenParser_Filter(), |
|
|
28 |
new Twig_TokenParser_Macro(), |
|
|
29 |
new Twig_TokenParser_Import(), |
|
|
30 |
new Twig_TokenParser_From(), |
|
|
31 |
new Twig_TokenParser_Set(), |
|
|
32 |
new Twig_TokenParser_Spaceless(), |
|
|
33 |
); |
|
|
34 |
} |
|
|
35 |
|
|
|
36 |
/** |
|
|
37 |
* Returns a list of filters to add to the existing list. |
|
|
38 |
* |
|
|
39 |
* @return array An array of filters |
|
|
40 |
*/ |
|
|
41 |
public function getFilters() |
|
|
42 |
{ |
|
|
43 |
$filters = array( |
|
|
44 |
// formatting filters |
|
|
45 |
'date' => new Twig_Filter_Function('twig_date_format_filter'), |
|
|
46 |
'format' => new Twig_Filter_Function('sprintf'), |
|
|
47 |
'replace' => new Twig_Filter_Function('twig_strtr'), |
|
|
48 |
|
|
|
49 |
// encoding |
|
|
50 |
'url_encode' => new Twig_Filter_Function('twig_urlencode_filter'), |
|
|
51 |
'json_encode' => new Twig_Filter_Function('twig_jsonencode_filter'), |
|
|
52 |
|
|
|
53 |
// string filters |
|
|
54 |
'title' => new Twig_Filter_Function('twig_title_string_filter', array('needs_environment' => true)), |
|
|
55 |
'capitalize' => new Twig_Filter_Function('twig_capitalize_string_filter', array('needs_environment' => true)), |
|
|
56 |
'upper' => new Twig_Filter_Function('strtoupper'), |
|
|
57 |
'lower' => new Twig_Filter_Function('strtolower'), |
|
|
58 |
'striptags' => new Twig_Filter_Function('strip_tags'), |
|
|
59 |
|
|
|
60 |
// array helpers |
|
|
61 |
'join' => new Twig_Filter_Function('twig_join_filter'), |
|
|
62 |
'reverse' => new Twig_Filter_Function('twig_reverse_filter'), |
|
|
63 |
'length' => new Twig_Filter_Function('twig_length_filter', array('needs_environment' => true)), |
|
|
64 |
'sort' => new Twig_Filter_Function('twig_sort_filter'), |
|
|
65 |
'merge' => new Twig_Filter_Function('twig_array_merge'), |
|
|
66 |
|
|
|
67 |
// iteration and runtime |
|
|
68 |
'default' => new Twig_Filter_Function('twig_default_filter'), |
|
|
69 |
'keys' => new Twig_Filter_Function('twig_get_array_keys_filter'), |
|
|
70 |
|
|
|
71 |
// escaping |
|
|
72 |
'escape' => new Twig_Filter_Function('twig_escape_filter', array('needs_environment' => true, 'is_safe_callback' => 'twig_escape_filter_is_safe')), |
|
|
73 |
'e' => new Twig_Filter_Function('twig_escape_filter', array('needs_environment' => true, 'is_safe_callback' => 'twig_escape_filter_is_safe')), |
|
|
74 |
); |
|
|
75 |
|
|
|
76 |
if (function_exists('mb_get_info')) { |
|
|
77 |
$filters['upper'] = new Twig_Filter_Function('twig_upper_filter', array('needs_environment' => true)); |
|
|
78 |
$filters['lower'] = new Twig_Filter_Function('twig_lower_filter', array('needs_environment' => true)); |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
return $filters; |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
/** |
|
|
85 |
* Returns a list of global functions to add to the existing list. |
|
|
86 |
* |
|
|
87 |
* @return array An array of global functions |
|
|
88 |
*/ |
|
|
89 |
public function getFunctions() |
|
|
90 |
{ |
|
|
91 |
return array( |
|
|
92 |
'range' => new Twig_Function_Function('range'), |
|
|
93 |
'constant' => new Twig_Function_Function('constant'), |
|
|
94 |
'cycle' => new Twig_Function_Function('twig_cycle'), |
|
|
95 |
); |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
/** |
|
|
99 |
* Returns a list of filters to add to the existing list. |
|
|
100 |
* |
|
|
101 |
* @return array An array of filters |
|
|
102 |
*/ |
|
|
103 |
public function getTests() |
|
|
104 |
{ |
|
|
105 |
return array( |
|
|
106 |
'even' => new Twig_Test_Function('twig_test_even'), |
|
|
107 |
'odd' => new Twig_Test_Function('twig_test_odd'), |
|
|
108 |
'defined' => new Twig_Test_Function('twig_test_defined'), |
|
|
109 |
'sameas' => new Twig_Test_Function('twig_test_sameas'), |
|
|
110 |
'none' => new Twig_Test_Function('twig_test_none'), |
|
|
111 |
'null' => new Twig_Test_Function('twig_test_none'), |
|
|
112 |
'divisibleby' => new Twig_Test_Function('twig_test_divisibleby'), |
|
|
113 |
'constant' => new Twig_Test_Function('twig_test_constant'), |
|
|
114 |
'empty' => new Twig_Test_Function('twig_test_empty'), |
|
|
115 |
); |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
/** |
|
|
119 |
* Returns a list of operators to add to the existing list. |
|
|
120 |
* |
|
|
121 |
* @return array An array of operators |
|
|
122 |
*/ |
|
|
123 |
public function getOperators() |
|
|
124 |
{ |
|
|
125 |
return array( |
|
|
126 |
array( |
|
|
127 |
'not' => array('precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'), |
|
|
128 |
'-' => array('precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Neg'), |
|
|
129 |
'+' => array('precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Pos'), |
|
|
130 |
), |
|
|
131 |
array( |
|
|
132 |
'or' => array('precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
133 |
'and' => array('precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
134 |
'==' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Equal', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
135 |
'!=' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotEqual', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
136 |
'<' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Less', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
137 |
'>' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Greater', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
138 |
'>=' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_GreaterEqual', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
139 |
'<=' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_LessEqual', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
140 |
'not in' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotIn', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
141 |
'in' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_In', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
142 |
'+' => array('precedence' => 30, 'class' => 'Twig_Node_Expression_Binary_Add', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
143 |
'-' => array('precedence' => 30, 'class' => 'Twig_Node_Expression_Binary_Sub', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
144 |
'~' => array('precedence' => 40, 'class' => 'Twig_Node_Expression_Binary_Concat', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
145 |
'*' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Mul', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
146 |
'/' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Div', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
147 |
'//' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_FloorDiv', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
148 |
'%' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Mod', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
149 |
'is' => array('precedence' => 100, 'callable' => array($this, 'parseTestExpression'), 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
150 |
'is not' => array('precedence' => 100, 'callable' => array($this, 'parseNotTestExpression'), 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
151 |
'..' => array('precedence' => 110, 'class' => 'Twig_Node_Expression_Binary_Range', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
|
152 |
'**' => array('precedence' => 200, 'class' => 'Twig_Node_Expression_Binary_Power', 'associativity' => Twig_ExpressionParser::OPERATOR_RIGHT), |
|
|
153 |
), |
|
|
154 |
); |
|
|
155 |
} |
|
|
156 |
|
|
|
157 |
public function parseNotTestExpression(Twig_Parser $parser, $node) |
|
|
158 |
{ |
|
|
159 |
return new Twig_Node_Expression_Unary_Not($this->parseTestExpression($parser, $node), $parser->getCurrentToken()->getLine()); |
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
public function parseTestExpression(Twig_Parser $parser, $node) |
|
|
163 |
{ |
|
|
164 |
$stream = $parser->getStream(); |
|
|
165 |
$name = $stream->expect(Twig_Token::NAME_TYPE); |
|
|
166 |
$arguments = null; |
|
|
167 |
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) { |
|
|
168 |
$arguments = $parser->getExpressionParser()->parseArguments(); |
|
|
169 |
} |
|
|
170 |
|
|
|
171 |
return new Twig_Node_Expression_Test($node, $name->getValue(), $arguments, $parser->getCurrentToken()->getLine()); |
|
|
172 |
} |
|
|
173 |
|
|
|
174 |
/** |
|
|
175 |
* Returns the name of the extension. |
|
|
176 |
* |
|
|
177 |
* @return string The extension name |
|
|
178 |
*/ |
|
|
179 |
public function getName() |
|
|
180 |
{ |
|
|
181 |
return 'core'; |
|
|
182 |
} |
|
|
183 |
} |
|
|
184 |
|
|
|
185 |
/** |
|
|
186 |
* Cycles over a value. |
|
|
187 |
* |
|
|
188 |
* @param ArrayAccess|array $values An array or an ArrayAccess instance |
|
|
189 |
* @param integer $i The cycle value |
|
|
190 |
* |
|
|
191 |
* @return string The next value in the cycle |
|
|
192 |
*/ |
|
|
193 |
function twig_cycle($values, $i) |
|
|
194 |
{ |
|
|
195 |
if (!is_array($values) && !$values instanceof ArrayAccess) { |
|
|
196 |
return $values; |
|
|
197 |
} |
|
|
198 |
|
|
|
199 |
return $values[$i % count($values)]; |
|
|
200 |
} |
|
|
201 |
|
|
|
202 |
/** |
|
|
203 |
* Converts a date to the given format. |
|
|
204 |
* |
|
|
205 |
* <pre> |
|
|
206 |
* {{ post.published_at|date("m/d/Y") }} |
|
|
207 |
* </pre> |
|
|
208 |
* |
|
|
209 |
* @param DateTime|string $date A date |
|
|
210 |
* @param string $format A format |
|
|
211 |
* @param DateTimeZone|string $timezone A timezone |
|
|
212 |
* |
|
|
213 |
* @return string The formatter date |
|
|
214 |
*/ |
|
|
215 |
function twig_date_format_filter($date, $format = 'F j, Y H:i', $timezone = null) |
|
|
216 |
{ |
|
|
217 |
if (!$date instanceof DateTime) { |
|
|
218 |
if (ctype_digit((string) $date)) { |
|
|
219 |
$date = new DateTime('@'.$date); |
|
|
220 |
$date->setTimezone(new DateTimeZone(date_default_timezone_get())); |
|
|
221 |
} else { |
|
|
222 |
$date = new DateTime($date); |
|
|
223 |
} |
|
|
224 |
} |
|
|
225 |
|
|
|
226 |
if (null !== $timezone) { |
|
|
227 |
if (!$timezone instanceof DateTimeZone) { |
|
|
228 |
$timezone = new DateTimeZone($timezone); |
|
|
229 |
} |
|
|
230 |
|
|
|
231 |
$date->setTimezone($timezone); |
|
|
232 |
} |
|
|
233 |
|
|
|
234 |
return $date->format($format); |
|
|
235 |
} |
|
|
236 |
|
|
|
237 |
/** |
|
|
238 |
* URL encodes a string. |
|
|
239 |
* |
|
|
240 |
* @param string $url A URL |
|
|
241 |
* @param bool $raw true to use rawurlencode() instead of urlencode |
|
|
242 |
* |
|
|
243 |
* @return string The URL encoded value |
|
|
244 |
*/ |
|
|
245 |
function twig_urlencode_filter($url, $raw = false) |
|
|
246 |
{ |
|
|
247 |
if ($raw) { |
|
|
248 |
return rawurlencode($url); |
|
|
249 |
} |
|
|
250 |
|
|
|
251 |
return urlencode($url); |
|
|
252 |
} |
|
|
253 |
|
|
|
254 |
if (version_compare(PHP_VERSION, '5.3.0', '<')) { |
|
|
255 |
/** |
|
|
256 |
* JSON encodes a PHP variable. |
|
|
257 |
* |
|
|
258 |
* @param mixed $value The value to encode. |
|
|
259 |
* @param integer $options Not used on PHP 5.2.x |
|
|
260 |
* |
|
|
261 |
* @return mixed The JSON encoded value |
|
|
262 |
*/ |
|
|
263 |
function twig_jsonencode_filter($value, $options = 0) |
|
|
264 |
{ |
|
|
265 |
if ($value instanceof Twig_Markup) { |
|
|
266 |
$value = (string) $value; |
|
|
267 |
} elseif (is_array($value)) { |
|
|
268 |
array_walk_recursive($value, '_twig_markup2string'); |
|
|
269 |
} |
|
|
270 |
|
|
|
271 |
return json_encode($value); |
|
|
272 |
} |
|
|
273 |
} else { |
|
|
274 |
/** |
|
|
275 |
* JSON encodes a PHP variable. |
|
|
276 |
* |
|
|
277 |
* @param mixed $value The value to encode. |
|
|
278 |
* @param integer $options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT |
|
|
279 |
* |
|
|
280 |
* @return mixed The JSON encoded value |
|
|
281 |
*/ |
|
|
282 |
function twig_jsonencode_filter($value, $options = 0) |
|
|
283 |
{ |
|
|
284 |
if ($value instanceof Twig_Markup) { |
|
|
285 |
$value = (string) $value; |
|
|
286 |
} elseif (is_array($value)) { |
|
|
287 |
array_walk_recursive($value, '_twig_markup2string'); |
|
|
288 |
} |
|
|
289 |
|
|
|
290 |
return json_encode($value, $options); |
|
|
291 |
} |
|
|
292 |
} |
|
|
293 |
|
|
|
294 |
function _twig_markup2string(&$value) |
|
|
295 |
{ |
|
|
296 |
if ($value instanceof Twig_Markup) { |
|
|
297 |
$value = (string) $value; |
|
|
298 |
} |
|
|
299 |
} |
|
|
300 |
|
|
|
301 |
/** |
|
|
302 |
* Merges an array with another one. |
|
|
303 |
* |
|
|
304 |
* <pre> |
|
|
305 |
* {% set items = { 'apple': 'fruit', 'orange': 'fruit' } %} |
|
|
306 |
* |
|
|
307 |
* {% set items = items|merge({ 'peugeot': 'car' }) %} |
|
|
308 |
* |
|
|
309 |
* {# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car' } #} |
|
|
310 |
* </pre> |
|
|
311 |
* |
|
|
312 |
* @param array $arr1 An array |
|
|
313 |
* @param array $arr2 An array |
|
|
314 |
* |
|
|
315 |
* @return array The merged array |
|
|
316 |
*/ |
|
|
317 |
function twig_array_merge($arr1, $arr2) |
|
|
318 |
{ |
|
|
319 |
if (!is_array($arr1) || !is_array($arr2)) { |
|
|
320 |
throw new Twig_Error_Runtime('The merge filter only work with arrays or hashes.'); |
|
|
321 |
} |
|
|
322 |
|
|
|
323 |
return array_merge($arr1, $arr2); |
|
|
324 |
} |
|
|
325 |
|
|
|
326 |
/** |
|
|
327 |
* Joins the values to a string. |
|
|
328 |
* |
|
|
329 |
* The separator between elements is an empty string per default, you can define it with the optional parameter. |
|
|
330 |
* |
|
|
331 |
* <pre> |
|
|
332 |
* {{ [1, 2, 3]|join('|') }} |
|
|
333 |
* {# returns 1|2|3 #} |
|
|
334 |
* |
|
|
335 |
* {{ [1, 2, 3]|join }} |
|
|
336 |
* {# returns 123 #} |
|
|
337 |
* </pre> |
|
|
338 |
* |
|
|
339 |
* @param array $value An array |
|
|
340 |
* @param string $glue The separator |
|
|
341 |
* |
|
|
342 |
* @return string The concatenated string |
|
|
343 |
*/ |
|
|
344 |
function twig_join_filter($value, $glue = '') |
|
|
345 |
{ |
|
|
346 |
return implode($glue, (array) $value); |
|
|
347 |
} |
|
|
348 |
|
|
|
349 |
/** |
|
|
350 |
* Returns the value or the default value when it is undefined or empty. |
|
|
351 |
* |
|
|
352 |
* <pre> |
|
|
353 |
* |
|
|
354 |
* {{ var.foo|default('foo item on var is not defined') }} |
|
|
355 |
* |
|
|
356 |
* </pre> |
|
|
357 |
* |
|
|
358 |
* @param mixed $value A value |
|
|
359 |
* @param mixed $default The default value |
|
|
360 |
* |
|
|
361 |
* @param mixed The value or the default value; |
|
|
362 |
*/ |
|
|
363 |
function twig_default_filter($value, $default = '') |
|
|
364 |
{ |
|
|
365 |
if (twig_test_empty($value)) { |
|
|
366 |
return $default; |
|
|
367 |
} else { |
|
|
368 |
return $value; |
|
|
369 |
} |
|
|
370 |
} |
|
|
371 |
|
|
|
372 |
/** |
|
|
373 |
* Returns the keys for the given array. |
|
|
374 |
* |
|
|
375 |
* It is useful when you want to iterate over the keys of an array: |
|
|
376 |
* |
|
|
377 |
* <pre> |
|
|
378 |
* {% for key in array|keys %} |
|
|
379 |
* {# ... #} |
|
|
380 |
* {% endfor %} |
|
|
381 |
* </pre> |
|
|
382 |
* |
|
|
383 |
* @param array $array An array |
|
|
384 |
* |
|
|
385 |
* @return array The keys |
|
|
386 |
*/ |
|
|
387 |
function twig_get_array_keys_filter($array) |
|
|
388 |
{ |
|
|
389 |
if (is_object($array) && $array instanceof Traversable) { |
|
|
390 |
return array_keys(iterator_to_array($array)); |
|
|
391 |
} |
|
|
392 |
|
|
|
393 |
if (!is_array($array)) { |
|
|
394 |
return array(); |
|
|
395 |
} |
|
|
396 |
|
|
|
397 |
return array_keys($array); |
|
|
398 |
} |
|
|
399 |
|
|
|
400 |
/** |
|
|
401 |
* Reverses an array. |
|
|
402 |
* |
|
|
403 |
* @param array|Traversable $array An array or a Traversable instance |
|
|
404 |
* |
|
|
405 |
* return array The array reversed |
|
|
406 |
*/ |
|
|
407 |
function twig_reverse_filter($array) |
|
|
408 |
{ |
|
|
409 |
if (is_object($array) && $array instanceof Traversable) { |
|
|
410 |
return array_reverse(iterator_to_array($array)); |
|
|
411 |
} |
|
|
412 |
|
|
|
413 |
if (!is_array($array)) { |
|
|
414 |
return array(); |
|
|
415 |
} |
|
|
416 |
|
|
|
417 |
return array_reverse($array); |
|
|
418 |
} |
|
|
419 |
|
|
|
420 |
/** |
|
|
421 |
* Sorts an array. |
|
|
422 |
* |
|
|
423 |
* @param array $array An array |
|
|
424 |
*/ |
|
|
425 |
function twig_sort_filter($array) |
|
|
426 |
{ |
|
|
427 |
asort($array); |
|
|
428 |
|
|
|
429 |
return $array; |
|
|
430 |
} |
|
|
431 |
|
|
|
432 |
/* used internally */ |
|
|
433 |
function twig_in_filter($value, $compare) |
|
|
434 |
{ |
|
|
435 |
if (is_array($compare)) { |
|
|
436 |
return in_array($value, $compare); |
|
|
437 |
} elseif (is_string($compare)) { |
|
|
438 |
return false !== strpos($compare, (string) $value); |
|
|
439 |
} elseif (is_object($compare) && $compare instanceof Traversable) { |
|
|
440 |
return in_array($value, iterator_to_array($compare, false)); |
|
|
441 |
} |
|
|
442 |
|
|
|
443 |
return false; |
|
|
444 |
} |
|
|
445 |
|
|
|
446 |
/** |
|
|
447 |
* Replaces placeholders in a string. |
|
|
448 |
* |
|
|
449 |
* <pre> |
|
|
450 |
* {{ "I like %this% and %that%."|replace({'%this%': foo, '%that%': "bar"}) }} |
|
|
451 |
* </pre> |
|
|
452 |
* |
|
|
453 |
* @param string $pattern A string |
|
|
454 |
* @param string $replacements The values for the placeholders |
|
|
455 |
* |
|
|
456 |
* @return string The string where the placeholders have been replaced |
|
|
457 |
*/ |
|
|
458 |
function twig_strtr($pattern, $replacements) |
|
|
459 |
{ |
|
|
460 |
return str_replace(array_keys($replacements), array_values($replacements), $pattern); |
|
|
461 |
} |
|
|
462 |
|
|
|
463 |
/** |
|
|
464 |
* Escapes a string. |
|
|
465 |
* |
|
|
466 |
* @param Twig_Environment $env A Twig_Environment instance |
|
|
467 |
* @param string $string The value to be escaped |
|
|
468 |
* @param string $type The escaping strategy |
|
|
469 |
* @param string $charset The charset |
|
|
470 |
*/ |
|
|
471 |
function twig_escape_filter(Twig_Environment $env, $string, $type = 'html', $charset = null) |
|
|
472 |
{ |
|
|
473 |
if (is_object($string) && $string instanceof Twig_Markup) { |
|
|
474 |
return $string; |
|
|
475 |
} |
|
|
476 |
|
|
|
477 |
if (!is_string($string) && !(is_object($string) && method_exists($string, '__toString'))) { |
|
|
478 |
return $string; |
|
|
479 |
} |
|
|
480 |
|
|
|
481 |
if (null === $charset) { |
|
|
482 |
$charset = $env->getCharset(); |
|
|
483 |
} |
|
|
484 |
|
|
|
485 |
switch ($type) { |
|
|
486 |
case 'js': |
|
|
487 |
// escape all non-alphanumeric characters |
|
|
488 |
// into their \xHH or \uHHHH representations |
|
|
489 |
if ('UTF-8' != $charset) { |
|
|
490 |
$string = _twig_convert_encoding($string, 'UTF-8', $charset); |
|
|
491 |
} |
|
|
492 |
|
|
|
493 |
if (null === $string = preg_replace_callback('#[^\p{L}\p{N} ]#u', '_twig_escape_js_callback', $string)) { |
|
|
494 |
throw new Twig_Error_Runtime('The string to escape is not a valid UTF-8 string.'); |
|
|
495 |
} |
|
|
496 |
|
|
|
497 |
if ('UTF-8' != $charset) { |
|
|
498 |
$string = _twig_convert_encoding($string, $charset, 'UTF-8'); |
|
|
499 |
} |
|
|
500 |
|
|
|
501 |
return $string; |
|
|
502 |
|
|
|
503 |
case 'html': |
|
|
504 |
return htmlspecialchars($string, ENT_QUOTES, $charset); |
|
|
505 |
|
|
|
506 |
default: |
|
|
507 |
throw new Twig_Error_Runtime(sprintf('Invalid escape type "%s".', $type)); |
|
|
508 |
} |
|
|
509 |
} |
|
|
510 |
|
|
|
511 |
/* used internally */ |
|
|
512 |
function twig_escape_filter_is_safe(Twig_Node $filterArgs) |
|
|
513 |
{ |
|
|
514 |
foreach ($filterArgs as $arg) { |
|
|
515 |
if ($arg instanceof Twig_Node_Expression_Constant) { |
|
|
516 |
return array($arg->getAttribute('value')); |
|
|
517 |
} else { |
|
|
518 |
return array(); |
|
|
519 |
} |
|
|
520 |
|
|
|
521 |
break; |
|
|
522 |
} |
|
|
523 |
|
|
|
524 |
return array('html'); |
|
|
525 |
} |
|
|
526 |
|
|
|
527 |
if (function_exists('iconv')) { |
|
|
528 |
function _twig_convert_encoding($string, $to, $from) |
|
|
529 |
{ |
|
|
530 |
return iconv($from, $to, $string); |
|
|
531 |
} |
|
|
532 |
} elseif (function_exists('mb_convert_encoding')) { |
|
|
533 |
function _twig_convert_encoding($string, $to, $from) |
|
|
534 |
{ |
|
|
535 |
return mb_convert_encoding($string, $to, $from); |
|
|
536 |
} |
|
|
537 |
} else { |
|
|
538 |
function _twig_convert_encoding($string, $to, $from) |
|
|
539 |
{ |
|
|
540 |
throw new Twig_Error_Runtime('No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).'); |
|
|
541 |
} |
|
|
542 |
} |
|
|
543 |
|
|
|
544 |
function _twig_escape_js_callback($matches) |
|
|
545 |
{ |
|
|
546 |
$char = $matches[0]; |
|
|
547 |
|
|
|
548 |
// \xHH |
|
|
549 |
if (!isset($char[1])) { |
|
|
550 |
return '\\x'.substr('00'.bin2hex($char), -2); |
|
|
551 |
} |
|
|
552 |
|
|
|
553 |
// \uHHHH |
|
|
554 |
$char = _twig_convert_encoding($char, 'UTF-16BE', 'UTF-8'); |
|
|
555 |
|
|
|
556 |
return '\\u'.substr('0000'.bin2hex($char), -4); |
|
|
557 |
} |
|
|
558 |
|
|
|
559 |
// add multibyte extensions if possible |
|
|
560 |
if (function_exists('mb_get_info')) { |
|
|
561 |
/** |
|
|
562 |
* Returns the length of a PHP variable. |
|
|
563 |
* |
|
|
564 |
* @param Twig_Environment $env A Twig_Environment instance |
|
|
565 |
* @param mixed $thing A PHP variable |
|
|
566 |
* |
|
|
567 |
* @return integer The length of the value |
|
|
568 |
*/ |
|
|
569 |
function twig_length_filter(Twig_Environment $env, $thing) |
|
|
570 |
{ |
|
|
571 |
return is_scalar($thing) ? mb_strlen($thing, $env->getCharset()) : count($thing); |
|
|
572 |
} |
|
|
573 |
|
|
|
574 |
/** |
|
|
575 |
* Converts a string to uppercase. |
|
|
576 |
* |
|
|
577 |
* @param Twig_Environment $env A Twig_Environment instance |
|
|
578 |
* @param string $string A string |
|
|
579 |
* |
|
|
580 |
* @return string The uppercased string |
|
|
581 |
*/ |
|
|
582 |
function twig_upper_filter(Twig_Environment $env, $string) |
|
|
583 |
{ |
|
|
584 |
if (null !== ($charset = $env->getCharset())) { |
|
|
585 |
return mb_strtoupper($string, $charset); |
|
|
586 |
} |
|
|
587 |
|
|
|
588 |
return strtoupper($string); |
|
|
589 |
} |
|
|
590 |
|
|
|
591 |
/** |
|
|
592 |
* Converts a string to lowercase. |
|
|
593 |
* |
|
|
594 |
* @param Twig_Environment $env A Twig_Environment instance |
|
|
595 |
* @param string $string A string |
|
|
596 |
* |
|
|
597 |
* @return string The lowercased string |
|
|
598 |
*/ |
|
|
599 |
function twig_lower_filter(Twig_Environment $env, $string) |
|
|
600 |
{ |
|
|
601 |
if (null !== ($charset = $env->getCharset())) { |
|
|
602 |
return mb_strtolower($string, $charset); |
|
|
603 |
} |
|
|
604 |
|
|
|
605 |
return strtolower($string); |
|
|
606 |
} |
|
|
607 |
|
|
|
608 |
/** |
|
|
609 |
* Returns a titlecased string. |
|
|
610 |
* |
|
|
611 |
* @param Twig_Environment $env A Twig_Environment instance |
|
|
612 |
* @param string $string A string |
|
|
613 |
* |
|
|
614 |
* @return string The titlecased string |
|
|
615 |
*/ |
|
|
616 |
function twig_title_string_filter(Twig_Environment $env, $string) |
|
|
617 |
{ |
|
|
618 |
if (null !== ($charset = $env->getCharset())) { |
|
|
619 |
return mb_convert_case($string, MB_CASE_TITLE, $charset); |
|
|
620 |
} |
|
|
621 |
|
|
|
622 |
return ucwords(strtolower($string)); |
|
|
623 |
} |
|
|
624 |
|
|
|
625 |
/** |
|
|
626 |
* Returns a capitalized string. |
|
|
627 |
* |
|
|
628 |
* @param Twig_Environment $env A Twig_Environment instance |
|
|
629 |
* @param string $string A string |
|
|
630 |
* |
|
|
631 |
* @return string The capitalized string |
|
|
632 |
*/ |
|
|
633 |
function twig_capitalize_string_filter(Twig_Environment $env, $string) |
|
|
634 |
{ |
|
|
635 |
if (null !== ($charset = $env->getCharset())) { |
|
|
636 |
return mb_strtoupper(mb_substr($string, 0, 1, $charset), $charset). |
|
|
637 |
mb_strtolower(mb_substr($string, 1, mb_strlen($string, $charset), $charset), $charset); |
|
|
638 |
} |
|
|
639 |
|
|
|
640 |
return ucfirst(strtolower($string)); |
|
|
641 |
} |
|
|
642 |
} |
|
|
643 |
// and byte fallback |
|
|
644 |
else |
|
|
645 |
{ |
|
|
646 |
/** |
|
|
647 |
* Returns the length of a PHP variable. |
|
|
648 |
* |
|
|
649 |
* @param Twig_Environment $env A Twig_Environment instance |
|
|
650 |
* @param mixed $thing A PHP variable |
|
|
651 |
* |
|
|
652 |
* @return integer The length of the value |
|
|
653 |
*/ |
|
|
654 |
function twig_length_filter(Twig_Environment $env, $thing) |
|
|
655 |
{ |
|
|
656 |
return is_scalar($thing) ? strlen($thing) : count($thing); |
|
|
657 |
} |
|
|
658 |
|
|
|
659 |
/** |
|
|
660 |
* Returns a titlecased string. |
|
|
661 |
* |
|
|
662 |
* @param Twig_Environment $env A Twig_Environment instance |
|
|
663 |
* @param string $string A string |
|
|
664 |
* |
|
|
665 |
* @return string The titlecased string |
|
|
666 |
*/ |
|
|
667 |
function twig_title_string_filter(Twig_Environment $env, $string) |
|
|
668 |
{ |
|
|
669 |
return ucwords(strtolower($string)); |
|
|
670 |
} |
|
|
671 |
|
|
|
672 |
/** |
|
|
673 |
* Returns a capitalized string. |
|
|
674 |
* |
|
|
675 |
* @param Twig_Environment $env A Twig_Environment instance |
|
|
676 |
* @param string $string A string |
|
|
677 |
* |
|
|
678 |
* @return string The capitalized string |
|
|
679 |
*/ |
|
|
680 |
function twig_capitalize_string_filter(Twig_Environment $env, $string) |
|
|
681 |
{ |
|
|
682 |
return ucfirst(strtolower($string)); |
|
|
683 |
} |
|
|
684 |
} |
|
|
685 |
|
|
|
686 |
/* used internally */ |
|
|
687 |
function twig_ensure_traversable($seq) |
|
|
688 |
{ |
|
|
689 |
if (is_array($seq) || (is_object($seq) && $seq instanceof Traversable)) { |
|
|
690 |
return $seq; |
|
|
691 |
} else { |
|
|
692 |
return array(); |
|
|
693 |
} |
|
|
694 |
} |
|
|
695 |
|
|
|
696 |
/** |
|
|
697 |
* Checks that a variable points to the same memory address than another one. |
|
|
698 |
* |
|
|
699 |
* <pre> |
|
|
700 |
* {% if foo.attribute is sameas(false) %} |
|
|
701 |
* the foo attribute really is the ``false`` PHP value |
|
|
702 |
* {% endif %} |
|
|
703 |
* </pre> |
|
|
704 |
* |
|
|
705 |
* @param mixed $value A PHP variable |
|
|
706 |
* @param mixed $test The PHP variable to test against |
|
|
707 |
* |
|
|
708 |
* @return Boolean true if the values are the same, false otherwise |
|
|
709 |
*/ |
|
|
710 |
function twig_test_sameas($value, $test) |
|
|
711 |
{ |
|
|
712 |
return $value === $test; |
|
|
713 |
} |
|
|
714 |
|
|
|
715 |
/** |
|
|
716 |
* Checks that a variable is null. |
|
|
717 |
* |
|
|
718 |
* <pre> |
|
|
719 |
* {{ var is none }} |
|
|
720 |
* </pre> |
|
|
721 |
* |
|
|
722 |
* @param mixed $value a PHP variable. |
|
|
723 |
* |
|
|
724 |
* @return Boolean true if the value is null, false otherwise |
|
|
725 |
*/ |
|
|
726 |
function twig_test_none($value) |
|
|
727 |
{ |
|
|
728 |
return null === $value; |
|
|
729 |
} |
|
|
730 |
|
|
|
731 |
/** |
|
|
732 |
* Checks if a variable is divisible by a number. |
|
|
733 |
* |
|
|
734 |
* <pre> |
|
|
735 |
* {% if loop.index is divisibleby(3) %} |
|
|
736 |
* </pre> |
|
|
737 |
* |
|
|
738 |
* @param integer $value A PHP value |
|
|
739 |
* @param integer $num A number |
|
|
740 |
* |
|
|
741 |
* @return Boolean true if the value is divisible by the number, false otherwise |
|
|
742 |
*/ |
|
|
743 |
function twig_test_divisibleby($value, $num) |
|
|
744 |
{ |
|
|
745 |
return 0 == $value % $num; |
|
|
746 |
} |
|
|
747 |
|
|
|
748 |
/** |
|
|
749 |
* Checks if a number is even. |
|
|
750 |
* |
|
|
751 |
* <pre> |
|
|
752 |
* {{ var is even }} |
|
|
753 |
* </pre> |
|
|
754 |
* |
|
|
755 |
* @param integer $value An integer |
|
|
756 |
* |
|
|
757 |
* @return Boolean true if the value is even, false otherwise |
|
|
758 |
*/ |
|
|
759 |
function twig_test_even($value) |
|
|
760 |
{ |
|
|
761 |
return $value % 2 == 0; |
|
|
762 |
} |
|
|
763 |
|
|
|
764 |
/** |
|
|
765 |
* Checks if a number is odd. |
|
|
766 |
* |
|
|
767 |
* <pre> |
|
|
768 |
* {{ var is odd }} |
|
|
769 |
* </pre> |
|
|
770 |
* |
|
|
771 |
* @param integer $value An integer |
|
|
772 |
* |
|
|
773 |
* @return Boolean true if the value is odd, false otherwise |
|
|
774 |
*/ |
|
|
775 |
function twig_test_odd($value) |
|
|
776 |
{ |
|
|
777 |
return $value % 2 == 1; |
|
|
778 |
} |
|
|
779 |
|
|
|
780 |
/** |
|
|
781 |
* Checks if a variable is the exact same value as a constant. |
|
|
782 |
* |
|
|
783 |
* <pre> |
|
|
784 |
* {% if post.status is constant('Post::PUBLISHED') %} |
|
|
785 |
* the status attribute is exactly the same as Post::PUBLISHED |
|
|
786 |
* {% endif %} |
|
|
787 |
* </pre> |
|
|
788 |
* |
|
|
789 |
* @param mixed $value A PHP value |
|
|
790 |
* @param mixed $constant The constant to test against |
|
|
791 |
* |
|
|
792 |
* @return Boolean true if the value is the same as the constant, false otherwise |
|
|
793 |
*/ |
|
|
794 |
function twig_test_constant($value, $constant) |
|
|
795 |
{ |
|
|
796 |
return constant($constant) === $value; |
|
|
797 |
} |
|
|
798 |
|
|
|
799 |
/** |
|
|
800 |
* Checks if a variable is defined in the current context. |
|
|
801 |
* |
|
|
802 |
* <pre> |
|
|
803 |
* {# defined works with variable names #} |
|
|
804 |
* {% if foo is defined %} |
|
|
805 |
* {# ... #} |
|
|
806 |
* {% endif %} |
|
|
807 |
* </pre> |
|
|
808 |
* |
|
|
809 |
* @param mixed $name A PHP variable |
|
|
810 |
* @param array $context The current context |
|
|
811 |
* |
|
|
812 |
* @return Boolean true if the value is defined, false otherwise |
|
|
813 |
*/ |
|
|
814 |
function twig_test_defined($name, $context) |
|
|
815 |
{ |
|
|
816 |
return array_key_exists($name, $context); |
|
|
817 |
} |
|
|
818 |
|
|
|
819 |
/** |
|
|
820 |
* Checks if a variable is empty. |
|
|
821 |
* |
|
|
822 |
* <pre> |
|
|
823 |
* {# evaluates to true if the foo variable is null, false, or the empty string #} |
|
|
824 |
* {% if foo is empty %} |
|
|
825 |
* {# ... #} |
|
|
826 |
* {% endif %} |
|
|
827 |
* </pre> |
|
|
828 |
* |
|
|
829 |
* @param mixed $value A PHP variable |
|
|
830 |
* |
|
|
831 |
* @return Boolean true if the value is empty, false otherwise |
|
|
832 |
*/ |
|
|
833 |
function twig_test_empty($value) |
|
|
834 |
{ |
|
|
835 |
return false === $value || (empty($value) && '0' != $value); |
|
|
836 |
} |