Completed
Push — master ( dfe684...c4d587 )
by WEBEWEB
06:45
created

JQueryInputMaskTwigExtension::getFunctions()   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 core-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\CoreBundle\Twig\Extension\Plugin;
13
14
use Twig_Environment;
15
use Twig_SimpleFunction;
16
use WBW\Bundle\CoreBundle\Twig\Extension\RendererTwigExtension;
17
use WBW\Library\Core\Argument\ArrayHelper;
18
19
/**
20
 * jQuery Input mask Twig Extension.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\CoreBundle\Twig\Extension\Plugin
24
 */
25
class JQueryInputMaskTwigExtension extends AbstractJQueryInputMaskTwigExtension {
26
27
    /**
28
     * Service name.
29
     *
30
     * @var string
31
     */
32
    const SERVICE_NAME = "webeweb.core.twig.extension.plugin.jquery_inputmask";
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param Twig_Environment $twigEnvironment The twig environment.
38
     * @param RendererTwigExtension $renderer The renderer.
39
     */
40
    public function __construct(Twig_Environment $twigEnvironment, RendererTwigExtension $renderer) {
41
        parent::__construct($twigEnvironment, $renderer);
42
    }
43
44
    /**
45
     * Displays a jQuery input mask.
46
     *
47
     * @param array $args The arguments.
48
     * @return string Returns the jQuery input mask.
49
     */
50
    public function jQueryInputMaskFunction(array $args = []) {
51
        return $this->jQueryInputMask(ArrayHelper::get($args, "selector"), ArrayHelper::get($args, "mask", ""), ArrayHelper::get($args, "opts", []), ArrayHelper::get($args, "scriptTag", false));
52
    }
53
54
    /**
55
     * Displays a jQuery input mask "Phone number".
56
     *
57
     * @param array $args The arguments.
58
     * @return string Returns the jQuery input mask "Phone number".
59
     */
60
    public function jQueryInputMaskPhoneNumberFunction(array $args = []) {
61
        $defaultMask = "99 99 99 99 99";
62
        return $this->jQueryInputMask(ArrayHelper::get($args, "selector"), $defaultMask, $this->prepareOptions($args, $defaultMask), ArrayHelper::get($args, "scriptTag", false));
63
    }
64
65
    /**
66
     * Displays a jQuery input mask "SIRET number".
67
     *
68
     * @param array $args The arguments.
69
     * @return string Returns the jQuery input mask "SIRET number".
70
     */
71
    public function jQueryInputMaskSIRETNumberFunction(array $args = []) {
72
        $defaultMask = "999 999 999 99999";
73
        return $this->jQueryInputMask(ArrayHelper::get($args, "selector"), $defaultMask, $this->prepareOptions($args, $defaultMask), ArrayHelper::get($args, "scriptTag", false));
74
    }
75
76
    /**
77
     * Displays a jQuery input mask "Social security number".
78
     *
79
     * @param array $args The arguments.
80
     * @return string Returns the jQuery input mask "Social security number".
81
     */
82
    public function jQueryInputMaskSocialSecurityNumberFunction(array $args = []) {
83
        $defaultMask = "9 99 99 99 999 999 99";
84
        return $this->jQueryInputMask(ArrayHelper::get($args, "selector"), $defaultMask, $this->prepareOptions($args, $defaultMask), ArrayHelper::get($args, "scriptTag", false));
85
    }
86
87
    /**
88
     * Displays a jQuery input mask "Time 12 hour".
89
     *
90
     * @param array $args The arguments.
91
     * @return string Returns the jQuery input mask "Time 12 hour".
92
     */
93
    public function jQueryInputMaskTime12Function(array $args = []) {
94
        $defaultMask = "hh:mm t";
95
        return $this->jQueryInputMask(ArrayHelper::get($args, "selector"), $defaultMask, array_merge($this->prepareOptions($args, null), ["hourFormat" => "12", "placeholder" => "__:__ _m"]), ArrayHelper::get($args, "scriptTag", false));
96
    }
97
98
    /**
99
     * Displays a jQuery input mask "Time 24 hour".
100
     *
101
     * @param array $args The arguments.
102
     * @return string Returns the jQuery input mask "Time 24 hour".
103
     */
104
    public function jQueryInputMaskTime24Function(array $args = []) {
105
        $defaultMask = "hh:mm";
106
        return $this->jQueryInputMask(ArrayHelper::get($args, "selector"), $defaultMask, array_merge($this->prepareOptions($args, null), ["hourFormat" => "24", "placeholder" => "__:__"]), ArrayHelper::get($args, "scriptTag", false));
107
    }
108
109
    /**
110
     * Displays a jQuery input mask "VAT number".
111
     *
112
     * @param array $args The arguments.
113
     * @return string Returns the jQuery input mask "VAT number".
114
     */
115
    public function jQueryInputMaskVATNumberFunction(array $args = []) {
116
        $defaultMask = "**999 999 999 99";
117
        return $this->jQueryInputMask(ArrayHelper::get($args, "selector"), $defaultMask, $this->prepareOptions($args, $defaultMask), ArrayHelper::get($args, "scriptTag", false));
118
    }
119
120
    /**
121
     * Get the Twig functions.
122
     *
123
     * @return array Returns the Twig functions.
124
     */
125
    public function getFunctions() {
126
        return [
127
            new Twig_SimpleFunction("jQueryInputMask", [$this, "jQueryInputMaskFunction"], ["is_safe" => ["html"]]),
128
            new Twig_SimpleFunction("jQueryInputMaskPhoneNumber", [$this, "jQueryInputMaskPhoneNumberFunction"], ["is_safe" => ["html"]]),
129
            new Twig_SimpleFunction("jQueryInputMaskSIRETNumber", [$this, "jQueryInputMaskSIRETNumberFunction"], ["is_safe" => ["html"]]),
130
            new Twig_SimpleFunction("jQueryInputMaskSocialSecurityNumber", [$this, "jQueryInputMaskSocialSecurityNumberFunction"], ["is_safe" => ["html"]]),
131
            new Twig_SimpleFunction("jQueryInputMaskTime12", [$this, "jQueryInputMaskTime12Function"], ["is_safe" => ["html"]]),
132
            new Twig_SimpleFunction("jQueryInputMaskTime24", [$this, "jQueryInputMaskTime24Function"], ["is_safe" => ["html"]]),
133
            new Twig_SimpleFunction("jQueryInputMaskVATNumber", [$this, "jQueryInputMaskVATNumberFunction"], ["is_safe" => ["html"]]),
134
        ];
135
    }
136
137
    /**
138
     * Prepare the arguments.
139
     *
140
     * @param array $args The arguments.
141
     * @param string $defaultMask The default mask.
142
     * @return array Returns the prepared arguments.
143
     */
144
    private function prepareOptions(array $args, $defaultMask) {
145
146
        // Initialize the options.
147
        $options = ArrayHelper::get($args, "opts", []);
148
149
        $options["autoUnmask"]         = ArrayHelper::get($options, "autoUnmask", true);
150
        $options["removeMaskOnSubmit"] = ArrayHelper::get($options, "removeMaskOnSubmit", true);
151
        if (null !== $defaultMask) {
152
            $options["placeholder"] = preg_replace("/[^\ ][.]*/", "_", $defaultMask);
153
        }
154
155
        // Return the arguments.
156
        return $options;
157
    }
158
159
}
160