Completed
Push — master ( 67c65c...9a618a )
by WEBEWEB
06:47
created

AbstractComponentTwigExtension::bootstrapButton()   F

Complexity

Conditions 10
Paths 512

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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