2.61KiB; PHP | 2020-08-26 15:35:48+02 | SLOC 82
1
<?php␣
2
3
/**␣
4
 * SVG PHP Helper␣
5
 *␣
6
 * Based on https://github.com/chteuchteu/MaterialDesignIcons-PHP␣
7
 *␣
8
 * @author  Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>␣
9
 * @license MIT, GPLv2␣
10
 */␣
11
12
namespace dokuwiki\template\bootstrap3;␣
13
14
class SVG␣
15
{␣
16
17
    public static $iconsPath;␣
18
19
    public static $defaultAttributes = array();␣
20
21
    /**␣
22
     * Add icon␣
23
     *␣
24
     * @param string $icon  Icon name or full path␣
25
     * @param string $class Icon Class␣
26
     * @param int    $size  Icon size␣
27
     * @param array  $attrs Icon attributes␣
28
     *␣
29
     * @return string␣
30
     */␣
31 6
    public static function icon($icon, $class = null, $size = 24, $attrs = array())␣
32
    {␣
33
        // Find the icon, ensure it exists␣
34
        if (file_exists($icon)) {␣
35
            $file_path = $icon;␣
36 1
        } else {␣
ElseExpression The method icon uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. (kritika/PHPMD) Filter like this
37
            $file_path = self::$iconsPath . $icon . '.svg';␣
38
        }␣
39
40
        if (!is_file($file_path)) {␣
41
            msg(sprintf('Unrecognized icon "%s" (svg file "%s" does not exist).', $icon, $file_path), -1);␣
42
            return false;␣
43
        }␣
44
45
        // Read the file␣
46
        $svg = file_get_contents($file_path);␣
47
48
        // Only keep the <path d="..." /> part␣
49
        // Old REGEX: (<path d=".+" \/>)␣
50
        if (preg_match('/(<path\b([\s\S]*?)\/>)/', $svg, $matches) !== 1) {␣
51
            msg(sprintf('"%s" could not be recognized as an icon file', $file_path), -1);␣
52
            return false;␣
53
        }␣
54
55
        $svg = $matches[1];␣
56
57
        // Add some (clean) attributes␣
58
        $attributes = array_merge(␣
59
            array(␣
60
                'viewBox' => '0 0 24 24',␣
61
                'xmlns'   => 'http://www.w3.org/2000/svg',␣
62
                'width'   => $size,␣
63
                'height'  => $size,␣
64
                'role'    => 'presentation',␣
65
            ),␣
66
            self::$defaultAttributes,␣
67
            $attrs␣
68
        );␣
69
70
        if ($class !== null) {␣
71
            $attributes['class'] = $class;␣
72
        }␣
73
74
        // Remove possibly empty-ish attributes (self::$defaultAttributes or $attrs may contain null values)␣
75
        $attributes = array_filter($attributes);␣
76
77
        return sprintf(␣
78
            '<svg %s>%s</svg>',␣
79
            self::attributes($attributes),␣
80
            $svg␣
81
        );␣
82
    }␣
83
84
    /**␣
85
     * Turns a 1-dimension array into an HTML-ready attributes set.␣
86
     */␣
87
    private static function attributes($attrs = array())␣
88
    {␣
89
        return implode(' ', array_map(␣
90
            function ($val, $key) {␣
91
                return $key . '="' . htmlspecialchars($val) . '"';␣
92
            },␣
93
            $attrs,␣
94
            array_keys($attrs)␣
95
        ));␣
96
    }␣
97
}␣