99 * file that was distributed with this source code.
1010 */
1111
12- namespace Twig \Extra \Html {
12+ namespace Twig \Extra \Html ;
13+
1314use Symfony \Component \Mime \MimeTypes ;
15+ use Twig \Error \RuntimeError ;
1416use Twig \Extension \AbstractExtension ;
1517use Twig \TwigFilter ;
1618use Twig \TwigFunction ;
@@ -34,7 +36,7 @@ public function getFilters(): array
3436 public function getFunctions (): array
3537 {
3638 return [
37- new TwigFunction ('html_classes ' , ' twig_html_classes ' ),
39+ new TwigFunction ('html_classes ' , [ self ::class, ' htmlClasses ' ] ),
3840 ];
3941 }
4042
@@ -45,6 +47,8 @@ public function getFunctions(): array
4547 * be done before calling this filter.
4648 *
4749 * @return string The generated data URI
50+ *
51+ * @internal
4852 */
4953 public function dataUri (string $ data , string $ mime = null , array $ parameters = []): string
5054 {
@@ -79,33 +83,31 @@ public function dataUri(string $data, string $mime = null, array $parameters = [
7983
8084 return $ repr ;
8185 }
82- }
83- }
84-
85- namespace {
86- use Twig \Error \RuntimeError ;
8786
88- function twig_html_classes (...$ args ): string
89- {
90- $ classes = [];
91- foreach ($ args as $ i => $ arg ) {
92- if (\is_string ($ arg )) {
93- $ classes [] = $ arg ;
94- } elseif (\is_array ($ arg )) {
95- foreach ($ arg as $ class => $ condition ) {
96- if (!\is_string ($ class )) {
97- throw new RuntimeError (sprintf ('The html_classes function argument %d (key %d) should be a string, got "%s". ' , $ i , $ class , \gettype ($ class )));
98- }
99- if (!$ condition ) {
100- continue ;
87+ /**
88+ * @internal
89+ */
90+ public static function htmlClasses (...$ args ): string
91+ {
92+ $ classes = [];
93+ foreach ($ args as $ i => $ arg ) {
94+ if (\is_string ($ arg )) {
95+ $ classes [] = $ arg ;
96+ } elseif (\is_array ($ arg )) {
97+ foreach ($ arg as $ class => $ condition ) {
98+ if (!\is_string ($ class )) {
99+ throw new RuntimeError (sprintf ('The html_classes function argument %d (key %d) should be a string, got "%s". ' , $ i , $ class , \gettype ($ class )));
100+ }
101+ if (!$ condition ) {
102+ continue ;
103+ }
104+ $ classes [] = $ class ;
101105 }
102- $ classes [] = $ class ;
106+ } else {
107+ throw new RuntimeError (sprintf ('The html_classes function argument %d should be either a string or an array, got "%s". ' , $ i , \gettype ($ arg )));
103108 }
104- } else {
105- throw new RuntimeError (sprintf ('The html_classes function argument %d should be either a string or an array, got "%s". ' , $ i , \gettype ($ arg )));
106109 }
107- }
108110
109- return implode (' ' , array_unique ($ classes ));
110- }
111+ return implode (' ' , array_unique ($ classes ));
112+ }
111113}
0 commit comments