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