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 Twig_SimpleFunction; |
15
|
|
|
use WBW\Library\Core\Utility\ArrayUtility; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Input mask form Twig Extension. |
19
|
|
|
* |
20
|
|
|
* @author webeweb <https://github.com/webeweb/> |
21
|
|
|
* @package WBW\Bundle\BootstrapBundle\Twig\Extension\Form |
22
|
|
|
* @final |
23
|
|
|
*/ |
24
|
|
|
final class InputMaskFormTwigExtension extends AbstractFormTwigExtension { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Service name. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.twig.extension.form.inputmask"; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor. |
35
|
|
|
*/ |
36
|
|
|
public function __construct() { |
37
|
|
|
parent::__construct(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Displays a Bootstrap input mask. |
42
|
|
|
* |
43
|
|
|
* @param array $args The arguments. |
44
|
|
|
* @return string Returns the Bootstrap input mask. |
45
|
|
|
*/ |
46
|
|
|
public function bootstrapInputMaskFunction(array $args = []) { |
47
|
|
|
return $this->bootstrapInputMask(ArrayUtility::get($args, "selector"), ArrayUtility::get($args, "scriptTag", false), ArrayUtility::get($args, "opts", [])); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Displays a Bootstrap phone number input mask. |
52
|
|
|
* |
53
|
|
|
* @param array $args The arguments. |
54
|
|
|
* @return string Returns the Bootstrap phone number input mask. |
55
|
|
|
*/ |
56
|
|
|
public function bootstrapPhoneNumberInputMaskFunction(array $args = []) { |
57
|
|
|
return $this->bootstrapInputMask(ArrayUtility::get($args, "selector"), ArrayUtility::get($args, "scriptTag", false), $this->prepareOptions($args, "99 99 99 99 99")); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Displays a Bootstrap SIRET number input mask. |
62
|
|
|
* |
63
|
|
|
* @param array $args The arguments. |
64
|
|
|
* @return string Returns the Bootstrap SIRET number input mask. |
65
|
|
|
*/ |
66
|
|
|
public function bootstrapSIRETNumberInputMaskFunction(array $args = []) { |
67
|
|
|
return $this->bootstrapInputMask(ArrayUtility::get($args, "selector"), ArrayUtility::get($args, "scriptTag", false), $this->prepareOptions($args, "999 999 999 99999")); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Displays a Bootstrap social security number input mask. |
72
|
|
|
* |
73
|
|
|
* @param array $args The arguments. |
74
|
|
|
* @return string Returns the Bootstrap social security number input mask. |
75
|
|
|
*/ |
76
|
|
|
public function bootstrapSocialSecurityNumberInputMaskFunction(array $args = []) { |
77
|
|
|
return $this->bootstrapInputMask(ArrayUtility::get($args, "selector"), ArrayUtility::get($args, "scriptTag", false), $this->prepareOptions($args, "9 99 99 99 999 999 99")); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Displays a Bootstrap VAT number input mask. |
82
|
|
|
* |
83
|
|
|
* @param array $args The arguments. |
84
|
|
|
* @return string Returns the Bootstrap AT number input mask. |
85
|
|
|
*/ |
86
|
|
|
public function bootstrapVATNumberInputMaskFunction(array $args = []) { |
87
|
|
|
return $this->bootstrapInputMask(ArrayUtility::get($args, "selector"), ArrayUtility::get($args, "scriptTag", false), $this->prepareOptions($args, "**999 999 999 99")); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the Twig functions. |
92
|
|
|
* |
93
|
|
|
* @return array Returns the Twig functions. |
94
|
|
|
*/ |
95
|
|
|
public function getFunctions() { |
96
|
|
|
return [ |
97
|
|
|
new Twig_SimpleFunction("bootstrapInputMask", [$this, "bootstrapInputMaskFunction"], ["is_safe" => ["html"]]), |
98
|
|
|
new Twig_SimpleFunction("bootstrapPhoneNumberInputMask", [$this, "bootstrapPhoneNumberInputMaskFunction"], ["is_safe" => ["html"]]), |
99
|
|
|
new Twig_SimpleFunction("bootstrapSIRETNumberInputMask", [$this, "bootstrapSIRETNumberInputMaskFunction"], ["is_safe" => ["html"]]), |
100
|
|
|
new Twig_SimpleFunction("bootstrapSocialSecurityNumberInputMask", [$this, "bootstrapSocialSecurityNumberInputMaskFunction"], ["is_safe" => ["html"]]), |
101
|
|
|
new Twig_SimpleFunction("bootstrapVATNumberInputMask", [$this, "bootstrapVATNumberInputMaskFunction"], ["is_safe" => ["html"]]), |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Prepare the arguments. |
107
|
|
|
* |
108
|
|
|
* @param array $args The arguments. |
109
|
|
|
* @param string $defaultMask |
110
|
|
|
* @return array Returns the prepared arguments. |
111
|
|
|
*/ |
112
|
|
|
private function prepareOptions(array $args = [], $defaultMask) { |
113
|
|
|
|
114
|
|
|
// Initialize the options. |
115
|
|
|
$options = ArrayUtility::get($args, "opts", []); |
116
|
|
|
|
117
|
|
|
$options["autoUnmask"] = ArrayUtility::get($options, "autoUnmask", true); |
118
|
|
|
$options["removeMaskOnSubmit"] = ArrayUtility::get($options, "removeMaskOnSubmit", true); |
119
|
|
|
$options["mask"] = ArrayUtility::get($options, "mask", $defaultMask); |
120
|
|
|
if (null !== $defaultMask) { |
121
|
|
|
$options["placeholder"] = preg_replace("/[^\ ][.]*/", "_", $defaultMask); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Return the arguments. |
125
|
|
|
return $options; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|