Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / tests / modules / twig_extension_test / src / TwigExtension / TestExtension.php
1 <?php
2
3 namespace Drupal\twig_extension_test\TwigExtension;
4
5 /**
6  * A test Twig extension that adds a custom function and a custom filter.
7  */
8 class TestExtension extends \Twig_Extension {
9
10   /**
11    * Generates a list of all Twig functions that this extension defines.
12    *
13    * @return array
14    *   A key/value array that defines custom Twig functions. The key denotes the
15    *   function name used in the tag, e.g.:
16    *   @code
17    *   {{ testfunc() }}
18    *   @endcode
19    *
20    *   The value is a standard PHP callback that defines what the function does.
21    */
22   public function getFunctions() {
23     return [
24       'testfunc' => new \Twig_Function_Function(['Drupal\twig_extension_test\TwigExtension\TestExtension', 'testFunction']),
25     ];
26   }
27
28   /**
29    * Generates a list of all Twig filters that this extension defines.
30    *
31    * @return array
32    *   A key/value array that defines custom Twig filters. The key denotes the
33    *   filter name used in the tag, e.g.:
34    *   @code
35    *   {{ foo|testfilter }}
36    *   @endcode
37    *
38    *   The value is a standard PHP callback that defines what the filter does.
39    */
40   public function getFilters() {
41     return [
42       'testfilter' => new \Twig_Filter_Function(['Drupal\twig_extension_test\TwigExtension\TestExtension', 'testFilter']),
43     ];
44   }
45
46   /**
47    * Gets a unique identifier for this Twig extension.
48    *
49    * @return string
50    *   A unique identifier for this Twig extension.
51    */
52   public function getName() {
53     return 'twig_extension_test.test_extension';
54   }
55
56   /**
57    * Outputs either an uppercase or lowercase test phrase.
58    *
59    * The function generates either an uppercase or lowercase version of the
60    * phrase "The quick brown fox jumps over the lazy dog 123.", depending on
61    * whether or not the $upperCase parameter evaluates to TRUE. If $upperCase
62    * evaluates to TRUE, the result will be uppercase, and if it evaluates to
63    * FALSE, the result will be lowercase.
64    *
65    * @param bool $upperCase
66    *   (optional) Whether the result is uppercase (true) or lowercase (false).
67    *
68    * @return string
69    *   The generated string.
70    *
71    * @see \Drupal\system\Tests\Theme\TwigExtensionTest::testTwigExtensionFunction()
72    */
73   public static function testFunction($upperCase = FALSE) {
74     $string = "The quick brown box jumps over the lazy dog 123.";
75     if ($upperCase == TRUE) {
76       return strtoupper($string);
77     }
78     else {
79       return strtolower($string);
80     }
81   }
82
83   /**
84    * Replaces all instances of "animal" in a string with "plant".
85    *
86    * @param string $string
87    *   The string to be filtered.
88    *
89    * @return string
90    *   The filtered string.
91    *
92    * @see \Drupal\system\Tests\Theme\TwigExtensionTest::testTwigExtensionFilter()
93    */
94   public static function testFilter($string) {
95     return str_replace(['animal'], ['plant'], $string);
96   }
97
98 }