Completed
Push — feature/map-redesign ( 000b6a...c361ac )
by Vladimir
26:33
created

Ionicon::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace BZIon\Twig;
4
5
class Ionicon
6
{
7
    private static $svgPath = '/assets/svg';
8
    private static $svgIcons = [];
9
10
    public function __invoke(\Twig_Environment $env, $name, $height = null, $width = null)
11
    {
12
        $svg = DOC_ROOT . self::$svgPath . '/' . $name . '.svg';
13
14
        if (isset(self::$svgIcons[$name])) {
15
            return self::$svgIcons[$name];
16
        }
17
18
        if (file_exists($svg)) {
19
            $svgDoc = simplexml_load_string(file_get_contents($svg));
20
21
            if ($svgDoc === false) {
22
                return '';
23
            }
24
25
            $svgDoc->addAttribute('class', 'ionicon');
26
            $height && $svgDoc->addAttribute('height', $height);
27
            $width  && $svgDoc->addAttribute('width', $width);
28
29
            self::$svgIcons[$name] = $svgDoc->asXML();
30
31
            return self::$svgIcons[$name];
32
        }
33
34
        return '';
35
    }
36
37
    public static function get()
38
    {
39
        return new \Twig_SimpleFunction('ionicon', new self(), [
40
            'needs_environment' => true,
41
            'is_safe' => [
42
                'evaluate' => true
43
            ]
44
        ]);
45
    }
46
}
47