Completed
Push — master ( addd3e...e24f70 )
by WEBEWEB
02:21
created

RendererTwigExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\CoreBundle\Twig\Extension;
13
14
use Twig_Environment;
15
use Twig_SimpleFilter;
16
use WBW\Bundle\CoreBundle\Twig\Extension\Plugin\FontAwesomeTwigExtension;
17
use WBW\Bundle\CoreBundle\Twig\Extension\Plugin\MaterialDesignIconicFontTwigExtension;
18
use WBW\Bundle\CoreBundle\Twig\Extension\Plugin\MeteoconsTwigExtension;
19
20
/**
21
 * Renderer Twig extension.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\CoreBundle\Twig\Extension
25
 */
26
class RendererTwigExtension extends AbstractTwigExtension {
27
28
    /**
29
     * Service name.
30
     *
31
     * @var string
32
     */
33
    const SERVICE_NAME = "webeweb.core.twig.extension.renderer";
34
35
    /**
36
     * Displays a script.
37
     *
38
     * @param string $content The content.
39
     * @return string Returns a script.
40
     */
41
    public function coreScriptFilter($content) {
42
43
        $attributes = [];
44
45
        $attributes["type"] = "text/javascript";
46
47
        $innerHTML = null !== $content ? implode("", ["\n", $content, "\n"]) : "";
48
49
        return static::coreHTMLElement("script", $innerHTML, $attributes);
50
    }
51
52
    /**
53
     * Get the Twig filters.
54
     *
55
     * @return Twig_SimpleFilter[] Returns the Twig filters.
56
     */
57
    public function getFilters() {
58
        return [
59
            new Twig_SimpleFilter("coreScript", [$this, "coreScriptFilter"], ["is_safe" => ["html"]]),
60
        ];
61
    }
62
63
    /**
64
     * Render an icon.
65
     *
66
     * @param Twig_Environment $twigEnvironment The twig environment.
67
     * @param string $name The name.
68
     * @param string|null $style The style.
69
     * @return string Returns the rendered icon.
70
     */
71
    public static function renderIcon(Twig_Environment $twigEnvironment, $name, $style = null) {
72
73
        $handler = explode(":", $name);
74
        if (2 !== count($handler)) {
75
            return "";
76
        }
77
78
        $output = "";
79
80
        switch ($handler[0]) {
81
82
            case "fa": // Font Awesome
83
                $output = (new FontAwesomeTwigExtension($twigEnvironment))->renderIcon($handler[1], $style);
84
                break;
85
86
            case "mc": // Meteocons
87
                $output = (new MeteoconsTwigExtension($twigEnvironment))->renderIcon($handler[1], $style);
88
                break;
89
90
            case "zmdi": // Material Design Iconic Font
91
                $output = (new MaterialDesignIconicFontTwigExtension($twigEnvironment))->renderIcon($handler[1], $style);
92
                break;
93
        }
94
95
        return $output;
96
    }
97
}
98