RendererTwigExtension::renderIcon()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 4
nc 6
nop 3
1
<?php
2
3
/*
4
 * This file is part of the adminbsb-material-design-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\AdminBSBBundle\Twig\Extension;
13
14
use Twig\Environment;
15
use Twig\TwigFilter;
16
use WBW\Bundle\AdminBSBBundle\Twig\Extension\UI\IconTwigExtension;
17
use WBW\Bundle\BootstrapBundle\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.adminbsb.twig.extension.renderer";
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param Environment $twigEnvironment The twig environment.
38
     */
39
    public function __construct(Environment $twigEnvironment) {
40
        parent::__construct($twigEnvironment);
41
    }
42
43
    /**
44
     * Get the Twig filters.
45
     *
46
     * @return TwigFilter[] Returns the Twig filters.
47
     */
48
    public function getFilters(): array {
49
        return [];
50
    }
51
52
    /**
53
     * Render an icon.
54
     *
55
     * @param Environment $twigEnvironment The twig environment.
56
     * @param string $name The icon name.
57
     * @param string $style The icon style.
58
     * @return string Returns a rendered icon.
59
     */
60
    public static function renderIcon(Environment $twigEnvironment, string $name, string $style = null): string {
61
62
        // Determines the handler.
63
        $handler = explode(":", $name);
64
        if (1 === count($handler)) {
65
            array_unshift($handler, "mi");
66
        }
67
        if (2 !== count($handler)) {
68
            return "";
69
        }
70
71
        // Swith into handler.
72
        switch ($handler[0]) {
73
74
            case "mi": // Material design
75
                $output = (new IconTwigExtension($twigEnvironment))->renderIcon($handler[1], $style);
76
                break;
77
78
            default:
79
                $output = parent::renderIcon($twigEnvironment, $name, $style);
80
                break;
81
        }
82
83
        // Return the output.
84
        return $output;
85
    }
86
}
87