Completed
Push — master ( 662165...7658f4 )
by WEBEWEB
02:47
created

bootstrapScriptFilter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-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\BootstrapBundle\Twig\Extension;
13
14
use Twig_SimpleFilter;
15
use WBW\Bundle\BootstrapBundle\Twig\Extension\Component\GlyphiconTwigExtension;
16
use WBW\Bundle\BootstrapBundle\Twig\Extension\Plugin\FontAwesomeTwigExtension;
17
use WBW\Bundle\BootstrapBundle\Twig\Extension\Plugin\MaterialDesignIconicFontTwigExtension;
18
use WBW\Bundle\BootstrapBundle\Twig\Extension\Plugin\MeteoconsTwigExtension;
19
20
/**
21
 * Bootstrap renderer Twig extension.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension
25
 */
26
class BootstrapRendererTwigExtension extends AbstractBootstrapTwigExtension {
27
28
    /**
29
     * Service name.
30
     *
31
     * @var string
32
     */
33
    const SERVICE_NAME = "webeweb.bootstrapbundle.twig.extension.renderer";
34
35
    /**
36
     * Constructor.
37
     */
38
    public function __construct() {
39
        parent::__construct();
40
    }
41
42
    /**
43
     * Displays a Bootstrap script.
44
     *
45
     * @param string $content The content.
46
     */
47
    public function bootstrapScriptFilter($content) {
48
49
        // Initialize the attributes.
50
        $attributes = [];
51
52
        $attributes["type"] = "text/javascript";
53
54
        // Initialize the parameters.
55
        $innerHTML = null !== $content ? "\n" . $content . "\n" : "";
56
57
        // Return the HTML.
58
        return self::bootstrapHTMLElement("script", $innerHTML, $attributes);
59
    }
60
61
    /**
62
     * Get the Twig filters.
63
     *
64
     * @return Twig_SimpleFilter[] Returns the Twig filters.
65
     */
66
    public function getFilters() {
67
        return [
68
            new Twig_SimpleFilter("bootstrapScript", [$this, "bootstrapScriptFilter"], ["is_safe" => ["html"]]),
69
        ];
70
    }
71
72
    /**
73
     * Render an icon.
74
     *
75
     * @param string $name The icon name.
76
     * @param string $style The icon style.
77
     * @return string Returns a rendered icon.
78
     */
79
    public static function renderIcon($name, $style = null) {
80
81
        // Determines the handler.
82
        $handler = explode(":", $name);
83
84
        // Get and check the parse count.
85
        $parseNb = count($handler);
86
        if ($parseNb < 1 || 2 < $parseNb) {
87
            return "";
88
        }
89
        if (1 === count($handler)) {
90
            $handler = ["g", $name];
91
        }
92
93
        // Initialize the output.
94
        $output = "";
95
96
        // Swith into handler.
97
        switch ($handler[0]) {
98
99
            case "b": // Bootstrap
100
            case "g": // Glyphicon
101
                $output = (new GlyphiconTwigExtension())->renderIcon($handler[1], $style);
102
                break;
103
104
            case "fa": // Font Awesome
105
                $output = (new FontAwesomeTwigExtension())->renderIcon($handler[1], $style);
106
                break;
107
108
            case "mc": // Meteocons
109
                $output = (new MeteoconsTwigExtension())->renderIcon($handler[1], $style);
110
                break;
111
112
            case "zmdi": // Material Design Iconic Font
113
                $output = (new MaterialDesignIconicFontTwigExtension())->renderIcon($handler[1], $style);
114
                break;
115
        }
116
117
        // Return the output.
118
        return $output;
119
    }
120
121
}
122