SonataTranslationExtension   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 56
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A __construct() 0 4 1
A setTranslatableChecker() 0 4 1
A getTranslatableChecker() 0 4 1
A getTests() 0 6 1
A getFilters() 0 6 1
A isTranslatable() 0 4 1
A getLocaleName() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\TranslationBundle\Twig\Extension;
15
16
use Sonata\TranslationBundle\Checker\TranslatableChecker;
17
use Symfony\Component\Intl\Locales;
18
use Twig\Extension\AbstractExtension;
19
use Twig\TwigFilter;
20
use Twig\TwigTest;
21
22
/**
23
 * @author Nicolas Bastien <[email protected]>
24
 */
25
class SonataTranslationExtension extends AbstractExtension
26
{
27
    /**
28
     * @var TranslatableChecker
29
     */
30
    protected $translatableChecker;
31
32
    public function __construct(TranslatableChecker $translatableChecker)
33
    {
34
        $this->translatableChecker = $translatableChecker;
35
    }
36
37
    public function getName(): string
38
    {
39
        return 'sonata_translation';
40
    }
41
42
    public function setTranslatableChecker(TranslatableChecker $translatableChecker): void
43
    {
44
        $this->translatableChecker = $translatableChecker;
45
    }
46
47
    public function getTranslatableChecker(): TranslatableChecker
48
    {
49
        return $this->translatableChecker;
50
    }
51
52
    public function getTests()
53
    {
54
        return [
55
            new TwigTest('translatable', [$this, 'isTranslatable']),
56
        ];
57
    }
58
59
    public function getFilters()
60
    {
61
        return [
62
            new TwigFilter('localeName', [$this, 'getLocaleName']),
63
        ];
64
    }
65
66
    /**
67
     * Check if $object is translatable.
68
     *
69
     * @param mixed $object
70
     */
71
    public function isTranslatable($object): bool
72
    {
73
        return $this->getTranslatableChecker()->isTranslatable($object);
74
    }
75
76
    public function getLocaleName(string $locale, ?string $displayLocale = null): ?string
77
    {
78
        return Locales::getName($locale, $displayLocale);
79
    }
80
}
81