Completed
Push — master ( be9027...bcb5d0 )
by WEBEWEB
01:29
created

AbstractComponentTwigExtension::bootstrapButton()   F

Complexity

Conditions 9
Paths 256

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
rs 3
cc 9
eloc 15
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 NdC/WBW
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 WBW\Bundle\BootstrapBundle\Twig\Extension\AbstractBootstrapTwigExtension;
15
use WBW\Library\Core\Utility\StringUtility;
16
17
/**
18
 * Abstract component Twig extension.
19
 *
20
 * @author NdC/WBW <https://github.com/webeweb/>
21
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Component
22
 * @abstract
23
 */
24
abstract class AbstractComponentTwigExtension extends AbstractBootstrapTwigExtension {
25
26
    /**
27
     * Displays a Bootstrap alert.
28
     *
29
     * @param string $content The alert content.
30
     * @param boolean $dismissible Dismissible alert ?
31
     * @param string $class The alert class.
32
     * @return string Returns the Bootstrap alert.
33
     */
34
    final protected function bootstrapAlert($content, $dismissible, $class) {
35
36
        // Initialize the templates.
37
        $template = "<div %attributes%>%innerHTML%</div>";
38
        $button   = '<button class="close" type="button" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
39
40
        // Initialize the attributes.
41
        $attributes = [];
42
43
        $attributes["class"]   = ["alert", $class];
44
        $attributes["class"][] = true === $dismissible ? "alert-dismissible" : null;
45
        $attributes["role"]    = ["alert"];
46
47
        // Initialize the parameters.
48
        $innerHTML = (true === $dismissible ? $button : "") . (null !== $content ? $content : self::DEFAULT_CONTENT);
49
50
        // Return the HTML.
51
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
52
    }
53
54
    /**
55
     * Displays a Bootstrap badge.
56
     *
57
     * @param string $content The badge content.
58
     * @return string Returns the Bootstrap badge.
59
     */
60
    final protected function bootstrapBadge($content) {
61
62
        // Initialize the template.
63
        $template = '<span %attributes%>%innerHTML%</span>';
64
65
        // Initialize the attributes.
66
        $attributes = [];
67
68
        $attributes["class"] = ["badge"];
69
70
        // Initialize the parameters.
71
        $innerHTML = null !== $content ? $content : self::DEFAULT_CONTENT;
72
73
        // Return the HTML.
74
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
75
    }
76
77
    /**
78
     * Displays a Bootstrap button.
79
     *
80
     * @param string $content The button content.
81
     * @param string $title The button title.
82
     * @param string $size The button size.
83
     * @param boolean $block Block button ?
84
     * @param booelan $active Active button ?
85
     * @param booelan $disable Disable button ?
86
     * @param string $class The button class.
87
     * @param string $icon The button icon.
88
     * @return string Returns the Bootstrap button.
89
     */
90
    final protected function bootstrapButton($content, $title, $size, $block, $active, $disable, $class, $icon) {
91
92
        // Initialize the template.
93
        $template = "<button %attributes%>%glyphicon%%innerHTML%</button>";
94
95
        // Initialize the attributes.
96
        $attributes = [];
97
98
        $attributes["class"]          = ["btn", $class];
99
        $attributes["class"][]        = true === $block ? "btn-block" : null;
100
        $attributes["class"][]        = true === in_array($size, ["lg", "sm", "xs"]) ? "btn-" . $size : null;
101
        $attributes["class"][]        = true === $active ? "active" : null;
102
        $attributes["title"]          = $title;
103
        $attributes["type"]           = "button";
104
        $attributes["data-toggle"]    = null !== $title ? "tooltip" : null;
105
        $attributes["data-placement"] = null !== $title ? "top" : null;
106
        $attributes["disabled"]       = true === $disable ? "disabled" : null;
107
108
        // Handle the parameters.
109
        $glyphicon = null !== $icon ? $this->bootstrapGlyphicon($icon, null) . " " : "";
110
        $innerHTML = null !== $content ? $content : self::DEFAULT_CONTENT;
111
112
        // Return the HTML.
113
        return StringUtility::replace($template, ["%attributes%", "%glyphicon%", "%innerHTML%"], [StringUtility::parseArray($attributes), $glyphicon, $innerHTML]);
114
    }
115
116
    /**
117
     * Displays a Bootstrap glyphicon.
118
     *
119
     * @param string $name The glyphicon name.
120
     * @return string Returns the Bootstrap glyphicon.
121
     */
122
    final protected function bootstrapGlyphicon($name, $style) {
123
124
        // Initialize the template.
125
        $template = "<span %attributes%></span>";
126
127
        // Initialize the attributes.
128
        $attributes = [];
129
130
        $attributes["class"]       = ["glyphicon"];
131
        $attributes["class"][]     = null !== $name ? "glyphicon-" . $name : null;
132
        $attributes["aria-hidden"] = "true";
133
        $attributes["style"]       = $style;
134
135
        // Return the HTML.
136
        return StringUtility::replace($template, ["%attributes%"], [StringUtility::parseArray($attributes)]);
137
    }
138
139
    /**
140
     * Displays a Bootstrap label.
141
     *
142
     * @param string $content The label content.
143
     * @param string $class The label class.
144
     * @return string Returns the Bootstrap label.
145
     */
146
    final protected function bootstrapLabel($content, $class) {
147
148
        // Initialize the template.
149
        $template = "<span %attributes%>%innerHTML%</span>";
150
151
        // Initialize the attributes.
152
        $attributes = [];
153
154
        $attributes["class"] = ["label", $class];
155
156
        // Initialize the parameters.
157
        $innerHTML = null !== $content ? $content : self::DEFAULT_CONTENT;
158
159
        // Return the HTML.
160
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
161
    }
162
163
    /**
164
     * Displays a Bootstrap progress bar.
165
     *
166
     * @param string $content The progress bar content.
167
     * @param integer $value The progress bar value.
168
     * @param integer $min The progress bar min.
169
     * @param integer $max The progress bar max.
170
     * @param boolean $striped Progress bar striped ?
171
     * @param boolean $animated Progress bar animated ?
172
     * @param string $class The progress bar class.
173
     * @return string Returns the Bootstrap progress bar.
174
     */
175
    final protected function bootstrapProgressBar($content, $value, $min, $max, $striped, $animated, $class = null) {
176
177
        // Initialize the template.
178
        $template = '<div class="progress"><div %attributes%>%innerHTML%</div></div>';
179
180
        // Initialize the attributes.
181
        $attributes = [];
182
183
        $attributes["class"]         = ["progress-bar", $class];
184
        $attributes["class"][]       = true === $striped ? "progress-bar-striped" : null;
185
        $attributes["class"][]       = true === $animated ? "active" : null;
186
        $attributes["style"]         = "width: " . $value . "%;";
187
        $attributes["role"]          = "progressbar";
188
        $attributes["aria-valuenow"] = $value;
189
        $attributes["aria-valuemin"] = $min;
190
        $attributes["aria-valuemax"] = $max . "%";
191
192
        // Initialize the parameters.
193
        $innerHTML = !is_null($content) ? $content . '<span class="sr-only">' . $value . ' %</span>' : self::DEFAULT_CONTENT;
194
195
        // Return the HTML.
196
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
197
    }
198
199
}
200