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%>%content%</div>"; |
38
|
|
|
$button = '<button class="close" type="button" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'; |
39
|
|
|
|
40
|
|
|
// Initialize the attributes. |
41
|
|
|
$_attr = []; |
42
|
|
|
|
43
|
|
|
$_attr["class"] = ["alert", $class]; |
44
|
|
|
$_attr["class"][] = true === $dismissible ? "alert-dismissible" : null; |
45
|
|
|
$_attr["role"] = ["alert"]; |
46
|
|
|
|
47
|
|
|
// Initialize the parameters. |
48
|
|
|
$_content = (true === $dismissible ? $button : "") . (null !== $content ? $content : self::DEFAULT_CONTENT); |
49
|
|
|
|
50
|
|
|
// Return the HTML. |
51
|
|
|
return StringUtility::replace($template, ["%attributes%", "%content%"], [StringUtility::parseArray($_attr), $_content]); |
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%>%content%</span>'; |
64
|
|
|
|
65
|
|
|
// Initialize the attributes. |
66
|
|
|
$_attr = []; |
67
|
|
|
|
68
|
|
|
$_attr["class"] = ["badge"]; |
69
|
|
|
|
70
|
|
|
// Initialize the parameters. |
71
|
|
|
$_content = null !== $content ? $content : self::DEFAULT_CONTENT; |
72
|
|
|
|
73
|
|
|
// Return the HTML. |
74
|
|
|
return StringUtility::replace($template, ["%attributes%", "%content%"], [StringUtility::parseArray($_attr), $_content]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Displays a Bootstrap glyphicon. |
79
|
|
|
* |
80
|
|
|
* @param string $name The glyphicon name. |
81
|
|
|
* @return string Returns the Bootstrap glyphicon. |
82
|
|
|
*/ |
83
|
|
|
final protected function bootstrapGlyphicon($name) { |
84
|
|
|
|
85
|
|
|
// Initialize the template. |
86
|
|
|
$template = "<span %attributes%></span>"; |
87
|
|
|
|
88
|
|
|
// Initialize the attributes. |
89
|
|
|
$_attr = []; |
90
|
|
|
|
91
|
|
|
$_attr["class"] = ["glyphicon"]; |
92
|
|
|
$_attr["class"][] = null !== $name ? "glyphicon-" . $name : null; |
93
|
|
|
$_attr["aria-hidden"] = "true"; |
94
|
|
|
|
95
|
|
|
// Return the HTML. |
96
|
|
|
return StringUtility::replace($template, ["%attributes%"], [StringUtility::parseArray($_attr)]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Displays a Bootstrap label. |
101
|
|
|
* |
102
|
|
|
* @param string $content The label content. |
103
|
|
|
* @param string $class The label class. |
104
|
|
|
* @return string Returns the Bootstrap label. |
105
|
|
|
*/ |
106
|
|
|
final protected function bootstrapLabel($content, $class) { |
107
|
|
|
|
108
|
|
|
// Initialize the template. |
109
|
|
|
$template = "<span %attributes%>%content%</span>"; |
110
|
|
|
|
111
|
|
|
// Initialize the attributes. |
112
|
|
|
$_attr = []; |
113
|
|
|
|
114
|
|
|
$_attr["class"] = ["label", $class]; |
115
|
|
|
|
116
|
|
|
// Initialize the parameters. |
117
|
|
|
$_content = null !== $content ? $content : self::DEFAULT_CONTENT; |
118
|
|
|
|
119
|
|
|
// Return the HTML. |
120
|
|
|
return StringUtility::replace($template, ["%attributes%", "%content%"], [StringUtility::parseArray($_attr), $_content]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Displays a Bootstrap progress bar. |
125
|
|
|
* |
126
|
|
|
* @param string $label The progress bar label. |
127
|
|
|
* @param integer $value The progress bar value. |
128
|
|
|
* @param integer $min The progress bar min. |
129
|
|
|
* @param integer $max The progress bar max. |
130
|
|
|
* @param boolean $striped Progress bar striped ? |
131
|
|
|
* @param boolean $animated Progress bar animated ? |
132
|
|
|
* @param string $class The progress bar class. |
133
|
|
|
* @return string Returns the Bootstrap progress bar. |
134
|
|
|
*/ |
135
|
|
|
final protected function bootstrapProgressBar($label, $value, $min, $max, $striped, $animated, $class = null) { |
136
|
|
|
|
137
|
|
|
// Initialize the template. |
138
|
|
|
$template = '<div class="progress"><div %attributes%>%label%</div></div>'; |
139
|
|
|
|
140
|
|
|
// Initialize the attributes. |
141
|
|
|
$_attr = []; |
142
|
|
|
|
143
|
|
|
$_attr["class"] = ["progress-bar", $class]; |
144
|
|
|
$_attr["class"][] = true === $striped ? "progress-bar-striped" : null; |
145
|
|
|
$_attr["class"][] = true === $animated ? "active" : null; |
146
|
|
|
$_attr["style"] = "width: " . $value . "%;"; |
147
|
|
|
$_attr["role"] = "progressbar"; |
148
|
|
|
$_attr["aria-valuenow"] = $value; |
149
|
|
|
$_attr["aria-valuemin"] = $min; |
150
|
|
|
$_attr["aria-valuemax"] = $max . "%"; |
151
|
|
|
|
152
|
|
|
// Initialize the parameters. |
153
|
|
|
$_label = !is_null($label) ? $label . '<span class="sr-only">' . $value . ' %</span>' : self::DEFAULT_CONTENT; |
154
|
|
|
|
155
|
|
|
// Return the HTML. |
156
|
|
|
return StringUtility::replace($template, ["%attributes%", "%label%"], [StringUtility::parseArray($_attr), $_label]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|