|
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\CSS; |
|
13
|
|
|
|
|
14
|
|
|
use Twig\TwigFilter; |
|
15
|
|
|
use Twig\TwigFunction; |
|
16
|
|
|
use WBW\Bundle\BootstrapBundle\Button\ButtonFactory; |
|
17
|
|
|
use WBW\Library\Core\Argument\Helper\ArrayHelper; |
|
18
|
|
|
use WBW\Library\Core\Argument\Helper\StringHelper; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Button Twig extension. |
|
22
|
|
|
* |
|
23
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
24
|
|
|
* @package WBW\Bundle\BootstrapBundle\Twig\Extension\CSS |
|
25
|
|
|
* @link https://getbootstrap.com/docs/3.3/css/#buttons |
|
26
|
|
|
*/ |
|
27
|
|
|
class ButtonTwigExtension extends AbstractButtonTwigExtension { |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Service name. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
const SERVICE_NAME = "wbw.bootstrap.twig.extension.css.button"; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Displays a Bootstrap button "danger". |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $args The arguments. |
|
40
|
|
|
* @return string Returns the Bootstrap button "danger". |
|
41
|
|
|
*/ |
|
42
|
|
|
public function bootstrapButtonDangerFunction(array $args = []): string { |
|
43
|
|
|
return $this->bootstrapButton(ButtonFactory::parseDangerButton($args), ArrayHelper::get($args, "icon")); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Displays a Bootstrap button "dark". |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $args The arguments. |
|
50
|
|
|
* @return string Returns the Bootstrap button "dark". |
|
51
|
|
|
*/ |
|
52
|
|
|
public function bootstrapButtonDarkFunction(array $args = []): string { |
|
53
|
|
|
return $this->bootstrapButton(ButtonFactory::parseDarkButton($args), ArrayHelper::get($args, "icon")); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Displays a Bootstrap button "default". |
|
58
|
|
|
* |
|
59
|
|
|
* @param array $args The arguments. |
|
60
|
|
|
* @return string Returns the Bootstrap button "default". |
|
61
|
|
|
*/ |
|
62
|
|
|
public function bootstrapButtonDefaultFunction(array $args = []): string { |
|
63
|
|
|
return $this->bootstrapButton(ButtonFactory::parseDefaultButton($args), ArrayHelper::get($args, "icon")); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Displays a Bootstrap button "info". |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $args The arguments. |
|
70
|
|
|
* @return string Returns the Bootstrap button "info". |
|
71
|
|
|
*/ |
|
72
|
|
|
public function bootstrapButtonInfoFunction(array $args = []): string { |
|
73
|
|
|
return $this->bootstrapButton(ButtonFactory::parseInfoButton($args), ArrayHelper::get($args, "icon")); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Displays a Bootstrap button "light". |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $args The arguments. |
|
80
|
|
|
* @return string Returns the Bootstrap button "light". |
|
81
|
|
|
*/ |
|
82
|
|
|
public function bootstrapButtonLightFunction(array $args = []): string { |
|
83
|
|
|
return $this->bootstrapButton(ButtonFactory::parseLightButton($args), ArrayHelper::get($args, "icon")); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Transforms a Bootstrap button into an anchor. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $button The button. |
|
90
|
|
|
* @param string $href The href attribute. |
|
91
|
|
|
* @param string|null $target The target attribute. |
|
92
|
|
|
* @return string Returns the Bootstrap button transformed into an anchor. |
|
93
|
|
|
*/ |
|
94
|
|
|
public function bootstrapButtonLinkFilter(string $button, string $href = self::DEFAULT_HREF, string $target = null): string { |
|
95
|
|
|
|
|
96
|
|
|
if (1 === preg_match('/disabled="disabled"/', $button)) { |
|
97
|
|
|
|
|
98
|
|
|
$searches = [' disabled="disabled"', 'class="']; |
|
99
|
|
|
$replaces = ["", 'class="disabled ']; |
|
100
|
|
|
|
|
101
|
|
|
$button = str_replace($searches, $replaces, $button); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$searches = ["<button", "</button>", 'type="button"']; |
|
105
|
|
|
$replaces = ["<a", "</a>", StringHelper::parseArray(["href" => $href, "target" => $target])]; |
|
106
|
|
|
|
|
107
|
|
|
return str_replace($searches, $replaces, $button); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Displays a Bootstrap button "link". |
|
112
|
|
|
* |
|
113
|
|
|
* @param array $args The arguments. |
|
114
|
|
|
* @return string Returns the Bootstrap button "link". |
|
115
|
|
|
*/ |
|
116
|
|
|
public function bootstrapButtonLinkFunction(array $args = []): string { |
|
117
|
|
|
return $this->bootstrapButton(ButtonFactory::parseLinkButton($args), ArrayHelper::get($args, "icon")); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Displays a Bootstrap button "primary". |
|
122
|
|
|
* |
|
123
|
|
|
* @param array $args The arguments. |
|
124
|
|
|
* @return string Returns the Bootstrap button "primary". |
|
125
|
|
|
*/ |
|
126
|
|
|
public function bootstrapButtonPrimaryFunction(array $args = []): string { |
|
127
|
|
|
return $this->bootstrapButton(ButtonFactory::parsePrimaryButton($args), ArrayHelper::get($args, "icon")); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Displays a Bootstrap button "secondary". |
|
132
|
|
|
* |
|
133
|
|
|
* @param array $args The arguments. |
|
134
|
|
|
* @return string Returns the Bootstrap button "secondary". |
|
135
|
|
|
*/ |
|
136
|
|
|
public function bootstrapButtonSecondaryFunction(array $args = []): string { |
|
137
|
|
|
return $this->bootstrapButton(ButtonFactory::parseSecondaryButton($args), ArrayHelper::get($args, "icon")); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Transforms a Bootstrap button into a submit button. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $button The button. |
|
144
|
|
|
* @return string Returns the Bootstrap button transformed into a submit button. |
|
145
|
|
|
*/ |
|
146
|
|
|
public function bootstrapButtonSubmitFilter(string $button): string { |
|
147
|
|
|
return str_replace(['type="button"'], ['type="submit"'], $button); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Displays a Bootstrap button "success". |
|
152
|
|
|
* |
|
153
|
|
|
* @param array $args The arguments. |
|
154
|
|
|
* @return string Returns the Bootstrap button "success". |
|
155
|
|
|
*/ |
|
156
|
|
|
public function bootstrapButtonSuccessFunction(array $args = []): string { |
|
157
|
|
|
return $this->bootstrapButton(ButtonFactory::parseSuccessButton($args), ArrayHelper::get($args, "icon")); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Displays a Bootstrap button "warning". |
|
162
|
|
|
* |
|
163
|
|
|
* @param array $args The arguments. |
|
164
|
|
|
* @return string Returns the Bootstrap button "warning". |
|
165
|
|
|
*/ |
|
166
|
|
|
public function bootstrapButtonWarningFunction(array $args = []): string { |
|
167
|
|
|
return $this->bootstrapButton(ButtonFactory::parseWarningButton($args), ArrayHelper::get($args, "icon")); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Get the Twig filters. |
|
172
|
|
|
* |
|
173
|
|
|
* @return TwigFilter[] Returns the Twig filters. |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getFilters(): array { |
|
176
|
|
|
return [ |
|
177
|
|
|
new TwigFilter("bootstrapButtonLink", [$this, "bootstrapButtonLinkFilter"], ["is_safe" => ["html"]]), |
|
178
|
|
|
new TwigFilter("bsButtonLink", [$this, "bootstrapButtonLinkFilter"], ["is_safe" => ["html"]]), |
|
179
|
|
|
|
|
180
|
|
|
new TwigFilter("bootstrapButtonSubmit", [$this, "bootstrapButtonSubmitFilter"], ["is_safe" => ["html"]]), |
|
181
|
|
|
new TwigFilter("bsButtonSubmit", [$this, "bootstrapButtonSubmitFilter"], ["is_safe" => ["html"]]), |
|
182
|
|
|
]; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Get the Twig functions. |
|
187
|
|
|
* |
|
188
|
|
|
* @return TwigFunction[] Returns the Twig functions. |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getFunctions(): array { |
|
191
|
|
|
|
|
192
|
|
|
$functions3 = $this->getFunctions3(); |
|
193
|
|
|
if (3 === $this->getVersion()) { |
|
194
|
|
|
return $functions3; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$functions4 = $this->getFunctions4(); |
|
198
|
|
|
|
|
199
|
|
|
return array_merge($functions3, $functions4); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Get the Twig functions. |
|
204
|
|
|
* |
|
205
|
|
|
* @return TwigFunction[] Returns the Twig functions. |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function getFunctions3(): array { |
|
208
|
|
|
|
|
209
|
|
|
$functions = [ |
|
210
|
|
|
new TwigFunction("bootstrapButtonDanger", [$this, "bootstrapButtonDangerFunction"], ["is_safe" => ["html"]]), |
|
211
|
|
|
new TwigFunction("bsButtonDanger", [$this, "bootstrapButtonDangerFunction"], ["is_safe" => ["html"]]), |
|
212
|
|
|
|
|
213
|
|
|
new TwigFunction("bootstrapButtonInfo", [$this, "bootstrapButtonInfoFunction"], ["is_safe" => ["html"]]), |
|
214
|
|
|
new TwigFunction("bsButtonInfo", [$this, "bootstrapButtonInfoFunction"], ["is_safe" => ["html"]]), |
|
215
|
|
|
|
|
216
|
|
|
new TwigFunction("bootstrapButtonLink", [$this, "bootstrapButtonLinkFunction"], ["is_safe" => ["html"]]), |
|
217
|
|
|
new TwigFunction("bsButtonLink", [$this, "bootstrapButtonLinkFunction"], ["is_safe" => ["html"]]), |
|
218
|
|
|
|
|
219
|
|
|
new TwigFunction("bootstrapButtonPrimary", [$this, "bootstrapButtonPrimaryFunction"], ["is_safe" => ["html"]]), |
|
220
|
|
|
new TwigFunction("bsButtonPrimary", [$this, "bootstrapButtonPrimaryFunction"], ["is_safe" => ["html"]]), |
|
221
|
|
|
|
|
222
|
|
|
new TwigFunction("bootstrapButtonSuccess", [$this, "bootstrapButtonSuccessFunction"], ["is_safe" => ["html"]]), |
|
223
|
|
|
new TwigFunction("bsButtonSuccess", [$this, "bootstrapButtonSuccessFunction"], ["is_safe" => ["html"]]), |
|
224
|
|
|
|
|
225
|
|
|
new TwigFunction("bootstrapButtonWarning", [$this, "bootstrapButtonWarningFunction"], ["is_safe" => ["html"]]), |
|
226
|
|
|
new TwigFunction("bsButtonWarning", [$this, "bootstrapButtonWarningFunction"], ["is_safe" => ["html"]]), |
|
227
|
|
|
]; |
|
228
|
|
|
|
|
229
|
|
|
if (3 === $this->getVersion()) { |
|
230
|
|
|
|
|
231
|
|
|
$functions3 = [ |
|
232
|
|
|
new TwigFunction("bootstrapButtonDefault", [$this, "bootstrapButtonDefaultFunction"], ["is_safe" => ["html"]]), |
|
233
|
|
|
new TwigFunction("bsButtonDefault", [$this, "bootstrapButtonDefaultFunction"], ["is_safe" => ["html"]]), |
|
234
|
|
|
]; |
|
235
|
|
|
|
|
236
|
|
|
array_splice($functions, 2, 0, $functions3); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
return $functions; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Get the Twig functions. |
|
244
|
|
|
* |
|
245
|
|
|
* @return TwigFunction[] Returns the Twig functions. |
|
246
|
|
|
*/ |
|
247
|
|
|
protected function getFunctions4(): array { |
|
248
|
|
|
return [ |
|
249
|
|
|
new TwigFunction("bootstrapButtonDark", [$this, "bootstrapButtonDarkFunction"], ["is_safe" => ["html"]]), |
|
250
|
|
|
new TwigFunction("bsButtonDark", [$this, "bootstrapButtonDarkFunction"], ["is_safe" => ["html"]]), |
|
251
|
|
|
|
|
252
|
|
|
new TwigFunction("bootstrapButtonLight", [$this, "bootstrapButtonLightFunction"], ["is_safe" => ["html"]]), |
|
253
|
|
|
new TwigFunction("bsButtonLight", [$this, "bootstrapButtonLightFunction"], ["is_safe" => ["html"]]), |
|
254
|
|
|
|
|
255
|
|
|
new TwigFunction("bootstrapButtonSecondary", [$this, "bootstrapButtonSecondaryFunction"], ["is_safe" => ["html"]]), |
|
256
|
|
|
new TwigFunction("bsButtonSecondary", [$this, "bootstrapButtonSecondaryFunction"], ["is_safe" => ["html"]]), |
|
257
|
|
|
]; |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|