Completed
Push — master ( 1df8f3...ce123c )
by Philip
07:02 queued 04:19
created

TwigExtensions::getLanguageName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the CRUDlex package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
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 CRUDlex;
13
14
use Pimple\Container;
15
use Symfony\Component\Intl\Intl;
16
17
/**
18
 * Provides and setups the Twig extensions like filters.
19
 */
20
class TwigExtensions {
21
22
    /**
23
     * Registers all extensions.
24
     *
25
     * @param Container $app
26
     * the current application
27
     */
28
    public function registerTwigExtensions(Container $app) {
29
        $self = $this;
30
        $app->extend('twig', function(\Twig_Environment $twig) use ($self) {
31
            $twig->addFilter(new \Twig_SimpleFilter('arrayColumn', [$self, 'arrayColumn']));
32
            $twig->addFilter(new \Twig_SimpleFilter('languageName', [$self, 'getLanguageName']));
33
            return $twig;
34
        });
35
    }
36
    /**
37
     * To have array_column available as Twig filter.
38
     *
39
     * @param $array
40
     * the array
41
     * @param $key
42
     * the key
43
     *
44
     * @return array
45
     * the resulting array
46
     */
47
    public function arrayColumn($array, $key) {
48
        return array_column($array, $key);
49
    }
50
51
    /**
52
     * Gets a language name in the given language.
53
     *
54
     * @param string $language
55
     * the language code of the desired language name
56
     *
57
     * @return string
58
     * the language name in the given language or null if not available
59
     */
60
    public function getLanguageName($language) {
61
        return Intl::getLanguageBundle()->getLanguageName($language, $language, $language);
62
    }
63
64
}
65