Completed
Pull Request — master (#9)
by Guido
04:20
created

L10n::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of the Twig WordPress package.
4
 *
5
 * (c) Guido Scialfa <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace TwigWp\Module;
14
15
final class L10n implements Injectable
16
{
17
    use Helper;
18
19
    const FILTER_L10N_LIST = 'twigwp.i10n_list';
20
21
    /**
22
     * Kses Functions list
23
     *
24
     * @var array The list of kses functions to register in twig
25
     */
26
    private $i10n;
27
28
    /**
29
     * Kses constructor
30
     *
31
     * @param array $i10n The list of kses functions to register into twig
32
     */
33
    public function __construct(array $i10n = [])
34
    {
35
        $this->i10n = $i10n;
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function injectInto(\Twig\Environment $twig): \Twig\Environment
42
    {
43
        $i10n = $this->i10n;
44
45
        /**
46
         * Filter i10n List to register
47
         *
48
         * @param array $i10n The current kses list.
49
         * @param \Twig\Environment $twig The twig environment instance.
50
         */
51
        $i10n = apply_filters(self::FILTER_L10N_LIST, $i10n, $twig);
52
53
        foreach ($i10n as $key => $function) {
54
            // Looking for options.
55
            list($function, $options) = $this->extractConfiguration((array)$function);
56
57
            $twig->addFunction(new \Twig\TwigFunction($key, $function, $options));
58
59
            if (strpos($key, 'esc_', 0) !== false) {
60
                $twig->addFilter(new \Twig\TwigFilter($key, $function, $options));
61
            }
62
        }
63
64
        return $twig;
65
    }
66
}
67