FormButtonTwigExtension   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 78
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A bootstrapFormButtonCancelFunction() 0 7 1
A bootstrapFormButtonDefaultFunction() 0 8 1
A bootstrapFormButtonSubmitFunction() 0 7 1
A getFunctions() 0 7 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\Utility;
13
14
use Twig\Environment;
15
use Twig\TwigFunction;
16
use WBW\Bundle\BootstrapBundle\Twig\Extension\CSS\ButtonTwigExtension;
17
use WBW\Bundle\BootstrapBundle\Twig\Extension\CSS\ButtonTwigExtensionTrait;
18
use WBW\Bundle\CoreBundle\Component\Translation\BaseTranslatorInterface;
19
use WBW\Library\Core\Argument\Helper\ArrayHelper;
20
21
/**
22
 * Form button Twig extension.
23
 *
24
 * @author webeweb <https://github.com/webeweb/>
25
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Utility
26
 */
27
class FormButtonTwigExtension extends AbstractUtilityTwigExtension {
28
29
    use ButtonTwigExtensionTrait;
30
31
    /**
32
     * Service name.
33
     *
34
     * @var string
35
     */
36
    const SERVICE_NAME = "wbw.bootstrap.twig.extension.utility.form_button";
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param Environment $twigEnvironment The Twig environment.
42
     * @param BaseTranslatorInterface $translator The translator.
43
     * @param ButtonTwigExtension $extension The button component Twig extension.
44
     */
45
    public function __construct(Environment $twigEnvironment, $translator, ButtonTwigExtension $extension) {
46
        parent::__construct($twigEnvironment, $translator);
47
        $this->setButtonTwigExtension($extension);
48
    }
49
50
    /**
51
     * Displays a Bootstrap form button "cancel".
52
     *
53
     * @param array $args The arguments.
54
     * @return string Returns the Bootstrap form button "cancel".
55
     */
56
    public function bootstrapFormButtonCancelFunction(array $args = []): string {
57
58
        $txt = $this->getTranslator()->trans("label.cancel", [], "WBWBootstrapBundle");
59
        $but = $this->getButtonTwigExtension()->bootstrapButtonDefaultFunction(["content" => $txt, "title" => $txt, "icon" => "fa:times"]);
60
61
        return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($but, ArrayHelper::get($args, "href", self::DEFAULT_HREF));
62
    }
63
64
    /**
65
     * Displays a Bootstrap form buttons "default".
66
     *
67
     * @param array $args The arguments.
68
     * @return string Returns the Bootstrap form button "default".
69
     */
70
    public function bootstrapFormButtonDefaultFunction(array $args = []): string {
71
72
        $cancelButton = $this->bootstrapFormButtonCancelFunction(["href" => ArrayHelper::get($args, "cancel_href")]);
73
        $submitButton = $this->bootstrapFormButtonSubmitFunction();
74
75
        // Return the HTML.
76
        return implode(" ", [$cancelButton, $submitButton]);
77
    }
78
79
    /**
80
     * Displays a Bootstrap form button "submit".
81
     *
82
     * @return string Returns the Bootstrap form button "sSubmit".
83
     */
84
    public function bootstrapFormButtonSubmitFunction(): string {
85
86
        $txt = $this->getTranslator()->trans("label.submit", [], "WBWBootstrapBundle");
87
        $but = $this->getButtonTwigExtension()->bootstrapButtonPrimaryFunction(["content" => $txt, "title" => $txt, "icon" => "fa:check"]);
88
89
        return $this->getButtonTwigExtension()->bootstrapButtonSubmitFilter($but);
90
    }
91
92
    /**
93
     * Get the Twig functions.
94
     *
95
     * @return TwigFunction[] Returns the Twig functions.
96
     */
97
    public function getFunctions(): array {
98
        return [
99
            new TwigFunction("bootstrapFormButtonCancel", [$this, "bootstrapFormButtonCancelFunction"], ["is_safe" => ["html"]]),
100
            new TwigFunction("bootstrapFormButtonDefault", [$this, "bootstrapFormButtonDefaultFunction"], ["is_safe" => ["html"]]),
101
            new TwigFunction("bootstrapFormButtonSubmit", [$this, "bootstrapFormButtonSubmitFunction"], ["is_safe" => ["html"]]),
102
        ];
103
    }
104
}
105