Completed
Push — master ( be9027...bcb5d0 )
by WEBEWEB
01:29
created

bootstrapButtonSubmitFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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 Twig_SimpleFilter;
15
use Twig_SimpleFunction;
16
use WBW\Library\Core\Utility\ArrayUtility;
17
use WBW\Library\Core\Utility\StringUtility;
18
19
/**
20
 * ButtonComponentTwigExtension.
21
 *
22
 * @author Camille A. <[email protected]>
23
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Component
24
 */
25
final class ButtonComponentTwigExtension extends AbstractComponentTwigExtension {
26
27
    /**
28
     * Service name.
29
     *
30
     * @var string
31
     */
32
    const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.twig.extension.component.button";
33
34
    /**
35
     * Displays a Bootstrap button "Danger".
36
     *
37
     * @param array $args The arguments.
38
     * @return string Returns the Bootstrap button "Danger".
39
     */
40
    public function bootstrapButtonDangerFunction(array $args = []) {
41
        return $this->bootstrapButton(ArrayUtility::get($args, "content"), ArrayUtility::get($args, "title"), ArrayUtility::get($args, "size", false), ArrayUtility::get($args, "block", false), ArrayUtility::get($args, "active", false), ArrayUtility::get($args, "disable", false), "btn-danger", ArrayUtility::get($args, "icon"));
42
    }
43
44
    /**
45
     * Displays a Bootstrap button "Default".
46
     *
47
     * @param array $args The arguments.
48
     * @return string Returns the Bootstrap button "Default".
49
     */
50
    public function bootstrapButtonDefaultFunction(array $args = []) {
51
        return $this->bootstrapButton(ArrayUtility::get($args, "content"), ArrayUtility::get($args, "title"), ArrayUtility::get($args, "size", false), ArrayUtility::get($args, "block", false), ArrayUtility::get($args, "active", false), ArrayUtility::get($args, "disable", false), "btn-default", ArrayUtility::get($args, "icon"));
52
    }
53
54
    /**
55
     * Displays a Bootstrap button "Info".
56
     *
57
     * @param array $args The arguments.
58
     * @return string Returns the Bootstrap button "Info".
59
     */
60
    public function bootstrapButtonInfoFunction(array $args = []) {
61
        return $this->bootstrapButton(ArrayUtility::get($args, "content"), ArrayUtility::get($args, "title"), ArrayUtility::get($args, "size", false), ArrayUtility::get($args, "block", false), ArrayUtility::get($args, "active", false), ArrayUtility::get($args, "disable", false), "btn-info", ArrayUtility::get($args, "icon"));
62
    }
63
64
    /**
65
     * Transforms a Bootstrap button into an anchor.
66
     *
67
     * @param string $bootstrapButton The bootstrap button.
68
     * @param string $href The href attribute.
69
     * @return string Returns the Bootstrap button transformed into an anchor.
70
     */
71
    public function bootstrapButtonLinkFilter($bootstrapButton, $href = self::DEFAULT_HREF) {
72
        return StringUtility::replace($bootstrapButton, ["<button", "type=\"button\"", "</button>"], ["<a", "href=\"" . $href . "\"", "</a>"]);
73
    }
74
75
    /**
76
     * Displays a Bootstrap button "Link".
77
     *
78
     * @param array $args The arguments.
79
     * @return string Returns the Bootstrap button "Link".
80
     */
81
    public function bootstrapButtonLinkFunction(array $args = []) {
82
        return $this->bootstrapButton(ArrayUtility::get($args, "content"), ArrayUtility::get($args, "title"), ArrayUtility::get($args, "size", false), ArrayUtility::get($args, "block", false), ArrayUtility::get($args, "active", false), ArrayUtility::get($args, "disable", false), "btn-link", ArrayUtility::get($args, "icon"));
83
    }
84
85
    /**
86
     * Displays a Bootstrap button "Primary".
87
     *
88
     * @param array $args The arguments.
89
     * @return string Returns the Bootstrap button "Primary".
90
     */
91
    public function bootstrapButtonPrimaryFunction(array $args = []) {
92
        return $this->bootstrapButton(ArrayUtility::get($args, "content"), ArrayUtility::get($args, "title"), ArrayUtility::get($args, "size", false), ArrayUtility::get($args, "block", false), ArrayUtility::get($args, "active", false), ArrayUtility::get($args, "disable", false), "btn-primary", ArrayUtility::get($args, "icon"));
93
    }
94
95
    /**
96
     * Transforms a Bootstrap button into a submit button.
97
     *
98
     * @param string $bootstrapButton The bootstrap button.
99
     * @return string Returns the Bootstrap button transformed into a submit button.
100
     */
101
    public function bootstrapButtonSubmitFilter($bootstrapButton) {
102
        return StringUtility::replace($bootstrapButton, ["type=\"button\""], ["type=\"submit\""]);
103
    }
104
105
    /**
106
     * Displays a Bootstrap button "Success".
107
     *
108
     * @param array $args The arguments.
109
     * @return string Returns the Bootstrap button "Success".
110
     */
111
    public function bootstrapButtonSuccessFunction(array $args = []) {
112
        return $this->bootstrapButton(ArrayUtility::get($args, "content"), ArrayUtility::get($args, "title"), ArrayUtility::get($args, "size", false), ArrayUtility::get($args, "block", false), ArrayUtility::get($args, "active", false), ArrayUtility::get($args, "disable", false), "btn-success", ArrayUtility::get($args, "icon"));
113
    }
114
115
    /**
116
     * Displays a Bootstrap button "Warning".
117
     *
118
     * @param array $args The arguments.
119
     * @return string Returns the Bootstrap button "Warning".
120
     */
121
    public function bootstrapButtonWarningFunction(array $args = []) {
122
        return $this->bootstrapButton(ArrayUtility::get($args, "content"), ArrayUtility::get($args, "title"), ArrayUtility::get($args, "size", false), ArrayUtility::get($args, "block", false), ArrayUtility::get($args, "active", false), ArrayUtility::get($args, "disable", false), "btn-warning", ArrayUtility::get($args, "icon"));
123
    }
124
125
    /**
126
     * Get the Twig filters.
127
     *
128
     * @return Twig_SimpleFilter[] Returns the Twig filters.
129
     */
130
    public function getFilters() {
131
        return [
132
            new Twig_SimpleFilter("bootstrapButtonLink", [$this, "bootstrapButtonLinkFilter"], ["is_safe" => ["html"]]),
133
            new Twig_SimpleFilter("bootstrapButtonSubmit", [$this, "bootstrapButtonSubmitFilter"], ["is_safe" => ["html"]]),
134
        ];
135
    }
136
137
    /**
138
     * Get the Twig functions.
139
     *
140
     * @return Twig_SimpleFunction[] Returns the Twig functions.
141
     */
142
    public function getFunctions() {
143
        return [
144
            new Twig_SimpleFunction("bootstrapButtonDanger", [$this, "bootstrapButtonDangerFunction"], ["is_safe" => ["html"]]),
145
            new Twig_SimpleFunction("bootstrapButtonDefault", [$this, "bootstrapButtonDefaultFunction"], ["is_safe" => ["html"]]),
146
            new Twig_SimpleFunction("bootstrapButtonInfo", [$this, "bootstrapButtonInfoFunction"], ["is_safe" => ["html"]]),
147
            new Twig_SimpleFunction("bootstrapButtonLink", [$this, "bootstrapButtonLinkFunction"], ["is_safe" => ["html"]]),
148
            new Twig_SimpleFunction("bootstrapButtonPrimary", [$this, "bootstrapButtonPrimaryFunction"], ["is_safe" => ["html"]]),
149
            new Twig_SimpleFunction("bootstrapButtonSuccess", [$this, "bootstrapButtonSuccessFunction"], ["is_safe" => ["html"]]),
150
            new Twig_SimpleFunction("bootstrapButtonWarning", [$this, "bootstrapButtonWarningFunction"], ["is_safe" => ["html"]]),
151
        ];
152
    }
153
154
}
155