Completed
Push — master ( f2652a...9e06dc )
by WEBEWEB
01:43
created

JQueryInputMaskPluginTwigExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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