|
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\Component; |
|
13
|
|
|
|
|
14
|
|
|
use Twig\TwigFilter; |
|
15
|
|
|
use Twig\TwigFunction; |
|
16
|
|
|
use WBW\Bundle\BootstrapBundle\Badge\BadgeFactory; |
|
17
|
|
|
use WBW\Library\Core\Argument\Helper\StringHelper; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Badge component Twig extension. |
|
21
|
|
|
* |
|
22
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
23
|
|
|
* @package WBW\Bundle\BootstrapBundle\Twig\Extension\Component |
|
24
|
|
|
* @link https://getbootstrap.com/docs/3.3/components/#badges |
|
25
|
|
|
*/ |
|
26
|
|
|
class BadgeTwigExtension extends AbstractBadgeTwigExtension { |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Service name. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
const SERVICE_NAME = "wbw.bootstrap.twig.extension.component.badge"; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Displays a Bootstrap badge "danger". |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $args The arguments. |
|
39
|
|
|
* @return string Returns the Bootstrap badge "danger". |
|
40
|
|
|
*/ |
|
41
|
|
|
public function bootstrapBadgeDangerFunction(array $args = []): string { |
|
42
|
|
|
return $this->bootstrapBadge(BadgeFactory::parseDangerBadge($args)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Displays a Bootstrap badge "dark". |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $args The arguments. |
|
49
|
|
|
* @return string Returns the Bootstrap badge "dark". |
|
50
|
|
|
*/ |
|
51
|
|
|
public function bootstrapBadgeDarkFunction(array $args = []): string { |
|
52
|
|
|
return $this->bootstrapBadge(BadgeFactory::parseDarkBadge($args)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Displays a Bootstrap badge. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $args The arguments. |
|
59
|
|
|
* @return string Returns the Bootstrap badge. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function bootstrapBadgeFunction(array $args = []): string { |
|
62
|
|
|
return $this->bootstrapBadge(BadgeFactory::parseDefaultBadge($args)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Displays a Bootstrap badge "info". |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $args The arguments. |
|
69
|
|
|
* @return string Returns the Bootstrap badge "info". |
|
70
|
|
|
*/ |
|
71
|
|
|
public function bootstrapBadgeInfoFunction(array $args = []): string { |
|
72
|
|
|
return $this->bootstrapBadge(BadgeFactory::parseInfoBadge($args)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Displays a Bootstrap badge "light". |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $args The arguments. |
|
79
|
|
|
* @return string Returns the Bootstrap badge "light". |
|
80
|
|
|
*/ |
|
81
|
|
|
public function bootstrapBadgeLightFunction(array $args = []): string { |
|
82
|
|
|
return $this->bootstrapBadge(BadgeFactory::parseLightBadge($args)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Transforms a Bootstrap badge into an anchor. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $button The button. |
|
89
|
|
|
* @param string $href The href attribute. |
|
90
|
|
|
* @param string|null $target The target attribute. |
|
91
|
|
|
* @return string Returns the Bootstrap badge transformed into an anchor. |
|
92
|
|
|
*/ |
|
93
|
|
|
public function bootstrapBadgeLinkFilter(string $button, string $href = self::DEFAULT_HREF, string $target = null): string { |
|
94
|
|
|
|
|
95
|
|
|
$searches = ["<span", "</span>", "class="]; |
|
96
|
|
|
$replaces = ["<a", "</a>", StringHelper::parseArray(["href" => $href, "target" => $target]) . " class="]; |
|
97
|
|
|
|
|
98
|
|
|
return str_replace($searches, $replaces, $button); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Displays a Bootstrap badge "primary". |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $args The arguments. |
|
105
|
|
|
* @return string Returns the Bootstrap badge "primary". |
|
106
|
|
|
*/ |
|
107
|
|
|
public function bootstrapBadgePrimaryFunction(array $args = []): string { |
|
108
|
|
|
return $this->bootstrapBadge(BadgeFactory::parsePrimaryBadge($args)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Displays a Bootstrap badge "secondary". |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $args The arguments. |
|
115
|
|
|
* @return string Returns the Bootstrap badge "secondary". |
|
116
|
|
|
*/ |
|
117
|
|
|
public function bootstrapBadgeSecondaryFunction(array $args = []): string { |
|
118
|
|
|
return $this->bootstrapBadge(BadgeFactory::parseSecondaryBadge($args)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Displays a Bootstrap badge "success". |
|
123
|
|
|
* |
|
124
|
|
|
* @param array $args The arguments. |
|
125
|
|
|
* @return string Returns the Bootstrap badge "success". |
|
126
|
|
|
*/ |
|
127
|
|
|
public function bootstrapBadgeSuccessFunction(array $args = []): string { |
|
128
|
|
|
return $this->bootstrapBadge(BadgeFactory::parseSuccessBadge($args)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Displays a Bootstrap badge "warning". |
|
133
|
|
|
* |
|
134
|
|
|
* @param array $args The arguments. |
|
135
|
|
|
* @return string Returns the Bootstrap badge "warning". |
|
136
|
|
|
*/ |
|
137
|
|
|
public function bootstrapBadgeWarningFunction(array $args = []): string { |
|
138
|
|
|
return $this->bootstrapBadge(BadgeFactory::parseWarningBadge($args)); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get the Twig filters. |
|
143
|
|
|
* |
|
144
|
|
|
* @return TwigFilter[] Returns the Twig filters. |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getFilters(): array { |
|
147
|
|
|
return [ |
|
148
|
|
|
new TwigFilter("bootstrapBadgeLink", [$this, "bootstrapBadgeLinkFilter"], ["is_safe" => ["html"]]), |
|
149
|
|
|
new TwigFilter("bsBadgeLink", [$this, "bootstrapBadgeLinkFilter"], ["is_safe" => ["html"]]), |
|
150
|
|
|
]; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Get the Twig functions. |
|
155
|
|
|
* |
|
156
|
|
|
* @return TwigFunction[] Returns the Twig functions. |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getFunctions(): array { |
|
159
|
|
|
if (3 === $this->getVersion()) { |
|
160
|
|
|
return $this->getFunctions3(); |
|
161
|
|
|
} |
|
162
|
|
|
return $this->getFunctions4(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Get the Twig functions. |
|
167
|
|
|
* |
|
168
|
|
|
* @return TwigFunction[] Returns the Twig functions. |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getFunctions3(): array { |
|
171
|
|
|
return [ |
|
172
|
|
|
new TwigFunction("bootstrapBadge", [$this, "bootstrapBadgeFunction"], ["is_safe" => ["html"]]), |
|
173
|
|
|
new TwigFunction("bsBadge", [$this, "bootstrapBadgeFunction"], ["is_safe" => ["html"]]), |
|
174
|
|
|
]; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Get the Twig functions. |
|
179
|
|
|
* |
|
180
|
|
|
* @return TwigFunction[] Returns the Twig functions. |
|
181
|
|
|
*/ |
|
182
|
|
|
public function getFunctions4(): array { |
|
183
|
|
|
return [ |
|
184
|
|
|
new TwigFunction("bootstrapBadgeDanger", [$this, "bootstrapBadgeDangerFunction"], ["is_safe" => ["html"]]), |
|
185
|
|
|
new TwigFunction("bsBadgeDanger", [$this, "bootstrapBadgeDangerFunction"], ["is_safe" => ["html"]]), |
|
186
|
|
|
|
|
187
|
|
|
new TwigFunction("bootstrapBadgeDark", [$this, "bootstrapBadgeDarkFunction"], ["is_safe" => ["html"]]), |
|
188
|
|
|
new TwigFunction("bsBadgeDark", [$this, "bootstrapBadgeDarkFunction"], ["is_safe" => ["html"]]), |
|
189
|
|
|
|
|
190
|
|
|
new TwigFunction("bootstrapBadgeInfo", [$this, "bootstrapBadgeInfoFunction"], ["is_safe" => ["html"]]), |
|
191
|
|
|
new TwigFunction("bsBadgeInfo", [$this, "bootstrapBadgeInfoFunction"], ["is_safe" => ["html"]]), |
|
192
|
|
|
|
|
193
|
|
|
new TwigFunction("bootstrapBadgeLight", [$this, "bootstrapBadgeLightFunction"], ["is_safe" => ["html"]]), |
|
194
|
|
|
new TwigFunction("bsBadgeLight", [$this, "bootstrapBadgeLightFunction"], ["is_safe" => ["html"]]), |
|
195
|
|
|
|
|
196
|
|
|
new TwigFunction("bootstrapBadgePrimary", [$this, "bootstrapBadgePrimaryFunction"], ["is_safe" => ["html"]]), |
|
197
|
|
|
new TwigFunction("bsBadgePrimary", [$this, "bootstrapBadgePrimaryFunction"], ["is_safe" => ["html"]]), |
|
198
|
|
|
|
|
199
|
|
|
new TwigFunction("bootstrapBadgeSecondary", [$this, "bootstrapBadgeSecondaryFunction"], ["is_safe" => ["html"]]), |
|
200
|
|
|
new TwigFunction("bsBadgeSecondary", [$this, "bootstrapBadgeSecondaryFunction"], ["is_safe" => ["html"]]), |
|
201
|
|
|
|
|
202
|
|
|
new TwigFunction("bootstrapBadgeSuccess", [$this, "bootstrapBadgeSuccessFunction"], ["is_safe" => ["html"]]), |
|
203
|
|
|
new TwigFunction("bsBadgeSuccess", [$this, "bootstrapBadgeSuccessFunction"], ["is_safe" => ["html"]]), |
|
204
|
|
|
|
|
205
|
|
|
new TwigFunction("bootstrapBadgeWarning", [$this, "bootstrapBadgeWarningFunction"], ["is_safe" => ["html"]]), |
|
206
|
|
|
new TwigFunction("bsBadgeWarning", [$this, "bootstrapBadgeWarningFunction"], ["is_safe" => ["html"]]), |
|
207
|
|
|
]; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|