Completed
Push — master ( 4b8173...652bbf )
by WEBEWEB
01:31
created

JQueryInputMaskTwigExtension   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 122
rs 10
c 0
b 0
f 0

9 Methods

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