Completed
Push — master ( a27d66...168bdb )
by WEBEWEB
05:24
created

bootstrapBadgePrimaryFunction()   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\Component;
13
14
use Twig\TwigFilter;
15
use Twig\TwigFunction;
16
use WBW\Bundle\BootstrapBundle\Badge\BadgeFactory;
17
use WBW\Library\Core\Argument\StringHelper;
18
19
/**
20
 * Badge component Twig extension.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Component
24
 * @link https://getbootstrap.com/docs/3.3/components/#badges
25
 */
26
class BadgeTwigExtension extends AbstractBadgeTwigExtension {
27
28
    /**
29
     * Service name.
30
     *
31
     * @var string
32
     */
33
    const SERVICE_NAME = "wbw.bootstrap.twig.extension.component.badge";
34
35
    /**
36
     * Displays a Bootstrap badge "danger".
37
     *
38
     * @param array $args The arguments.
39
     * @return string Returns the Bootstrap badge "danger".
40
     */
41
    public function bootstrapBadgeDangerFunction(array $args = []) {
42
        return $this->bootstrapBadge(BadgeFactory::parseDangerBadge($args));
43
    }
44
45
    /**
46
     * Displays a Bootstrap badge "dark".
47
     *
48
     * @param array $args The arguments.
49
     * @return string Returns the Bootstrap badge "dark".
50
     */
51
    public function bootstrapBadgeDarkFunction(array $args = []) {
52
        return $this->bootstrapBadge(BadgeFactory::parseDarkBadge($args));
53
    }
54
55
    /**
56
     * Displays a Bootstrap badge.
57
     *
58
     * @param array $args The arguments.
59
     * @return string Returns the Bootstrap badge.
60
     */
61
    public function bootstrapBadgeFunction(array $args = []) {
62
        return $this->bootstrapBadge(BadgeFactory::parseDefaultBadge($args));
63
    }
64
65
    /**
66
     * Displays a Bootstrap badge "info".
67
     *
68
     * @param array $args The arguments.
69
     * @return string Returns the Bootstrap badge "info".
70
     */
71
    public function bootstrapBadgeInfoFunction(array $args = []) {
72
        return $this->bootstrapBadge(BadgeFactory::parseInfoBadge($args));
73
    }
74
75
    /**
76
     * Displays a Bootstrap badge "light".
77
     *
78
     * @param array $args The arguments.
79
     * @return string Returns the Bootstrap badge "light".
80
     */
81
    public function bootstrapBadgeLightFunction(array $args = []) {
82
        return $this->bootstrapBadge(BadgeFactory::parseLightBadge($args));
83
    }
84
85
    /**
86
     * Transforms a Bootstrap badge into an anchor.
87
     *
88
     * @param string $button The button.
89
     * @param string $href The href attribute.
90
     * @param string $target The target attribute.
91
     * @return string Returns the Bootstrap badge transformed into an anchor.
92
     */
93
    public function bootstrapBadgeLinkFilter($button, $href = self::DEFAULT_HREF, $target = null) {
94
95
        $searches = ["<span", "</span>", "class="];
96
        $replaces = ["<a", "</a>", StringHelper::parseArray(["href" => $href, "target" => $target]) . " class="];
97
98
        return str_replace($searches, $replaces, $button);
99
    }
100
101
    /**
102
     * Displays a Bootstrap badge "primary".
103
     *
104
     * @param array $args The arguments.
105
     * @return string Returns the Bootstrap badge "primary".
106
     */
107
    public function bootstrapBadgePrimaryFunction(array $args = []) {
108
        return $this->bootstrapBadge(BadgeFactory::parsePrimaryBadge($args));
109
    }
110
111
    /**
112
     * Displays a Bootstrap badge "secondary".
113
     *
114
     * @param array $args The arguments.
115
     * @return string Returns the Bootstrap badge "secondary".
116
     */
117
    public function bootstrapBadgeSecondaryFunction(array $args = []) {
118
        return $this->bootstrapBadge(BadgeFactory::parseSecondaryBadge($args));
119
    }
120
121
    /**
122
     * Displays a Bootstrap badge "success".
123
     *
124
     * @param array $args The arguments.
125
     * @return string Returns the Bootstrap badge "success".
126
     */
127
    public function bootstrapBadgeSuccessFunction(array $args = []) {
128
        return $this->bootstrapBadge(BadgeFactory::parseSuccessBadge($args));
129
    }
130
131
    /**
132
     * Displays a Bootstrap badge "warning".
133
     *
134
     * @param array $args The arguments.
135
     * @return string Returns the Bootstrap badge "warning".
136
     */
137
    public function bootstrapBadgeWarningFunction(array $args = []) {
138
        return $this->bootstrapBadge(BadgeFactory::parseWarningBadge($args));
139
    }
140
141
    /**
142
     * Get the Twig filters.
143
     *
144
     * @return TwigFilter[] Returns the Twig filters.
145
     */
146
    public function getFilters() {
147
        return [
148
            new TwigFilter("bootstrapBadgeLink", [$this, "bootstrapBadgeLinkFilter"], ["is_safe" => ["html"]]),
149
            new TwigFilter("bsBadgeLink", [$this, "bootstrapBadgeLinkFilter"], ["is_safe" => ["html"]]),
150
        ];
151
    }
152
153
    /**
154
     * Get the Twig functions.
155
     *
156
     * @return TwigFunction[] Returns the Twig functions.
157
     */
158
    public function getFunctions() {
159
        return [
160
            new TwigFunction("bootstrapBadge", [$this, "bootstrapBadgeFunction"], ["is_safe" => ["html"]]),
161
            new TwigFunction("bsBadge", [$this, "bootstrapBadgeFunction"], ["is_safe" => ["html"]]),
162
163
            new TwigFunction("bootstrapBadgeDanger", [$this, "bootstrapBadgeDangerFunction"], ["is_safe" => ["html"]]),
164
            new TwigFunction("bsBadgeDanger", [$this, "bootstrapBadgeDangerFunction"], ["is_safe" => ["html"]]),
165
166
            new TwigFunction("bootstrapBadgeDark", [$this, "bootstrapBadgeDarkFunction"], ["is_safe" => ["html"]]),
167
            new TwigFunction("bsBadgeDark", [$this, "bootstrapBadgeDarkFunction"], ["is_safe" => ["html"]]),
168
169
            new TwigFunction("bootstrapBadgeInfo", [$this, "bootstrapBadgeInfoFunction"], ["is_safe" => ["html"]]),
170
            new TwigFunction("bsBadgeInfo", [$this, "bootstrapBadgeInfoFunction"], ["is_safe" => ["html"]]),
171
172
            new TwigFunction("bootstrapBadgeLight", [$this, "bootstrapBadgeLightFunction"], ["is_safe" => ["html"]]),
173
            new TwigFunction("bsBadgeLight", [$this, "bootstrapBadgeLightFunction"], ["is_safe" => ["html"]]),
174
175
            new TwigFunction("bootstrapBadgePrimary", [$this, "bootstrapBadgePrimaryFunction"], ["is_safe" => ["html"]]),
176
            new TwigFunction("bsBadgePrimary", [$this, "bootstrapBadgePrimaryFunction"], ["is_safe" => ["html"]]),
177
178
            new TwigFunction("bootstrapBadgeSecondary", [$this, "bootstrapBadgeSecondaryFunction"], ["is_safe" => ["html"]]),
179
            new TwigFunction("bsBadgeSecondary", [$this, "bootstrapBadgeSecondaryFunction"], ["is_safe" => ["html"]]),
180
181
            new TwigFunction("bootstrapBadgeSuccess", [$this, "bootstrapBadgeSuccessFunction"], ["is_safe" => ["html"]]),
182
            new TwigFunction("bsBadgeSuccess", [$this, "bootstrapBadgeSuccessFunction"], ["is_safe" => ["html"]]),
183
184
            new TwigFunction("bootstrapBadgeWarning", [$this, "bootstrapBadgeWarningFunction"], ["is_safe" => ["html"]]),
185
            new TwigFunction("bsBadgeWarning", [$this, "bootstrapBadgeWarningFunction"], ["is_safe" => ["html"]]),
186
        ];
187
    }
188
}
189