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 Symfony\Component\Routing\RouterInterface; |
15
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
16
|
|
|
use Twig_SimpleFunction; |
17
|
|
|
use WBW\Library\Core\Utility\Argument\ArrayUtility; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Input mask form Twig Extension. |
21
|
|
|
* |
22
|
|
|
* @author webeweb <https://github.com/webeweb/> |
23
|
|
|
* @package WBW\Bundle\BootstrapBundle\Twig\Extension\Form |
24
|
|
|
*/ |
25
|
|
|
class InputMaskFormTwigExtension extends AbstractFormTwigExtension { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Service name. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.twig.extension.form.inputmask"; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
* |
37
|
|
|
* @param RouterInterface $router The router. |
38
|
|
|
* @param TranslatorInterface $translator The translator. |
39
|
|
|
*/ |
40
|
|
|
public function __construct(RouterInterface $router, TranslatorInterface $translator) { |
41
|
|
|
parent::__construct($router, $translator); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Displays a Bootstrap input mask. |
46
|
|
|
* |
47
|
|
|
* @param array $args The arguments. |
48
|
|
|
* @return string Returns the Bootstrap input mask. |
49
|
|
|
*/ |
50
|
|
|
public function bootstrapInputMaskFunction(array $args = []) { |
51
|
|
|
return $this->bootstrapInputMask(ArrayUtility::get($args, "selector"), ArrayUtility::get($args, "scriptTag", false), ArrayUtility::get($args, "opts", [])); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Displays a Bootstrap input mask "Phone number". |
56
|
|
|
* |
57
|
|
|
* @param array $args The arguments. |
58
|
|
|
* @return string Returns the Bootstrap input mask "Phone number". |
59
|
|
|
*/ |
60
|
|
|
public function bootstrapInputMaskPhoneNumberFunction(array $args = []) { |
61
|
|
|
return $this->bootstrapInputMask(ArrayUtility::get($args, "selector"), ArrayUtility::get($args, "scriptTag", false), $this->prepareOptions($args, "99 99 99 99 99")); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Displays a Bootstrap input mask "SIRET number". |
66
|
|
|
* |
67
|
|
|
* @param array $args The arguments. |
68
|
|
|
* @return string Returns the Bootstrap input mask "SIRET number". |
69
|
|
|
*/ |
70
|
|
|
public function bootstrapInputMaskSIRETNumberFunction(array $args = []) { |
71
|
|
|
return $this->bootstrapInputMask(ArrayUtility::get($args, "selector"), ArrayUtility::get($args, "scriptTag", false), $this->prepareOptions($args, "999 999 999 99999")); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Displays a Bootstrap input mask "Social security number". |
76
|
|
|
* |
77
|
|
|
* @param array $args The arguments. |
78
|
|
|
* @return string Returns the Bootstrap input mask "Social security number". |
79
|
|
|
*/ |
80
|
|
|
public function bootstrapInputMaskSocialSecurityNumberFunction(array $args = []) { |
81
|
|
|
return $this->bootstrapInputMask(ArrayUtility::get($args, "selector"), ArrayUtility::get($args, "scriptTag", false), $this->prepareOptions($args, "9 99 99 99 999 999 99")); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Displays a Bootstrap input mask "Time 12 hour". |
86
|
|
|
* |
87
|
|
|
* @param array $args The arguments. |
88
|
|
|
* @return string Returns the Bootstrap input mask "Time 12 hour". |
89
|
|
|
*/ |
90
|
|
|
public function bootstrapInputMaskTime12Function(array $args = []) { |
91
|
|
|
return $this->bootstrapInputMask(ArrayUtility::get($args, "selector"), ArrayUtility::get($args, "scriptTag", false), array_merge($this->prepareOptions($args, "hh:mm t"), ["alias" => "time12", "hourFormat" => "12", "placeholder" => "__:__ _m"])); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Displays a Bootstrap input mask "Time 24 hour". |
96
|
|
|
* |
97
|
|
|
* @param array $args The arguments. |
98
|
|
|
* @return string Returns the Bootstrap input mask "Time 24 hour". |
99
|
|
|
*/ |
100
|
|
|
public function bootstrapInputMaskTime24Function(array $args = []) { |
101
|
|
|
return $this->bootstrapInputMask(ArrayUtility::get($args, "selector"), ArrayUtility::get($args, "scriptTag", false), array_merge($this->prepareOptions($args, "hh:mm"), ["alias" => "time24", "hourFormat" => "24", "placeholder" => "__:__ _m"])); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Displays a Bootstrap input mask "VAT number". |
106
|
|
|
* |
107
|
|
|
* @param array $args The arguments. |
108
|
|
|
* @return string Returns the Bootstrap input mask "VAT number". |
109
|
|
|
*/ |
110
|
|
|
public function bootstrapInputMaskVATNumberFunction(array $args = []) { |
111
|
|
|
return $this->bootstrapInputMask(ArrayUtility::get($args, "selector"), ArrayUtility::get($args, "scriptTag", false), $this->prepareOptions($args, "**999 999 999 99")); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the Twig functions. |
116
|
|
|
* |
117
|
|
|
* @return array Returns the Twig functions. |
118
|
|
|
*/ |
119
|
|
|
public function getFunctions() { |
120
|
|
|
return [ |
121
|
|
|
new Twig_SimpleFunction("bootstrapInputMask", [$this, "bootstrapInputMaskFunction"], ["is_safe" => ["html"]]), |
122
|
|
|
new Twig_SimpleFunction("bootstrapInputMaskPhoneNumber", [$this, "bootstrapInputMaskPhoneNumberFunction"], ["is_safe" => ["html"]]), |
123
|
|
|
new Twig_SimpleFunction("bootstrapInputMaskSIRETNumber", [$this, "bootstrapInputMaskSIRETNumberFunction"], ["is_safe" => ["html"]]), |
124
|
|
|
new Twig_SimpleFunction("bootstrapInputMaskSocialSecurityNumber", [$this, "bootstrapInputMaskSocialSecurityNumberFunction"], ["is_safe" => ["html"]]), |
125
|
|
|
new Twig_SimpleFunction("bootstrapInputMaskTime12", [$this, "bootstrapInputMaskTime12Function"], ["is_safe" => ["html"]]), |
126
|
|
|
new Twig_SimpleFunction("bootstrapInputMaskTime24", [$this, "bootstrapInputMaskTime24Function"], ["is_safe" => ["html"]]), |
127
|
|
|
new Twig_SimpleFunction("bootstrapInputMaskVATNumber", [$this, "bootstrapInputMaskVATNumberFunction"], ["is_safe" => ["html"]]), |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Prepare the arguments. |
133
|
|
|
* |
134
|
|
|
* @param array $args The arguments. |
135
|
|
|
* @param string $defaultMask |
136
|
|
|
* @return array Returns the prepared arguments. |
137
|
|
|
*/ |
138
|
|
|
private function prepareOptions(array $args = [], $defaultMask) { |
139
|
|
|
|
140
|
|
|
// Initialize the options. |
141
|
|
|
$options = ArrayUtility::get($args, "opts", []); |
142
|
|
|
|
143
|
|
|
$options["autoUnmask"] = ArrayUtility::get($options, "autoUnmask", true); |
144
|
|
|
$options["removeMaskOnSubmit"] = ArrayUtility::get($options, "removeMaskOnSubmit", true); |
145
|
|
|
$options["mask"] = ArrayUtility::get($options, "mask", $defaultMask); |
146
|
|
|
if (null !== $defaultMask) { |
147
|
|
|
$options["placeholder"] = preg_replace("/[^\ ][.]*/", "_", $defaultMask); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Return the arguments. |
151
|
|
|
return $options; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|