Completed
Push — master ( f4996e...e1ea0b )
by WEBEWEB
01:42
created

AbstractComponentTwigExtension::bootstrapButton()   D

Complexity

Conditions 10
Paths 512

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 4.1777
c 0
b 0
f 0
cc 10
nc 512
nop 8

How to fix   Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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