Completed
Push — master ( 4d5688...be014c )
by WEBEWEB
01:51
created

ButtonTwigExtension::__construct()   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 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\CSS;
13
14
use Twig_SimpleFilter;
15
use Twig_SimpleFunction;
16
use WBW\Bundle\BootstrapBundle\Button\ButtonFactory;
17
use WBW\Library\Core\Argument\ArrayHelper;
18
use WBW\Library\Core\Argument\StringHelper;
19
20
/**
21
 * Button Twig extension.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\CSS
25
 * @link https://getbootstrap.com/docs/3.3/css/#buttons
26
 */
27
class ButtonTwigExtension extends AbstractButtonTwigExtension {
28
29
    /**
30
     * Service name.
31
     *
32
     * @var string
33
     */
34
    const SERVICE_NAME = "webeweb.bootstrap.twig.extension.css.button";
35
36
    /**
37
     * Displays a Bootstrap button "Danger".
38
     *
39
     * @param array $args The arguments.
40
     * @return string Returns the Bootstrap button "Danger".
41
     */
42
    public function bootstrapButtonDangerFunction(array $args = []) {
43
        return $this->bootstrapButton(ButtonFactory::parseDangerButton($args), ArrayHelper::get($args, "icon"));
44
    }
45
46
    /**
47
     * Displays a Bootstrap button "Default".
48
     *
49
     * @param array $args The arguments.
50
     * @return string Returns the Bootstrap button "Default".
51
     */
52
    public function bootstrapButtonDefaultFunction(array $args = []) {
53
        return $this->bootstrapButton(ButtonFactory::parseDefaultButton($args), ArrayHelper::get($args, "icon"));
54
    }
55
56
    /**
57
     * Displays a Bootstrap button "Info".
58
     *
59
     * @param array $args The arguments.
60
     * @return string Returns the Bootstrap button "Info".
61
     */
62
    public function bootstrapButtonInfoFunction(array $args = []) {
63
        return $this->bootstrapButton(ButtonFactory::parseInfoButton($args), ArrayHelper::get($args, "icon"));
64
    }
65
66
    /**
67
     * Transforms a Bootstrap button into an anchor.
68
     *
69
     * @param string $button The button.
70
     * @param string $href The href attribute.
71
     * @param string $target The target attribute.
72
     * @return string Returns the Bootstrap button transformed into an anchor.
73
     */
74
    public function bootstrapButtonLinkFilter($button, $href = self::DEFAULT_HREF, $target = null) {
75
76
        if (1 === preg_match("/disabled=\"disabled\"/", $button)) {
77
78
            $searches = [" disabled=\"disabled\"", "class=\""];
79
            $replaces = ["", "class=\"disabled "];
80
81
            $button = StringHelper::replace($button, $searches, $replaces);
82
        }
83
84
        $searches = ["<button", "type=\"button\"", "</button>"];
85
        $replaces = ["<a", "href=\"" . $href . "\"" . (null !== $target ? " target=\"" . $target . "\"" : ""), "</a>"];
86
87
        return StringHelper::replace($button, $searches, $replaces);
88
    }
89
90
    /**
91
     * Displays a Bootstrap button "Link".
92
     *
93
     * @param array $args The arguments.
94
     * @return string Returns the Bootstrap button "Link".
95
     */
96
    public function bootstrapButtonLinkFunction(array $args = []) {
97
        return $this->bootstrapButton(ButtonFactory::parseLinkButton($args), ArrayHelper::get($args, "icon"));
98
    }
99
100
    /**
101
     * Displays a Bootstrap button "Primary".
102
     *
103
     * @param array $args The arguments.
104
     * @return string Returns the Bootstrap button "Primary".
105
     */
106
    public function bootstrapButtonPrimaryFunction(array $args = []) {
107
        return $this->bootstrapButton(ButtonFactory::parsePrimaryButton($args), ArrayHelper::get($args, "icon"));
108
    }
109
110
    /**
111
     * Transforms a Bootstrap button into a submit button.
112
     *
113
     * @param string $bootstrapButton The bootstrap button.
114
     * @return string Returns the Bootstrap button transformed into a submit button.
115
     */
116
    public function bootstrapButtonSubmitFilter($bootstrapButton) {
117
        return StringHelper::replace($bootstrapButton, ["type=\"button\""], ["type=\"submit\""]);
118
    }
119
120
    /**
121
     * Displays a Bootstrap button "Success".
122
     *
123
     * @param array $args The arguments.
124
     * @return string Returns the Bootstrap button "Success".
125
     */
126
    public function bootstrapButtonSuccessFunction(array $args = []) {
127
        return $this->bootstrapButton(ButtonFactory::parseSuccessButton($args), ArrayHelper::get($args, "icon"));
128
    }
129
130
    /**
131
     * Displays a Bootstrap button "Warning".
132
     *
133
     * @param array $args The arguments.
134
     * @return string Returns the Bootstrap button "Warning".
135
     */
136
    public function bootstrapButtonWarningFunction(array $args = []) {
137
        return $this->bootstrapButton(ButtonFactory::parseWarningButton($args), ArrayHelper::get($args, "icon"));
138
    }
139
140
    /**
141
     * Get the Twig filters.
142
     *
143
     * @return Twig_SimpleFilter[] Returns the Twig filters.
144
     */
145
    public function getFilters() {
146
        return [
147
            new Twig_SimpleFilter("bootstrapButtonLink", [$this, "bootstrapButtonLinkFilter"], ["is_safe" => ["html"]]),
148
            new Twig_SimpleFilter("bsButtonLink", [$this, "bootstrapButtonLinkFilter"], ["is_safe" => ["html"]]),
149
150
            new Twig_SimpleFilter("bootstrapButtonSubmit", [$this, "bootstrapButtonSubmitFilter"], ["is_safe" => ["html"]]),
151
            new Twig_SimpleFilter("bsButtonSubmit", [$this, "bootstrapButtonSubmitFilter"], ["is_safe" => ["html"]]),
152
        ];
153
    }
154
155
    /**
156
     * Get the Twig functions.
157
     *
158
     * @return Twig_SimpleFunction[] Returns the Twig functions.
159
     */
160
    public function getFunctions() {
161
        return [
162
            new Twig_SimpleFunction("bootstrapButtonDanger", [$this, "bootstrapButtonDangerFunction"], ["is_safe" => ["html"]]),
163
            new Twig_SimpleFunction("bsButtonDanger", [$this, "bootstrapButtonDangerFunction"], ["is_safe" => ["html"]]),
164
165
            new Twig_SimpleFunction("bootstrapButtonDefault", [$this, "bootstrapButtonDefaultFunction"], ["is_safe" => ["html"]]),
166
            new Twig_SimpleFunction("bsButtonDefault", [$this, "bootstrapButtonDefaultFunction"], ["is_safe" => ["html"]]),
167
168
            new Twig_SimpleFunction("bootstrapButtonInfo", [$this, "bootstrapButtonInfoFunction"], ["is_safe" => ["html"]]),
169
            new Twig_SimpleFunction("bsButtonInfo", [$this, "bootstrapButtonInfoFunction"], ["is_safe" => ["html"]]),
170
171
            new Twig_SimpleFunction("bootstrapButtonLink", [$this, "bootstrapButtonLinkFunction"], ["is_safe" => ["html"]]),
172
            new Twig_SimpleFunction("bsButtonLink", [$this, "bootstrapButtonLinkFunction"], ["is_safe" => ["html"]]),
173
174
            new Twig_SimpleFunction("bootstrapButtonPrimary", [$this, "bootstrapButtonPrimaryFunction"], ["is_safe" => ["html"]]),
175
            new Twig_SimpleFunction("bsButtonPrimary", [$this, "bootstrapButtonPrimaryFunction"], ["is_safe" => ["html"]]),
176
177
            new Twig_SimpleFunction("bootstrapButtonSuccess", [$this, "bootstrapButtonSuccessFunction"], ["is_safe" => ["html"]]),
178
            new Twig_SimpleFunction("bsButtonSuccess", [$this, "bootstrapButtonSuccessFunction"], ["is_safe" => ["html"]]),
179
180
            new Twig_SimpleFunction("bootstrapButtonWarning", [$this, "bootstrapButtonWarningFunction"], ["is_safe" => ["html"]]),
181
            new Twig_SimpleFunction("bsButtonWarning", [$this, "bootstrapButtonWarningFunction"], ["is_safe" => ["html"]]),
182
        ];
183
    }
184
}
185