Completed
Push — master ( b5c247...10654e )
by WEBEWEB
01:40
created

bootstrapLinkAlertFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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 Twig_SimpleFunction;
15
use WBW\Bundle\BootstrapBundle\Navigation\NavigationInterface;
16
use WBW\Library\Core\Utility\ArrayUtility;
17
use WBW\Library\Core\Utility\StringUtility;
18
19
/**
20
 * Alert component Twig extension.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Component
24
 * @final
25
 */
26
final class AlertComponentTwigExtension extends AbstractComponentTwigExtension {
27
28
    /**
29
     * Service name.
30
     *
31
     * @var string
32
     */
33
    const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.twig.extension.component.alert";
34
35
    /**
36
     * Constructor.
37
     */
38
    public function __construct() {
39
        parent::__construct();
40
    }
41
42
    /**
43
     * Displays a Bootstrap alert "Danger".
44
     *
45
     * @param array $args The arguments.
46
     * @return string Returns the Bootstrap alert "Danger".
47
     */
48
    public function bootstrapAlertDangerFunction(array $args = []) {
49
        return $this->bootstrapAlert(ArrayUtility::get($args, "content"), ArrayUtility::get($args, "dismissible"), "alert-danger");
50
    }
51
52
    /**
53
     * Displays a Bootstrap alert "Info".
54
     *
55
     * @param array $args The arguments.
56
     * @return string Returns the Bootstrap alert "Info".
57
     */
58
    public function bootstrapAlertInfoFunction(array $args = []) {
59
        return $this->bootstrapAlert(ArrayUtility::get($args, "content"), ArrayUtility::get($args, "dismissible"), "alert-info");
60
    }
61
62
    /**
63
     * Displays a Bootstrap alert "Success".
64
     *
65
     * @param array $args The arguments.
66
     * @return string Returns the Bootstrap alert "Success".
67
     */
68
    public function bootstrapAlertSuccessFunction(array $args = []) {
69
        return $this->bootstrapAlert(ArrayUtility::get($args, "content"), ArrayUtility::get($args, "dismissible"), "alert-success");
70
    }
71
72
    /**
73
     * Displays a Bootstrap alert "Warning".
74
     *
75
     * @param array $args The arguments.
76
     * @return string Returns the Bootstrap alert "Warning".
77
     */
78
    public function bootstrapAlertWarningFunction(array $args = []) {
79
        return $this->bootstrapAlert(ArrayUtility::get($args, "content"), ArrayUtility::get($args, "dismissible"), "alert-warning");
80
    }
81
82
    /**
83
     * Get the Twig functions.
84
     *
85
     * @return Twig_SimpleFunction[] Returns the Twig functions.
86
     */
87
    public function getFunctions() {
88
        return [
89
            new Twig_SimpleFunction("bootstrapAlertDanger", [$this, "bootstrapAlertDangerFunction"], ["is_safe" => ["html"]]),
90
            new Twig_SimpleFunction("bootstrapAlertInfo", [$this, "bootstrapAlertInfoFunction"], ["is_safe" => ["html"]]),
91
            new Twig_SimpleFunction("bootstrapAlertSuccess", [$this, "bootstrapAlertSuccessFunction"], ["is_safe" => ["html"]]),
92
            new Twig_SimpleFunction("bootstrapAlertWarning", [$this, "bootstrapAlertWarningFunction"], ["is_safe" => ["html"]]),
93
            new Twig_SimpleFunction("bootstrapLinkAlert", [$this, "bootstrapLinkAlertFunction"], ["is_safe" => ["html"]]),
94
        ];
95
    }
96
97
    /**
98
     * Displays a Bootstrap link alert.
99
     *
100
     * @param array $args The arguments.
101
     * @return string Returns the Bootstrap link alert.
102
     */
103
    public function bootstrapLinkAlertFunction(array $args = []) {
104
105
        // Initialize the template.
106
        $template = "<a %attributes%>%innerHTML%</a>";
107
108
        // Initialiize the attributes.
109
        $attributes["href"] = ArrayUtility::get($args, "href", NavigationInterface::DEFAULT_HREF);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attributes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
110
111
        // Initialize the parameters.
112
        $innerHTML = ArrayUtility::get($args, "content");
113
114
        // Return the HTML.
115
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
116
    }
117
118
}
119