Completed
Push — master ( e1137c...71fb49 )
by WEBEWEB
01:41
created

RendererTwigExtension::renderIcon()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 9.0808
c 0
b 0
f 0
cc 5
nc 5
nop 3
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
     * Constructor.
37
     *
38
     * @param Twig_Environment $twigEnvironment The twig environment.
39
     */
40
    public function __construct(Twig_Environment $twigEnvironment) {
41
        parent::__construct($twigEnvironment);
42
    }
43
44
    /**
45
     * Displays a script.
46
     *
47
     * @param string $content The content.
48
     * @return string Returns a script.
49
     */
50
    public function coreScriptFilter($content) {
51
52
        // Initialize the attributes.
53
        $attributes = [];
54
55
        $attributes["type"] = "text/javascript";
56
57
        // Initialize the parameters.
58
        $innerHTML = null !== $content ? implode("", ["\n", $content, "\n"]) : "";
59
60
        // Return the HTML.
61
        return static::coreHTMLElement("script", $innerHTML, $attributes);
62
    }
63
64
    /**
65
     * Get the Twig filters.
66
     *
67
     * @return Twig_SimpleFilter[] Returns the Twig filters.
68
     */
69
    public function getFilters() {
70
        return [
71
            new Twig_SimpleFilter("coreScript", [$this, "coreScriptFilter"], ["is_safe" => ["html"]]),
72
        ];
73
    }
74
75
    /**
76
     * Render an icon.
77
     *
78
     * @param Twig_Environment $twigEnvironment The twig environment.
79
     * @param string $name The name.
80
     * @param string $style The style.
81
     * @return string Returns the rendered icon.
82
     */
83
    public static function renderIcon(Twig_Environment $twigEnvironment, $name, $style = null) {
84
85
        // Determines the handler.
86
        $handler = explode(":", $name);
87
88
        // Get and check the parse count.
89
        $parseNb = count($handler);
90
        if (2 !== $parseNb) {
91
            return "";
92
        }
93
94
        // Initialize the output.
95
        $output = "";
96
97
        // Swith into handler.
98
        switch ($handler[0]) {
99
100
            case "fa": // Font Awesome
101
                $output = (new FontAwesomeTwigExtension($twigEnvironment))->renderIcon($handler[1], $style);
102
                break;
103
104
            case "mc": // Meteocons
105
                $output = (new MeteoconsTwigExtension($twigEnvironment))->renderIcon($handler[1], $style);
106
                break;
107
108
            case "zmdi": // Material Design Iconic Font
109
                $output = (new MaterialDesignIconicFontTwigExtension($twigEnvironment))->renderIcon($handler[1], $style);
110
                break;
111
        }
112
113
        // Return the output.
114
        return $output;
115
    }
116
117
}
118