AbstractJQueryInputMaskTwigExtension   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 9
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A jQueryInputMask() 0 10 2
A __construct() 0 4 1
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\Assets;
13
14
use Twig\Environment;
15
use WBW\Bundle\CoreBundle\Twig\Extension\AbstractTwigExtension;
16
use WBW\Bundle\CoreBundle\Twig\Extension\AssetsTwigExtension;
17
use WBW\Bundle\CoreBundle\Twig\Extension\AssetsTwigExtensionTrait;
18
19
/**
20
 * Abstract jQuery input mask Twig extension.
21
 *
22
 * @author webeweb <https://github.com/webeweb>
23
 * @package WBW\Bundle\Core\Twig\Extension\Assets
24
 * @abstract
25
 */
26
abstract class AbstractJQueryInputMaskTwigExtension extends AbstractTwigExtension {
27
28
    use AssetsTwigExtensionTrait;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param Environment $twigEnvironment The Twig environment.
34
     * @param AssetsTwigExtension $rendererTwigExtension The renderer Twig extension.
35
     */
36
    public function __construct(Environment $twigEnvironment, AssetsTwigExtension $rendererTwigExtension) {
37
        parent::__construct($twigEnvironment);
38
39
        $this->setAssetsTwigExtension($rendererTwigExtension);
40
    }
41
42
    /**
43
     * Display a jQuery input mask.
44
     *
45
     * @param string $selector The selector.
46
     * @param string $mask The mask.
47
     * @param array $options The options.
48
     * @param bool $scriptTag Script tag ?
49
     * @return string Returns the jQuery input mask.
50
     */
51
    protected function jQueryInputMask(string $selector, string $mask, array $options, bool $scriptTag): string {
52
53
        $template = '$(\'%selector%\').inputmask("%mask%",%arguments%);';
54
55
        $innerHTML = str_replace(["%selector%", "%mask%", "%arguments%"], [$selector, $mask, json_encode($options)], $template);
56
57
        if (true === $scriptTag) {
58
            return $this->getAssetsTwigExtension()->coreScriptFilter($innerHTML);
59
        }
60
        return $innerHTML;
61
    }
62
}
63