Completed
Push — master ( dfde71...be9027 )
by WEBEWEB
01:28
created

AbstractComponentTwigExtension::bootstrapButton()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
c 1
b 0
f 0
rs 4.9999
cc 8
eloc 14
nc 128
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["disabled"]    = true === $disable ? "disabled" : null;
106
107
        // Handle the parameters.
108
        $glyphicon = null !== $icon ? $this->bootstrapGlyphicon($icon, null) : "";
109
        $innerHTML = null !== $content ? $content : self::DEFAULT_CONTENT;
110
111
        // Return the HTML.
112
        return StringUtility::replace($template, ["%attributes%", "%glyphicon%", "%innerHTML%"], [StringUtility::parseArray($attributes), $glyphicon, $innerHTML]);
113
    }
114
115
    /**
116
     * Displays a Bootstrap glyphicon.
117
     *
118
     * @param string $name The glyphicon name.
119
     * @return string Returns the Bootstrap glyphicon.
120
     */
121
    final protected function bootstrapGlyphicon($name, $style) {
122
123
        // Initialize the template.
124
        $template = "<span %attributes%></span>";
125
126
        // Initialize the attributes.
127
        $attributes = [];
128
129
        $attributes["class"]       = ["glyphicon"];
130
        $attributes["class"][]     = null !== $name ? "glyphicon-" . $name : null;
131
        $attributes["aria-hidden"] = "true";
132
        $attributes["style"]       = $style;
133
134
        // Return the HTML.
135
        return StringUtility::replace($template, ["%attributes%"], [StringUtility::parseArray($attributes)]);
136
    }
137
138
    /**
139
     * Displays a Bootstrap label.
140
     *
141
     * @param string $content The label content.
142
     * @param string $class The label class.
143
     * @return string Returns the Bootstrap label.
144
     */
145
    final protected function bootstrapLabel($content, $class) {
146
147
        // Initialize the template.
148
        $template = "<span %attributes%>%innerHTML%</span>";
149
150
        // Initialize the attributes.
151
        $attributes = [];
152
153
        $attributes["class"] = ["label", $class];
154
155
        // Initialize the parameters.
156
        $innerHTML = null !== $content ? $content : self::DEFAULT_CONTENT;
157
158
        // Return the HTML.
159
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
160
    }
161
162
    /**
163
     * Displays a Bootstrap progress bar.
164
     *
165
     * @param string $content The progress bar content.
166
     * @param integer $value The progress bar value.
167
     * @param integer $min The progress bar min.
168
     * @param integer $max The progress bar max.
169
     * @param boolean $striped Progress bar striped ?
170
     * @param boolean $animated Progress bar animated ?
171
     * @param string $class The progress bar class.
172
     * @return string Returns the Bootstrap progress bar.
173
     */
174
    final protected function bootstrapProgressBar($content, $value, $min, $max, $striped, $animated, $class = null) {
175
176
        // Initialize the template.
177
        $template = '<div class="progress"><div %attributes%>%innerHTML%</div></div>';
178
179
        // Initialize the attributes.
180
        $attributes = [];
181
182
        $attributes["class"]         = ["progress-bar", $class];
183
        $attributes["class"][]       = true === $striped ? "progress-bar-striped" : null;
184
        $attributes["class"][]       = true === $animated ? "active" : null;
185
        $attributes["style"]         = "width: " . $value . "%;";
186
        $attributes["role"]          = "progressbar";
187
        $attributes["aria-valuenow"] = $value;
188
        $attributes["aria-valuemin"] = $min;
189
        $attributes["aria-valuemax"] = $max . "%";
190
191
        // Initialize the parameters.
192
        $innerHTML = !is_null($content) ? $content . '<span class="sr-only">' . $value . ' %</span>' : self::DEFAULT_CONTENT;
193
194
        // Return the HTML.
195
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
196
    }
197
198
}
199