Completed
Push — master ( 25c12d...6c5c5c )
by WEBEWEB
01:41
created

ButtonFormTwigExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\Form;
13
14
use Symfony\Component\Routing\RouterInterface;
15
use Symfony\Component\Translation\TranslatorInterface;
16
use Twig_SimpleFunction;
17
use WBW\Bundle\BootstrapBundle\Twig\Extension\Component\ButtonComponentTwigExtension;
18
use WBW\Library\Core\Utility\ArrayUtility;
19
20
/**
21
 * Button form Twig extension.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Form
25
 * @final
26
 */
27
final class ButtonFormTwigExtension extends AbstractFormTwigExtension {
28
29
    /**
30
     * Service name.
31
     *
32
     * @var string
33
     */
34
    const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.twig.extension.form.button";
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param RouterInterface $router The router.
40
     * @param TranslatorInterface $translator The translator.
41
     */
42
    public function __construct(RouterInterface $router, TranslatorInterface $translator) {
43
        parent::__construct($router, $translator);
44
    }
45
46
    /**
47
     * Displays a Bootstrap cancel form button.
48
     *
49
     * @param array $args The arguments.
50
     * @return string Returns the Bootstrap cancel form button.
51
     */
52
    public function bootstrapCancelFormButtonFunction(array $args = []) {
53
54
        // Translate the label.
55
        $txt = $this->translator->trans("label.cancel", [], "BootstrapBundle");
56
57
        // Generate the URL.
58
        $url = $this->router->generate(ArrayUtility::get($args, "route"), ArrayUtility::get($args, "arguments"));
59
60
        // Initialize the button.
61
        $ext = new ButtonComponentTwigExtension();
62
        $but = $ext->bootstrapButtonDefaultFunction(["content" => $txt, "title" => $txt, "icon" => "close"]);
63
64
        // Return the cancel form button.
65
        return $ext->bootstrapButtonLinkFilter($but, $url);
66
    }
67
68
    /**
69
     * Displays a Bootstrap default form buttons.
70
     *
71
     * @param array $args The arguments.
72
     * @return string Returns the Bootstrap default form buttons.
73
     */
74
    public function bootstrapDefaultFormButtonsFunction(array $args = []) {
75
        $cancelButton = $this->bootstrapCancelFormButtonFunction(["route" => ArrayUtility::get($args, "cancel_route"), "cancel_arguments" => ArrayUtility::get($args, "cancel_arguments")]);
76
        $submitButton = $this->bootstrapSubmitFormButtonFunction();
77
        return implode(" ", [$cancelButton, $submitButton]);
78
    }
79
80
    /**
81
     * Displays a Bootstrap submit form button.
82
     *
83
     * @return string Returns the Bootstrap submit form button.
84
     */
85
    public function bootstrapSubmitFormButtonFunction() {
86
87
        // Translate the label.
88
        $txt = $this->translator->trans("label.submit", [], "BootstrapBundle");
89
90
        // Initialize the button.
91
        $ext = new ButtonComponentTwigExtension();
92
        $but = $ext->bootstrapButtonPrimaryFunction(["content" => $txt, "title" => $txt, "icon" => "ok"]);
93
94
        // Return the submit form button.
95
        return $ext->bootstrapButtonSubmitFilter($but);
96
    }
97
98
    /**
99
     * Get the Twig functions.
100
     *
101
     * @return array Returns the Twig functions.
102
     */
103
    public function getFunctions() {
104
        return [
105
            new Twig_SimpleFunction('bootstrapCancelFormButton', [$this, 'bootstrapCancelFormButtonFunction'], ['is_safe' => ['html']]),
106
            new Twig_SimpleFunction('bootstrapDefaultFormButtons', [$this, 'bootstrapDefaultFormButtonsFunction'], ['is_safe' => ['html']]),
107
            new Twig_SimpleFunction('bootstrapSubmitFormButton', [$this, 'bootstrapSubmitFormButtonFunction'], ['is_safe' => ['html']]),
108
        ];
109
    }
110
111
}
112