Completed
Push — master ( f4996e...e1ea0b )
by WEBEWEB
01:42
created

bootstrapButtonGroupToolbarFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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_SimpleFilter;
15
use Twig_SimpleFunction;
16
use WBW\Bundle\BootstrapBundle\BootstrapBundle;
17
use WBW\Library\Core\Utility\Argument\ArrayUtility;
18
use WBW\Library\Core\Utility\Argument\StringUtility;
19
20
/**
21
 * Button component Twig extension.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Component
25
 */
26
class ButtonComponentTwigExtension extends AbstractComponentTwigExtension {
27
28
    /**
29
     * Service name.
30
     *
31
     * @var string
32
     */
33
    const SERVICE_NAME = "webeweb.bootstrapbundle.twig.extension.component.button";
34
35
    /**
36
     * Constructor.
37
     */
38
    public function __construct() {
39
        parent::__construct();
40
    }
41
42
    /**
43
     * Displays a Bootstrap button "Danger".
44
     *
45
     * @param array $args The arguments.
46
     * @return string Returns the Bootstrap button "Danger".
47
     */
48
    public function bootstrapButtonDangerFunction(array $args = []) {
49
        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-" . BootstrapBundle::BOOTSTRAP_DANGER, ArrayUtility::get($args, "icon"));
50
    }
51
52
    /**
53
     * Displays a Bootstrap button "Default".
54
     *
55
     * @param array $args The arguments.
56
     * @return string Returns the Bootstrap button "Default".
57
     */
58
    public function bootstrapButtonDefaultFunction(array $args = []) {
59
        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-" . BootstrapBundle::BOOTSTRAP_DEFAULT, ArrayUtility::get($args, "icon"));
60
    }
61
62
    /**
63
     * Displays a Bootstrap button group "Basic".
64
     *
65
     * @param array $args The arguments.
66
     * @param array $buttons The buttons.
67
     * @return string Returns the Bootstrap button group "Basic".
68
     */
69
    public function bootstrapButtonGroupBasicFunction(array $args = [], array $buttons = []) {
70
        return $this->bootstrapButtonGroup("btn-group", "group", $buttons);
71
    }
72
73
    /**
74
     * Displays a Bootstrap button group "Toolbar".
75
     *
76
     * @param array $args The arguments.
77
     * @param array $buttons The buttons.
78
     * @return string Returns the Bootstrap button group "Toolbar".
79
     */
80
    public function bootstrapButtonGroupToolbarFunction(array $args = [], array $buttons = []) {
81
        return $this->bootstrapButtonGroup("btn-toolbar", "toolbar", $buttons);
82
    }
83
84
    /**
85
     * Displays a Bootstrap button "Info".
86
     *
87
     * @param array $args The arguments.
88
     * @return string Returns the Bootstrap button "Info".
89
     */
90
    public function bootstrapButtonInfoFunction(array $args = []) {
91
        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-" . BootstrapBundle::BOOTSTRAP_INFO, ArrayUtility::get($args, "icon"));
92
    }
93
94
    /**
95
     * Transforms a Bootstrap button into an anchor.
96
     *
97
     * @param string $bootstrapButton The bootstrap button.
98
     * @param string $href The href attribute.
99
     * @param string $target The target attribute.
100
     * @return string Returns the Bootstrap button transformed into an anchor.
101
     */
102
    public function bootstrapButtonLinkFilter($bootstrapButton, $href = self::DEFAULT_HREF, $target = null) {
103
        $searches = ["<button", "type=\"button\"", "</button>"];
104
        $replaces = ["<a", "href=\"" . $href . "\"" . (null !== $target ? " target=\"" . $target . "\"" : ""), "</a>"];
105
        return StringUtility::replace($bootstrapButton, $searches, $replaces);
106
    }
107
108
    /**
109
     * Displays a Bootstrap button "Link".
110
     *
111
     * @param array $args The arguments.
112
     * @return string Returns the Bootstrap button "Link".
113
     */
114
    public function bootstrapButtonLinkFunction(array $args = []) {
115
        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"));
116
    }
117
118
    /**
119
     * Displays a Bootstrap button "Primary".
120
     *
121
     * @param array $args The arguments.
122
     * @return string Returns the Bootstrap button "Primary".
123
     */
124
    public function bootstrapButtonPrimaryFunction(array $args = []) {
125
        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-" . BootstrapBundle::BOOTSTRAP_PRIMARY, ArrayUtility::get($args, "icon"));
126
    }
127
128
    /**
129
     * Transforms a Bootstrap button into a submit button.
130
     *
131
     * @param string $bootstrapButton The bootstrap button.
132
     * @return string Returns the Bootstrap button transformed into a submit button.
133
     */
134
    public function bootstrapButtonSubmitFilter($bootstrapButton) {
135
        return StringUtility::replace($bootstrapButton, ["type=\"button\""], ["type=\"submit\""]);
136
    }
137
138
    /**
139
     * Displays a Bootstrap button "Success".
140
     *
141
     * @param array $args The arguments.
142
     * @return string Returns the Bootstrap button "Success".
143
     */
144
    public function bootstrapButtonSuccessFunction(array $args = []) {
145
        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-" . BootstrapBundle::BOOTSTRAP_SUCCESS, ArrayUtility::get($args, "icon"));
146
    }
147
148
    /**
149
     * Displays a Bootstrap button "Warning".
150
     *
151
     * @param array $args The arguments.
152
     * @return string Returns the Bootstrap button "Warning".
153
     */
154
    public function bootstrapButtonWarningFunction(array $args = []) {
155
        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-" . BootstrapBundle::BOOTSTRAP_WARNING, ArrayUtility::get($args, "icon"));
156
    }
157
158
    /**
159
     * Get the Twig filters.
160
     *
161
     * @return Twig_SimpleFilter[] Returns the Twig filters.
162
     */
163
    public function getFilters() {
164
        return [
165
            new Twig_SimpleFilter("bootstrapButtonLink", [$this, "bootstrapButtonLinkFilter"], ["is_safe" => ["html"]]),
166
            new Twig_SimpleFilter("bootstrapButtonSubmit", [$this, "bootstrapButtonSubmitFilter"], ["is_safe" => ["html"]]),
167
        ];
168
    }
169
170
    /**
171
     * Get the Twig functions.
172
     *
173
     * @return Twig_SimpleFunction[] Returns the Twig functions.
174
     */
175
    public function getFunctions() {
176
        return [
177
            new Twig_SimpleFunction("bootstrapButtonDanger", [$this, "bootstrapButtonDangerFunction"], ["is_safe" => ["html"]]),
178
            new Twig_SimpleFunction("bootstrapButtonDefault", [$this, "bootstrapButtonDefaultFunction"], ["is_safe" => ["html"]]),
179
            new Twig_SimpleFunction("bootstrapButtonGroupBasic", [$this, "bootstrapButtonGroupBasicFunction"], ["is_safe" => ["html"]]),
180
            new Twig_SimpleFunction("bootstrapButtonGroupToolbar", [$this, "bootstrapButtonGroupToolbarFunction"], ["is_safe" => ["html"]]),
181
            new Twig_SimpleFunction("bootstrapButtonInfo", [$this, "bootstrapButtonInfoFunction"], ["is_safe" => ["html"]]),
182
            new Twig_SimpleFunction("bootstrapButtonLink", [$this, "bootstrapButtonLinkFunction"], ["is_safe" => ["html"]]),
183
            new Twig_SimpleFunction("bootstrapButtonPrimary", [$this, "bootstrapButtonPrimaryFunction"], ["is_safe" => ["html"]]),
184
            new Twig_SimpleFunction("bootstrapButtonSuccess", [$this, "bootstrapButtonSuccessFunction"], ["is_safe" => ["html"]]),
185
            new Twig_SimpleFunction("bootstrapButtonWarning", [$this, "bootstrapButtonWarningFunction"], ["is_safe" => ["html"]]),
186
        ];
187
    }
188
189
}
190