Completed
Push — master ( 341a7d...fd15d4 )
by WEBEWEB
02:17
created

BootstrapRendererTwigExtension::renderIcon()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 37
rs 8.0835
c 1
b 0
f 0
cc 8
nc 11
nop 2
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 WBW\Bundle\BootstrapBundle\Twig\Extension\Component\GlyphiconComponentTwigExtension;
15
use WBW\Bundle\BootstrapBundle\Twig\Extension\Plugin\FontAwesomePluginTwigExtension;
16
use WBW\Bundle\BootstrapBundle\Twig\Extension\Plugin\MaterialDesignIconicFontPluginTwigExtension;
17
18
/**
19
 * Bootstrap renderer Twig extension.
20
 *
21
 * @author webeweb <https://github.com/webeweb/>
22
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension
23
 */
24
class BootstrapRendererTwigExtension {
25
26
    /**
27
     * Render an icon.
28
     *
29
     * @param string $name The icon name.
30
     * @param string $style The icon style.
31
     * @return string Returns a rendered icon.
32
     */
33
    public static function renderIcon($name, $style = null) {
34
35
        // Determines the handler.
36
        $handler = explode(":", $name);
37
38
        // Get and check the parse count.
39
        $parseNb = count($handler);
40
        if ($parseNb < 1 || 2 < $parseNb) {
41
            return "";
42
        }
43
        if (1 === count($handler)) {
44
            $handler = ["g", $name];
45
        }
46
47
        // Initialize the output.
48
        $output = "";
49
50
        // Swith into handler.
51
        switch ($handler[0]) {
52
53
            case "b": // Bootstrap
54
            case "g": // Glyphicon
55
                $output = (new GlyphiconComponentTwigExtension())->renderIcon($handler[1], $style);
56
                break;
57
58
            case "fa": // Font Awesome
59
                $output = (new FontAwesomePluginTwigExtension())->renderIcon($handler[1], $style);
60
                break;
61
62
            case "zmdi": // Material Design Iconic Font
63
                $output = (new MaterialDesignIconicFontPluginTwigExtension())->renderIcon($handler[1], $style);
64
                break;
65
        }
66
67
        // Return the output.
68
        return $output;
69
    }
70
71
}
72