Completed
Pull Request — master (#19)
by Flo
04:12
created

Language::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Class LanguageLink | LanguageLink.php
4
 * @package Faulancer\View\Helper
5
 * @author  Florian Knapp <[email protected]>
6
 */
7
namespace Faulancer\View\Helper;
8
9
use Faulancer\Service\Config;
10
use Faulancer\Service\SessionManagerService;
11
use Faulancer\Session\SessionManager;
12
use Faulancer\View\AbstractViewHelper;
13
14
/**
15
 * Class LanguageLink
16
 */
17
class Language extends AbstractViewHelper
18
{
19
20
    /** @var array */
21
    protected $languageTextMapping = [
22
        'de' => 'Deutsch',
23
        'en' => 'English',
24
        'hr' => 'Hrvatski',
25
        'fr' => 'France'
26
    ];
27
28
    /**
29
     * @return Language $this
30
     */
31
    public function __invoke()
32
    {
33
        return $this;
34
    }
35
36
    /**
37
     * @param bool $codeOnly
38
     * @return string
39
     */
40
    public function getCurrent($codeOnly = true)
41
    {
42
        /** @var SessionManager $sessionManager */
43
        $sessionManager = $this->getServiceLocator()->get(SessionManagerService::class);
44
        $code           = $sessionManager->get('language') ?? 'de';
0 ignored issues
show
Bug Compatibility introduced by
The expression $sessionManager->get('language') ?? 'de'; of type string|array adds the type array to the return on line 47 which is incompatible with the return type documented by Faulancer\View\Helper\Language::getCurrent of type string.
Loading history...
45
46
        if ($codeOnly) {
47
            return $code;
48
        }
49
50
        return $this->languageTextMapping[$code];
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getLinks()
57
    {
58
        /** @var Config $config */
59
        $config       = $this->getServiceLocator()->get(Config::class);
60
        $translations = $config->get('translation');
61
62
        $result  = [];
63
        $pattern = '<a rel="alternate" hreflang="%s" class="lang %s" href="?lang=%s">%s</a>';
64
65
        foreach ($translations as $key => $content) {
66
            $result[] = sprintf($pattern, strtolower($key), strtolower($key), $key, $this->languageTextMapping[$key]);
67
        }
68
69
        return implode('', $result);
70
    }
71
72
}