Completed
Push — master ( 2a62e7...7d0ef3 )
by WEBEWEB
06:55
created

AbstractComponentTwigExtension::bootstrapButton()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.5222
c 0
b 0
f 0
cc 9
nc 256
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
        $span   = self::bootstrapHTMLElement("span", "&times;", ["aria-hidden" => "true"]);
61
        $button = self::bootstrapHTMLElement("button", $span, ["class" => "close", "type" => "button", "data-dismiss" => "alert", "aria-label" => "Close"]);
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 self::bootstrapHTMLElement("div", $innerHTML, $attributes);
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 attributes.
86
        $attributes = [];
87
88
        $attributes["class"] = ["badge"];
89
90
        // Return the HTML.
91
        return self::bootstrapHTMLElement("span", $content, $attributes);
92
    }
93
94
    /**
95
     * Displays a Bootstrap breadcrumb.
96
     *
97
     * @param NavigationNode $node The navigation node.
98
     * @param boolean $last Last node ?.
99
     * @return string Returns the Bootstrap breadcrumb.
100
     */
101
    private function bootstrapBreadcrumb(NavigationNode $node, $last) {
102
103
        // Initialize the attributes.
104
        $attributes = true === $node->getActive() && true === $last ? ["class" => "active"] : [];
105
106
        // Initialize the parameters.
107
        $content   = $this->getTranslator()->trans($node->getId());
108
        $innerHTML = true === $last ? $content : self::bootstrapHTMLElement("a", $content, ["href" => $node->getRoute()]);
109
110
        // Return the HTML.
111
        return self::bootstrapHTMLElement("li", $innerHTML, $attributes);
112
    }
113
114
    /**
115
     * Displays a Bootstrap breadcrumbs.
116
     *
117
     * @param NavigationTree $tree The tree.
118
     * @return string Returns the Bootstrap breadcrumbs.
119
     */
120
    protected function bootstrapBreadcrumbs(NavigationTree $tree) {
121
122
        // Initialize the attributes.
123
        $attributes = [];
124
125
        $attributes["class"] = ["breadcrumb"];
126
127
        // Initialize the parameters.
128
        $innerHTML = [];
129
130
        // Get the breadcrumb node.
131
        $nodes = NavigationTreeHelper::getBreadcrumbs($tree);
132
        $count = count($nodes);
133
134
        // Handle each breadcrumb node.
135
        for ($i = 0; $i < $count; ++$i) {
136
            $innerHTML[] = $this->bootstrapBreadcrumb($nodes[$i], $count === $i + 1);
137
        }
138
139
        // Return the HTML.
140
        return self::bootstrapHTMLElement("ol", "\n" . implode("\n", $innerHTML) . "\n", $attributes);
141
    }
142
143
    /**
144
     * Displays a Bootstrap button.
145
     *
146
     * @param string $content The button content.
147
     * @param string $title The button title.
148
     * @param string $size The button size.
149
     * @param boolean $block Block button ?
150
     * @param booelan $active Active button ?
151
     * @param booelan $disable Disable button ?
152
     * @param string $class The button class.
153
     * @param string $icon The button icon.
154
     * @return string Returns the Bootstrap button.
155
     */
156
    protected function bootstrapButton($content, $title, $size, $block, $active, $disable, $class, $icon) {
157
158
        // Initialize the attributes.
159
        $attributes = [];
160
161
        $attributes["class"]          = ["btn", $class];
162
        $attributes["class"][]        = true === $block ? "btn-block" : null;
163
        $attributes["class"][]        = true === in_array($size, ["lg", "sm", "xs"]) ? "btn-" . $size : null;
164
        $attributes["class"][]        = true === $active ? "active" : null;
165
        $attributes["title"]          = $title;
166
        $attributes["type"]           = "button";
167
        $attributes["data-toggle"]    = null !== $title ? "tooltip" : null;
168
        $attributes["data-placement"] = null !== $title ? "top" : null;
169
        $attributes["disabled"]       = true === $disable ? "disabled" : null;
170
171
        // Handle the parameters.
172
        $glyphicon = null !== $icon ? BootstrapRendererTwigExtension::renderIcon($icon) : "";
173
        $innerHTML = null !== $content ? $content : "";
174
175
        // Return the HTML.
176
        return self::bootstrapHTMLElement("button", implode(" ", [$glyphicon, $innerHTML]), $attributes);
177
    }
178
179
    /**
180
     * Displays a Bootstrap button group.
181
     *
182
     * @param string $class The class.
183
     * @param string $role The role.
184
     * @param array $buttons The buttons.
185
     * @return string Returns the Bootstrap button group.
186
     */
187
    protected function bootstrapButtonGroup($class, $role, array $buttons) {
188
189
        // Initialize the attributes.
190
        $attributes = [];
191
192
        $attributes["class"] = $class;
193
        $attributes["role"]  = $role;
194
195
        // Initialize the parameters.
196
        $innerHTML = "\n" . implode("\n", $buttons) . "\n";
197
198
        // Return the HTML.
199
        return self::bootstrapHTMLElement("div", $innerHTML, $attributes);
200
    }
201
202
    /**
203
     * Displays a Bootstrap dropdown "Button".
204
     *
205
     * @param string $content The content.
206
     * @param string $id The id.
207
     * @param boolean $expanded Expanded ?
208
     * @param string $class The class.
209
     * @return string Returns the Bootstrap dropdown "Button".
210
     */
211
    protected function bootstrapDropdownButton($content, $id, $expanded, $class) {
212
213
        // Initailize the values.
214
        $classes = BootstrapBundle::getBootstrapConstants();
215
216
        // Initialize the attributes.
217
        $attributes = [];
218
219
        $attributes["class"][]         = "btn";
220
        $attributes["class"][]         = true === in_array($class, $classes) ? "btn-" . $class : "btn-default";
221
        $attributes["class"][]         = "dropdown-toggle";
222
        $attributes["type"][]          = "button";
223
        $attributes["id"][]            = null !== $id ? $id : "";
224
        $attributes["data-toggle"][]   = "dropdown";
225
        $attributes["aria-haspopup"][] = "true";
226
        $attributes["aria-expanded"][] = StringUtility::parseBoolean($expanded);
227
228
        // Initialize the parameters.
229
        $innerHTML = (null !== $content ? $content : "") . "<span class=\"caret\"></span>";
230
231
        // Return the HTML.
232
        return self::bootstrapHTMLElement("button", $innerHTML, $attributes);
233
    }
234
235
    /**
236
     * Displays a Bootstrap dropdown "Divider".
237
     *
238
     * @return string Returns the Bootstrap dropdown "Divider".
239
     */
240
    protected function bootstrapDropdownDivider() {
241
242
        // Initialize the attributes.
243
        $attributes = [];
244
245
        $attributes["class"] = "divider";
246
        $attributes["role"]  = "separator";
247
248
        // Return the HTML.
249
        return self::bootstrapHTMLElement("li", null, $attributes);
250
    }
251
252
    /**
253
     * Displays a Bootstrap dropdown "Header".
254
     *
255
     * @param string $content The content.
256
     * @return string Returns the Bootstrap dropdown "Header".
257
     */
258
    protected function bootstrapDropdownHeader($content) {
259
260
        // Initialize the attributes.
261
        $attributes = [];
262
263
        $attributes["class"] = "dropdown-header";
264
265
        // Return the HTML.
266
        return self::bootstrapHTMLElement("li", $content, $attributes);
267
    }
268
269
    /**
270
     * Displays a Bootstrap glyphicon.
271
     *
272
     * @param string $name The glyphicon name.
273
     * @return string Returns the Bootstrap glyphicon.
274
     */
275
    protected function bootstrapGlyphicon($name, $style) {
276
277
        // Initialize the attributes.
278
        $attributes = [];
279
280
        $attributes["class"][]     = "glyphicon";
281
        $attributes["class"][]     = null !== $name ? "glyphicon-" . $name : null;
282
        $attributes["aria-hidden"] = "true";
283
        $attributes["style"]       = $style;
284
285
        // Return the HTML.
286
        return self::bootstrapHTMLElement("span", null, $attributes);
287
    }
288
289
    /**
290
     * Displays a Bootstrap label.
291
     *
292
     * @param string $content The label content.
293
     * @param string $class The label class.
294
     * @return string Returns the Bootstrap label.
295
     */
296
    protected function bootstrapLabel($content, $class) {
297
298
        // Initialize the attributes.
299
        $attributes = [];
300
301
        $attributes["class"] = ["label", $class];
302
303
        // Return the HTML.
304
        return self::bootstrapHTMLElement("span", $content, $attributes);
305
    }
306
307
    /**
308
     * Displays a Bootstrap progress bar.
309
     *
310
     * @param string $content The progress bar content.
311
     * @param integer $value The progress bar value.
312
     * @param integer $min The progress bar min.
313
     * @param integer $max The progress bar max.
314
     * @param boolean $striped Progress bar striped ?
315
     * @param boolean $animated Progress bar animated ?
316
     * @param string $class The progress bar class.
317
     * @return string Returns the Bootstrap progress bar.
318
     */
319
    protected function bootstrapProgressBar($content, $value, $min, $max, $striped, $animated, $class = null) {
320
321
        // Initialize the template.
322
        $span = self::bootstrapHTMLElement("span", $value . "%", ["class" => "sr-only"]);
323
324
        // Initialize the attributes.
325
        $attributes = [];
326
327
        $attributes["class"]         = ["progress-bar", $class];
328
        $attributes["class"][]       = true === $striped ? "progress-bar-striped" : null;
329
        $attributes["class"][]       = true === $animated ? "active" : null;
330
        $attributes["style"]         = "width: " . $value . "%;";
331
        $attributes["role"]          = "progressbar";
332
        $attributes["aria-valuenow"] = $value;
333
        $attributes["aria-valuemin"] = $min;
334
        $attributes["aria-valuemax"] = $max . "%";
335
336
        // Initialize the parameters.
337
        $innerHTML = null !== $content ? $content : $span;
338
339
        // Initialize the template.
340
        $div = self::bootstrapHTMLElement("div", $innerHTML, $attributes);
341
342
        // Return the HTML.
343
        return self::bootstrapHTMLElement("div", $div, ["class" => "progress"]);
344
    }
345
346
    /**
347
     * Get the translator.
348
     *
349
     * @return TranslatorInterface Returns the translator.
350
     */
351
    public function getTranslator() {
352
        return $this->translator;
353
    }
354
355
    /**
356
     * Set the translator.
357
     * @param TranslatorInterface $translator The translator.
358
     * @return AbstractComponentTwigExtension Returns this component Twig extension.
359
     */
360
    protected function setTranslator(TranslatorInterface $translator = null) {
361
        $this->translator = $translator;
362
        return $this;
363
    }
364
365
}
366