Completed
Push — master ( 9b9ad3...4d6e8b )
by WEBEWEB
02:01
created

bootstrapFormButtonSubmitFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Symfony\Component\Translation\TranslatorInterface;
15
use Twig_SimpleFunction;
16
use WBW\Bundle\BootstrapBundle\Twig\Extension\CSS\ButtonTwigExtension;
17
use WBW\Library\Core\Utility\Argument\ArrayUtility;
18
19
/**
20
 * Form button Twig extension.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Utility
24
 */
25
class FormButtonTwigExtension extends AbstractUtilityTwigExtension {
26
27
    /**
28
     * Service name.
29
     *
30
     * @var string
31
     */
32
    const SERVICE_NAME = "webeweb.bootstrapbundle.twig.extension.utility.formbutton";
33
34
    /**
35
     * Extension.
36
     *
37
     * @var ButtonTwigExtension
38
     */
39
    private $extension;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param TranslatorInterface $translator The translator.
45
     * @param ButtonTwigExtension $extension The button component Twig extension.
46
     */
47
    public function __construct(TranslatorInterface $translator, ButtonTwigExtension $extension) {
48
        parent::__construct($translator);
49
        $this->setExtension($extension);
50
    }
51
52
    /**
53
     * Displays a Bootstrap form button "Cancel".
54
     *
55
     * @param array $args The arguments.
56
     * @return string Returns the Bootstrap form button "Cancel".
57
     */
58
    public function bootstrapFormButtonCancelFunction(array $args = []) {
59
60
        // Translate the label.
61
        $txt = $this->getTranslator()->trans("label.cancel", [], "BootstrapBundle");
62
63
        // Initialize the button.
64
        $but = $this->getExtension()->bootstrapButtonDefaultFunction(["content" => $txt, "title" => $txt, "icon" => "remove"]);
65
66
        // Return the HTML.
67
        return $this->getExtension()->bootstrapButtonLinkFilter($but, ArrayUtility::get($args, "href", self::DEFAULT_HREF));
68
    }
69
70
    /**
71
     * Displays a Bootstrap form buttons "Default".
72
     *
73
     * @param array $args The arguments.
74
     * @return string Returns the Bootstrap form button "Default".
75
     */
76
    public function bootstrapFormButtonDefaultFunction(array $args = []) {
77
78
        // Initialize the buttons.
79
        $cancelButton = $this->bootstrapFormButtonCancelFunction(["href" => ArrayUtility::get($args, "cancel_href")]);
80
        $submitButton = $this->bootstrapFormButtonSubmitFunction();
81
82
        // Return the HTML.
83
        return implode(" ", [$cancelButton, $submitButton]);
84
    }
85
86
    /**
87
     * Displays a Bootstrap form button "Submit".
88
     *
89
     * @return string Returns the Bootstrap form button "Submit".
90
     */
91
    public function bootstrapFormButtonSubmitFunction() {
92
93
        // Translate the label.
94
        $txt = $this->getTranslator()->trans("label.submit", [], "BootstrapBundle");
95
96
        // Initialize the button.
97
        $but = $this->getExtension()->bootstrapButtonPrimaryFunction(["content" => $txt, "title" => $txt, "icon" => "ok"]);
98
99
        // Return the HTML.
100
        return $this->getExtension()->bootstrapButtonSubmitFilter($but);
101
    }
102
103
    /**
104
     * Get the extension.
105
     *
106
     * @return ButtonTwigExtension Returns the extension.
107
     */
108
    public function getExtension() {
109
        return $this->extension;
110
    }
111
112
    /**
113
     * Get the Twig functions.
114
     *
115
     * @return array Returns the Twig functions.
116
     */
117
    public function getFunctions() {
118
        return [
119
            new Twig_SimpleFunction("bootstrapFormButtonCancel", [$this, "bootstrapFormButtonCancelFunction"], ["is_safe" => ["html"]]),
120
            new Twig_SimpleFunction("bootstrapFormButtonDefault", [$this, "bootstrapFormButtonDefaultFunction"], ["is_safe" => ["html"]]),
121
            new Twig_SimpleFunction("bootstrapFormButtonSubmit", [$this, "bootstrapFormButtonSubmitFunction"], ["is_safe" => ["html"]]),
122
        ];
123
    }
124
125
    /**
126
     * Set the extension.
127
     *
128
     * @param ButtonTwigExtension $extension The extension.
129
     * @return FormButtonTwigExtension Returns this form button Twig extension.
130
     */
131
    protected function setExtension(ButtonTwigExtension $extension) {
132
        $this->extension = $extension;
133
        return $this;
134
    }
135
136
}
137