RendererTwigExtension::renderIcon()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
c 0
b 0
f 0
rs 9.1928
cc 5
nc 8
nop 3
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\Environment;
15
use Twig\TwigFilter;
16
use WBW\Bundle\BootstrapBundle\Twig\Extension\Component\GlyphiconTwigExtension;
17
use WBW\Bundle\CoreBundle\Twig\Extension\RendererTwigExtension as BaseTwigExtension;
18
19
/**
20
 * Renderer Twig extension.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension
24
 */
25
class RendererTwigExtension extends BaseTwigExtension {
26
27
    /**
28
     * Service name.
29
     *
30
     * @var string
31
     */
32
    const SERVICE_NAME = "wbw.bootstrap.twig.extension.renderer";
33
34
    /**
35
     * Get the Twig filters.
36
     *
37
     * @return TwigFilter[] Returns the Twig filters.
38
     */
39
    public function getFilters(): array {
40
        return [];
41
    }
42
43
    /**
44
     * Render an icon.
45
     *
46
     * @param Environment $twigEnvironment The twig environment.
47
     * @param string $name The icon name.
48
     * @param string|null $style The icon style.
49
     * @return string Returns a rendered icon.
50
     */
51
    public static function renderIcon(Environment $twigEnvironment, string $name, ?string $style = null): string {
52
53
        // Determines the handler.
54
        $handler = explode(":", $name);
55
        if (1 === count($handler)) {
56
            array_unshift($handler, "g");
57
        }
58
        if (2 !== count($handler)) {
59
            return "";
60
        }
61
62
        // Swith into handler.
63
        switch ($handler[0]) {
64
65
            case "b": // Bootstrap
66
            case "g": // Glyphicon
67
                $output = (new GlyphiconTwigExtension($twigEnvironment))->renderIcon($handler[1], $style);
68
                break;
69
70
            default:
71
                $output = parent::renderIcon($twigEnvironment, $name, $style);
72
                break;
73
        }
74
75
        return $output;
76
    }
77
}
78