Completed
Push — master ( ba9a91...2a62e7 )
by WEBEWEB
02:17
created

bootstrapDropdownHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 1
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 Symfony\Component\Translation\TranslatorInterface;
15
use WBW\Bundle\BootstrapBundle\BootstrapBundle;
16
use WBW\Bundle\BootstrapBundle\Helper\NavigationTreeHelper;
17
use WBW\Bundle\BootstrapBundle\Navigation\NavigationNode;
18
use WBW\Bundle\BootstrapBundle\Navigation\NavigationTree;
19
use WBW\Bundle\BootstrapBundle\Twig\Extension\AbstractBootstrapTwigExtension;
20
use WBW\Bundle\BootstrapBundle\Twig\Extension\BootstrapRendererTwigExtension;
21
use WBW\Library\Core\Utility\Argument\StringUtility;
22
23
/**
24
 * Abstract component Twig extension.
25
 *
26
 * @author webeweb <https://github.com/webeweb/>
27
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Component
28
 * @abstract
29
 */
30
abstract class AbstractComponentTwigExtension extends AbstractBootstrapTwigExtension {
31
32
    /**
33
     * Translator.
34
     *
35
     * @var TranslatorInterface
36
     */
37
    private $translator;
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param TranslatorInterface $translator The translator.
43
     */
44
    protected function __construct(TranslatorInterface $translator = null) {
45
        parent::__construct();
46
        $this->setTranslator($translator);
47
    }
48
49
    /**
50
     * Displays a Bootstrap alert.
51
     *
52
     * @param string $content The alert content.
53
     * @param boolean $dismissible Dismissible alert ?
54
     * @param string $class The alert class.
55
     * @return string Returns the Bootstrap alert.
56
     */
57
    protected function bootstrapAlert($content, $dismissible, $class) {
58
59
        // Initialize the templates.
60
        $template = "<div %attributes%>%innerHTML%</div>";
61
        $button   = "<button class=\"close\" type=\"button\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>";
62
63
        // Initialize the attributes.
64
        $attributes = [];
65
66
        $attributes["class"]   = ["alert", $class];
67
        $attributes["class"][] = true === $dismissible ? "alert-dismissible" : null;
68
        $attributes["role"]    = ["alert"];
69
70
        // Initialize the parameters.
71
        $innerHTML = (true === $dismissible ? $button : "") . (null !== $content ? $content : "");
72
73
        // Return the HTML.
74
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
75
    }
76
77
    /**
78
     * Displays a Bootstrap badge.
79
     *
80
     * @param string $content The badge content.
81
     * @return string Returns the Bootstrap badge.
82
     */
83
    protected function bootstrapBadge($content) {
84
85
        // Initialize the template.
86
        $template = "<span %attributes%>%innerHTML%</span>";
87
88
        // Initialize the attributes.
89
        $attributes = [];
90
91
        $attributes["class"] = ["badge"];
92
93
        // Initialize the parameters.
94
        $innerHTML = null !== $content ? $content : "";
95
96
        // Return the HTML.
97
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
98
    }
99
100
    /**
101
     * Displays a Bootstrap breadcrumb.
102
     *
103
     * @param NavigationNode $node The navigation node.
104
     * @param boolean $last Last node ?.
105
     * @return string Returns the Bootstrap breadcrumb.
106
     */
107
    private function bootstrapBreadcrumb(NavigationNode $node, $last) {
108
109
        // Initialize the template.
110
        $template = "<li%attributes%>%innerHTML%</li>";
111
112
        // Initialize the parameters.
113
        $content = $this->getTranslator()->trans($node->getId());
114
115
        $attributes = true === $node->getActive() && true === $last ? " class=\"active\"" : "";
116
        $innerHTML  = true === $last ? $content : $this->bootstrapDOMObject("a", "href=\"" . $node->getRoute() . "\"", $content);
117
118
        // Return the HTML.
119
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [$attributes, $innerHTML]);
120
    }
121
122
    /**
123
     * Displays a Bootstrap breadcrumbs.
124
     *
125
     * @param NavigationTree $tree The tree.
126
     * @return string Returns the Bootstrap breadcrumbs.
127
     */
128
    protected function bootstrapBreadcrumbs(NavigationTree $tree) {
129
130
        // Initialize the template.
131
        $template = "<ol %attributes%>\n%innerHTML%\n</ol>";
132
133
        // Initialize the attributes.
134
        $attributes = [];
135
136
        $attributes["class"] = ["breadcrumb"];
137
138
        // Initialize the parameters.
139
        $innerHTML = [];
140
141
        // Get the breadcrumb node.
142
        $nodes = NavigationTreeHelper::getBreadcrumbs($tree);
143
        $count = count($nodes);
144
145
        // Handle each breadcrumb node.
146
        for ($i = 0; $i < $count; ++$i) {
147
            $innerHTML[] = $this->bootstrapBreadcrumb($nodes[$i], $count === $i + 1);
148
        }
149
150
        // Return the HTML.
151
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), implode("\n", $innerHTML)]);
152
    }
153
154
    /**
155
     * Displays a Bootstrap button.
156
     *
157
     * @param string $content The button content.
158
     * @param string $title The button title.
159
     * @param string $size The button size.
160
     * @param boolean $block Block button ?
161
     * @param booelan $active Active button ?
162
     * @param booelan $disable Disable button ?
163
     * @param string $class The button class.
164
     * @param string $icon The button icon.
165
     * @return string Returns the Bootstrap button.
166
     */
167
    protected function bootstrapButton($content, $title, $size, $block, $active, $disable, $class, $icon) {
168
169
        // Initialize the template.
170
        $template = "<button %attributes%>%glyphicon%%innerHTML%</button>";
171
172
        // Initialize the attributes.
173
        $attributes = [];
174
175
        $attributes["class"]          = ["btn", $class];
176
        $attributes["class"][]        = true === $block ? "btn-block" : null;
177
        $attributes["class"][]        = true === in_array($size, ["lg", "sm", "xs"]) ? "btn-" . $size : null;
178
        $attributes["class"][]        = true === $active ? "active" : null;
179
        $attributes["title"]          = $title;
180
        $attributes["type"]           = "button";
181
        $attributes["data-toggle"]    = null !== $title ? "tooltip" : null;
182
        $attributes["data-placement"] = null !== $title ? "top" : null;
183
        $attributes["disabled"]       = true === $disable ? "disabled" : null;
184
185
        // Handle the parameters.
186
        $glyphicon = null !== $icon ? BootstrapRendererTwigExtension::renderIcon($icon) : "";
187
        $innerHTML = null !== $content ? ("" !== $glyphicon ? " " . $content : $content) : "";
188
189
        // Return the HTML.
190
        return StringUtility::replace($template, ["%attributes%", "%glyphicon%", "%innerHTML%"], [StringUtility::parseArray($attributes), $glyphicon, $innerHTML]);
191
    }
192
193
    /**
194
     * Displays a Bootstrap button group.
195
     *
196
     * @param string $class The class.
197
     * @param string $role The role.
198
     * @param array $buttons The buttons.
199
     * @return string Returns the Bootstrap button group.
200
     */
201
    protected function bootstrapButtonGroup($class, $role, array $buttons) {
202
203
        // Initialize the template.
204
        $template = "<div %attributes%>\n%innerHTML%\n</div>";
205
206
        // Initialize the attributes.
207
        $attributes = [];
208
209
        $attributes["class"] = $class;
210
        $attributes["role"]  = $role;
211
212
        // Initialize the parameters.
213
        $innerHTML = implode("\n", $buttons);
214
215
        // Return the HTML.
216
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
217
    }
218
219
    /**
220
     * Displays a Bootstrap dropdown "Button".
221
     *
222
     * @param string $content The content.
223
     * @param string $id The id.
224
     * @param boolean $expanded Expanded ?
225
     * @param string $class The class.
226
     * @return string Returns the Bootstrap dropdown "Button".
227
     */
228
    protected function bootstrapDropdownButton($content, $id, $expanded, $class) {
229
230
        // Initailize the values.
231
        $classes = BootstrapBundle::getBootstrapConstants();
232
233
        // Initialize the template.
234
        $template = "<button %attributes%>%innerHTML%<span class=\"caret\"></span></button>";
235
236
        // Initialize the attributes.
237
        $attributes = [];
238
239
        $attributes["class"][]         = "btn";
240
        $attributes["class"][]         = true === in_array($class, $classes) ? "btn-" . $class : "btn-default";
241
        $attributes["class"][]         = "dropdown-toggle";
242
        $attributes["type"][]          = "button";
243
        $attributes["id"][]            = null !== $id ? $id : "";
244
        $attributes["data-toggle"][]   = "dropdown";
245
        $attributes["aria-haspopup"][] = "true";
246
        $attributes["aria-expanded"][] = StringUtility::parseBoolean($expanded);
247
248
        // Initialize the parameters.
249
        $innerHTML = null !== $content ? $content : "";
250
251
        // Return the HTML.
252
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
253
    }
254
255
    /**
256
     * Displays a Bootstrap dropdown "Divider".
257
     *
258
     * @return string Returns the Bootstrap dropdown "Divider".
259
     */
260
    protected function bootstrapDropdownDivider() {
261
262
        // Initialize the template.
263
        $template = "<li %attributes%></li>";
264
265
        // Initialize the attributes.
266
        $attributes = [];
267
268
        $attributes["class"] = "divider";
269
        $attributes["role"]  = "separator";
270
271
        // Return the HTML.
272
        return StringUtility::replace($template, ["%attributes%"], [StringUtility::parseArray($attributes)]);
273
    }
274
275
    /**
276
     * Displays a Bootstrap dropdown "Header".
277
     *
278
     * @param string $content The content.
279
     * @return string Returns the Bootstrap dropdown "Header".
280
     */
281
    protected function bootstrapDropdownHeader($content) {
282
283
        // Initialize the template.
284
        $template = "<li %attributes%>%innerHTML%</li>";
285
286
        // Initialize the attributes.
287
        $attributes = [];
288
289
        $attributes["class"] = "dropdown-header";
290
291
        // Initialize the parameters.
292
        $innerHTML = null !== $content ? $content : "";
293
294
        // Return the HTML.
295
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
296
    }
297
298
    /**
299
     * Displays a Bootstrap glyphicon.
300
     *
301
     * @param string $name The glyphicon name.
302
     * @return string Returns the Bootstrap glyphicon.
303
     */
304
    protected function bootstrapGlyphicon($name, $style) {
305
306
        // Initialize the template.
307
        $template = "<span %attributes%></span>";
308
309
        // Initialize the attributes.
310
        $attributes = [];
311
312
        $attributes["class"]       = ["glyphicon"];
313
        $attributes["class"][]     = null !== $name ? "glyphicon-" . $name : null;
314
        $attributes["aria-hidden"] = "true";
315
        $attributes["style"]       = $style;
316
317
        // Return the HTML.
318
        return StringUtility::replace($template, ["%attributes%"], [StringUtility::parseArray($attributes)]);
319
    }
320
321
    /**
322
     * Displays a Bootstrap label.
323
     *
324
     * @param string $content The label content.
325
     * @param string $class The label class.
326
     * @return string Returns the Bootstrap label.
327
     */
328
    protected function bootstrapLabel($content, $class) {
329
330
        // Initialize the template.
331
        $template = "<span %attributes%>%innerHTML%</span>";
332
333
        // Initialize the attributes.
334
        $attributes = [];
335
336
        $attributes["class"] = ["label", $class];
337
338
        // Initialize the parameters.
339
        $innerHTML = null !== $content ? $content : "";
340
341
        // Return the HTML.
342
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
343
    }
344
345
    /**
346
     * Displays a Bootstrap progress bar.
347
     *
348
     * @param string $content The progress bar content.
349
     * @param integer $value The progress bar value.
350
     * @param integer $min The progress bar min.
351
     * @param integer $max The progress bar max.
352
     * @param boolean $striped Progress bar striped ?
353
     * @param boolean $animated Progress bar animated ?
354
     * @param string $class The progress bar class.
355
     * @return string Returns the Bootstrap progress bar.
356
     */
357
    protected function bootstrapProgressBar($content, $value, $min, $max, $striped, $animated, $class = null) {
358
359
        // Initialize the template.
360
        $template = "<div class=\"progress\"><div %attributes%>%innerHTML%</div></div>";
361
        $span     = "<span class=\"sr-only\">%value%%</span>";
362
363
        // Initialize the attributes.
364
        $attributes = [];
365
366
        $attributes["class"]         = ["progress-bar", $class];
367
        $attributes["class"][]       = true === $striped ? "progress-bar-striped" : null;
368
        $attributes["class"][]       = true === $animated ? "active" : null;
369
        $attributes["style"]         = "width: " . $value . "%;";
370
        $attributes["role"]          = "progressbar";
371
        $attributes["aria-valuenow"] = $value;
372
        $attributes["aria-valuemin"] = $min;
373
        $attributes["aria-valuemax"] = $max . "%";
374
375
        // Initialize the parameters.
376
        $innerHTML = null !== $content ? $content : StringUtility::replace($span, ["%value%"], [$value]);
377
378
        // Return the HTML.
379
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
380
    }
381
382
    /**
383
     * Get the translator.
384
     *
385
     * @return TranslatorInterface Returns the translator.
386
     */
387
    public function getTranslator() {
388
        return $this->translator;
389
    }
390
391
    /**
392
     * Set the translator.
393
     * @param TranslatorInterface $translator The translator.
394
     * @return AbstractComponentTwigExtension Returns this component Twig extension.
395
     */
396
    protected function setTranslator(TranslatorInterface $translator = null) {
397
        $this->translator = $translator;
398
        return $this;
399
    }
400
401
}
402