Completed
Push — master ( a3d6f2...653760 )
by WEBEWEB
02:00
created

AbstractComponentTwigExtension::setTranslator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Translation\TranslatorInterface;
16
use WBW\Bundle\BootstrapBundle\Helper\NavigationTreeHelper;
17
use WBW\Bundle\BootstrapBundle\Navigation\NavigationNode;
18
use WBW\Bundle\BootstrapBundle\Navigation\NavigationTree;
19
use WBW\Bundle\BootstrapBundle\Twig\Extension\AbstractBootstrapTwigExtension;
20
use WBW\Bundle\BootstrapBundle\Twig\Extension\BootstrapRendererTwigExtension;
21
use WBW\Library\Core\Utility\Argument\StringUtility;
22
23
/**
24
 * Abstract component Twig extension.
25
 *
26
 * @author webeweb <https://github.com/webeweb/>
27
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Component
28
 * @abstract
29
 */
30
abstract class AbstractComponentTwigExtension extends AbstractBootstrapTwigExtension {
31
32
    /**
33
     * Translator.
34
     *
35
     * @var TranslatorInterface
36
     */
37
    private $translator;
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param TranslatorInterface $translator The translator.
43
     */
44
    protected function __construct(TranslatorInterface $translator = null) {
45
        parent::__construct();
46
        $this->setTranslator($translator);
47
    }
48
49
    /**
50
     * Displays a Bootstrap alert.
51
     *
52
     * @param string $content The alert content.
53
     * @param boolean $dismissible Dismissible alert ?
54
     * @param string $class The alert class.
55
     * @return string Returns the Bootstrap alert.
56
     */
57
    protected function bootstrapAlert($content, $dismissible, $class) {
58
59
        // Initialize the templates.
60
        $template = "<div %attributes%>%innerHTML%</div>";
61
        $button   = "<button class=\"close\" type=\"button\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>";
62
63
        // Initialize the attributes.
64
        $attributes = [];
65
66
        $attributes["class"]   = ["alert", $class];
67
        $attributes["class"][] = true === $dismissible ? "alert-dismissible" : null;
68
        $attributes["role"]    = ["alert"];
69
70
        // Initialize the parameters.
71
        $innerHTML = (true === $dismissible ? $button : "") . (null !== $content ? $content : "");
72
73
        // Return the HTML.
74
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
75
    }
76
77
    /**
78
     * Displays a Bootstrap badge.
79
     *
80
     * @param string $content The badge content.
81
     * @return string Returns the Bootstrap badge.
82
     */
83
    protected function bootstrapBadge($content) {
84
85
        // Initialize the template.
86
        $template = "<span %attributes%>%innerHTML%</span>";
87
88
        // Initialize the attributes.
89
        $attributes = [];
90
91
        $attributes["class"] = ["badge"];
92
93
        // Initialize the parameters.
94
        $innerHTML = null !== $content ? $content : "";
95
96
        // Return the HTML.
97
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
98
    }
99
100
    /**
101
     * Displays a Bootstrap breadcrumb.
102
     *
103
     * @param NavigationNode $node The navigation node.
104
     * @param boolean $last Last node ?.
105
     * @return string Returns the Bootstrap breadcrumb.
106
     */
107
    private function bootstrapBreadcrumb(NavigationNode $node, $last) {
108
109
        // Initialize the template.
110
        $template = "<li%attributes%>%innerHTML%</li>";
111
112
        // Initialize the parameters.
113
        $content = $this->getTranslator()->trans($node->getId());
114
115
        $attributes = true === $node->getActive() && true == $last ? " class=\"active\"" : "";
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
116
        $innerHTML  = true === $last ? $content : $this->bootstrapDOMObject("a", "href=\"" . $node->getRoute() . "\"", $content);
117
118
        // Return the HTML.
119
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [$attributes, $innerHTML]);
120
    }
121
122
    /**
123
     * Displays a Bootstrap breadcrumbs.
124
     *
125
     * @param NavigationTree $tree The tree.
126
     * @param Request $request The request.
127
     * @return string Returns the Bootstrap breadcrumbs.
128
     */
129
    protected function bootstrapBreadcrumbs(NavigationTree $tree, Request $request) {
130
131
        // Initialize the template.
132
        $template = "<ol %attributes%>\n%innerHTML%\n</ol>";
133
134
        // Initialize the attributes.
135
        $attributes = [];
136
137
        $attributes["class"] = ["breadcrumb"];
138
139
        // Initialize the parameters.
140
        $innerHTML = [];
141
142
        // Get the breadcrumb node.
143
        $nodes = NavigationTreeHelper::getBreadcrumbs($tree, $request);
0 ignored issues
show
Unused Code introduced by
The call to NavigationTreeHelper::getBreadcrumbs() has too many arguments starting with $request.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
144
        $count = count($nodes);
145
146
        // Handle each breadcrumb node.
147
        for ($i = 0; $i < $count; ++$i) {
148
            if (false === ($nodes[$i] instanceOf NavigationNode)) {
149
                continue;
150
            }
151
            $innerHTML[] = $this->bootstrapBreadcrumb($nodes[$i], $count === $i + 1);
152
        }
153
154
        // Return the HTML.
155
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), implode("\n", $innerHTML)]);
156
    }
157
158
    /**
159
     * Displays a Bootstrap button.
160
     *
161
     * @param string $content The button content.
162
     * @param string $title The button title.
163
     * @param string $size The button size.
164
     * @param boolean $block Block button ?
165
     * @param booelan $active Active button ?
166
     * @param booelan $disable Disable button ?
167
     * @param string $class The button class.
168
     * @param string $icon The button icon.
169
     * @return string Returns the Bootstrap button.
170
     */
171
    protected function bootstrapButton($content, $title, $size, $block, $active, $disable, $class, $icon) {
172
173
        // Initialize the template.
174
        $template = "<button %attributes%>%glyphicon%%innerHTML%</button>";
175
176
        // Initialize the attributes.
177
        $attributes = [];
178
179
        $attributes["class"]          = ["btn", $class];
180
        $attributes["class"][]        = true === $block ? "btn-block" : null;
181
        $attributes["class"][]        = true === in_array($size, ["lg", "sm", "xs"]) ? "btn-" . $size : null;
182
        $attributes["class"][]        = true === $active ? "active" : null;
183
        $attributes["title"]          = $title;
184
        $attributes["type"]           = "button";
185
        $attributes["data-toggle"]    = null !== $title ? "tooltip" : null;
186
        $attributes["data-placement"] = null !== $title ? "top" : null;
187
        $attributes["disabled"]       = true === $disable ? "disabled" : null;
188
189
        // Handle the parameters.
190
        $glyphicon = null !== $icon ? BootstrapRendererTwigExtension::renderIcon($icon) : "";
191
        $innerHTML = null !== $content ? ("" !== $glyphicon ? " " . $content : $content) : "";
192
193
        // Return the HTML.
194
        return StringUtility::replace($template, ["%attributes%", "%glyphicon%", "%innerHTML%"], [StringUtility::parseArray($attributes), $glyphicon, $innerHTML]);
195
    }
196
197
    /**
198
     * Displays a Bootstrap button group.
199
     *
200
     * @param string $class The class.
201
     * @param string $role The role.
202
     * @param array $buttons The buttons.
203
     * @return string Returns the Bootstrap button group.
204
     */
205
    protected function bootstrapButtonGroup($class, $role, array $buttons) {
206
207
        // Initialize the template.
208
        $template = "<div %attributes%>\n%innerHTML%\n</div>";
209
210
        // Initialize the attributes.
211
        $attributes = [];
212
213
        $attributes["class"] = $class;
214
        $attributes["role"]  = $role;
215
216
        // Initialize the parameters.
217
        $innerHTML = implode("\n", $buttons);
218
219
        // Return the HTML.
220
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
221
    }
222
223
    /**
224
     * Displays a Bootstrap glyphicon.
225
     *
226
     * @param string $name The glyphicon name.
227
     * @return string Returns the Bootstrap glyphicon.
228
     */
229
    protected function bootstrapGlyphicon($name, $style) {
230
231
        // Initialize the template.
232
        $template = "<span %attributes%></span>";
233
234
        // Initialize the attributes.
235
        $attributes = [];
236
237
        $attributes["class"]       = ["glyphicon"];
238
        $attributes["class"][]     = null !== $name ? "glyphicon-" . $name : null;
239
        $attributes["aria-hidden"] = "true";
240
        $attributes["style"]       = $style;
241
242
        // Return the HTML.
243
        return StringUtility::replace($template, ["%attributes%"], [StringUtility::parseArray($attributes)]);
244
    }
245
246
    /**
247
     * Displays a Bootstrap label.
248
     *
249
     * @param string $content The label content.
250
     * @param string $class The label class.
251
     * @return string Returns the Bootstrap label.
252
     */
253
    protected function bootstrapLabel($content, $class) {
254
255
        // Initialize the template.
256
        $template = "<span %attributes%>%innerHTML%</span>";
257
258
        // Initialize the attributes.
259
        $attributes = [];
260
261
        $attributes["class"] = ["label", $class];
262
263
        // Initialize the parameters.
264
        $innerHTML = null !== $content ? $content : "";
265
266
        // Return the HTML.
267
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
268
    }
269
270
    /**
271
     * Displays a Bootstrap progress bar.
272
     *
273
     * @param string $content The progress bar content.
274
     * @param integer $value The progress bar value.
275
     * @param integer $min The progress bar min.
276
     * @param integer $max The progress bar max.
277
     * @param boolean $striped Progress bar striped ?
278
     * @param boolean $animated Progress bar animated ?
279
     * @param string $class The progress bar class.
280
     * @return string Returns the Bootstrap progress bar.
281
     */
282
    protected function bootstrapProgressBar($content, $value, $min, $max, $striped, $animated, $class = null) {
283
284
        // Initialize the template.
285
        $template = "<div class=\"progress\"><div %attributes%>%innerHTML%</div></div>";
286
        $span     = "<span class=\"sr-only\">%value%%</span>";
287
288
        // Initialize the attributes.
289
        $attributes = [];
290
291
        $attributes["class"]         = ["progress-bar", $class];
292
        $attributes["class"][]       = true === $striped ? "progress-bar-striped" : null;
293
        $attributes["class"][]       = true === $animated ? "active" : null;
294
        $attributes["style"]         = "width: " . $value . "%;";
295
        $attributes["role"]          = "progressbar";
296
        $attributes["aria-valuenow"] = $value;
297
        $attributes["aria-valuemin"] = $min;
298
        $attributes["aria-valuemax"] = $max . "%";
299
300
        // Initialize the parameters.
301
        $innerHTML = null !== $content ? $content : StringUtility::replace($span, ["%value%"], [$value]);
302
303
        // Return the HTML.
304
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
305
    }
306
307
    /**
308
     * Get the translator.
309
     *
310
     * @return TranslatorInterface Returns the translator.
311
     */
312
    public function getTranslator() {
313
        return $this->translator;
314
    }
315
316
    /**
317
     * Set the translator.
318
     * @param TranslatorInterface $translator The translator.
319
     * @return AbstractComponentTwigExtension Returns this component Twig extension.
320
     */
321
    protected function setTranslator(TranslatorInterface $translator = null) {
322
        $this->translator = $translator;
323
        return $this;
324
    }
325
326
}
327