Completed
Push — master ( a6151b...66e545 )
by WEBEWEB
01:50
created

FontAwesomePluginTwigExtension::renderIcon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
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\Plugin;
13
14
use Twig_SimpleFilter;
15
use Twig_SimpleFunction;
16
use WBW\Bundle\BootstrapBundle\Twig\Extension\IconRendererTwigExtensionInterface;
17
use WBW\Library\Core\Utility\Argument\ArrayUtility;
18
use WBW\Library\Core\Utility\Argument\StringUtility;
19
20
/**
21
 * Font Awesome plugin Twig extension.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Plugin
25
 */
26
class FontAwesomePluginTwigExtension extends AbstractPluginTwigExtension implements IconRendererTwigExtensionInterface {
27
28
    /**
29
     * Service name.
30
     *
31
     * @var string
32
     */
33
    const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.twig.extension.plugin.fontawesome";
34
35
    /**
36
     * Constructor.
37
     */
38
    public function __construct() {
39
        parent::__construct();
40
    }
41
42
    /**
43
     * Displays a Font Awesome icon.
44
     *
45
     * @param array $args The arguments.
46
     * @return Returns a Font Awesome icon.
47
     */
48
    public function fontAwesomeIconFunction(array $args = []) {
49
        return $this->fontAwesomeIcon(ArrayUtility::get($args, "font"), ArrayUtility::get($args, "name", "home"), ArrayUtility::get($args, "size"), ArrayUtility::get($args, "fixedWidth", false), ArrayUtility::get($args, "bordered", false), ArrayUtility::get($args, "pull"), ArrayUtility::get($args, "animated"), ArrayUtility::get($args, "style"));
50
    }
51
52
    /**
53
     * Displays a Font Awesome list.
54
     *
55
     * @param array|string $items The list items.
56
     * @return string Returns the Font Awesome list.
57
     */
58
    public function fontAwesomeListFilter($items) {
59
60
        // Initialize the template.
61
        $template = "<ul class=\"fa-ul\">%innerHTML%</ul>";
62
63
        // Initialize the parameters.
64
        $innerHTML = true === is_array($items) ? implode("\n", $items) : $items;
65
66
        // Return the HTML.
67
        return StringUtility::replace($template, ["%innerHTML%"], [$innerHTML]);
68
    }
69
70
    /**
71
     * Displays a Font Awesome list icon.
72
     *
73
     * @param string $icon The Font Awesome icon.
74
     * @param string $content The content.
75
     * @return string Returns the Font Awesome list icon.
76
     */
77
    public function fontAwesomeListIconFilter($icon, $content) {
78
79
        // Initialize the template.
80
        $template = "<li><span class=\"fa-li\">%glyphicon%</span>%innerHTML%</li>";
81
82
        // Initialize the parameters.
83
        $glyphicon = null !== $icon ? $icon : "";
84
        $innerHTML = null !== $content ? $content : "";
85
86
        // Return the HTML.
87
        return StringUtility::replace($template, ["%glyphicon%", "%innerHTML%"], [$glyphicon, $innerHTML]);
88
    }
89
90
    /**
91
     * Get the Twig filters.
92
     *
93
     * @return Twig_SimpleFilter[] Returns the Twig filters.
94
     */
95
    public function getFilters() {
96
        return [
97
            new Twig_SimpleFilter("fontAwesomeList", [$this, "fontAwesomeListFilter"], ["is_safe" => ["html"]]),
98
            new Twig_SimpleFilter("fontAwesomeListIcon", [$this, "fontAwesomeListIconFilter"], ["is_safe" => ["html"]]),
99
        ];
100
    }
101
102
    /**
103
     * Get the Twig functions.
104
     *
105
     * @return array Returns the Twig functions.
106
     */
107
    public function getFunctions() {
108
        return [
109
            new Twig_SimpleFunction("fontAwesomeIcon", [$this, "fontAwesomeIconFunction"], ["is_safe" => ["html"]]),
110
        ];
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function renderIcon($name, $style) {
117
        return $this->fontAwesomeIconFunction(["name" => $name, "style" => $style]);
118
    }
119
120
}
121