Passed
Push — master ( f1bbc8...8d94c2 )
by Gabor
06:08
created

DriverAdapter::translate()   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 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\I18n\DriverAdapter\Gettext;
15
16
use RuntimeException;
17
use WebHemi\I18n\ServiceInterface;
18
use WebHemi\I18n\DriverInterface;
19
20
/**
21
 * Interface DriverInterface.
22
 *
23
 * @codeCoverageIgnore - uses PHP extension
24
 */
25
class DriverAdapter implements DriverInterface
26
{
27
    /** @var string */
28
    private $translationPath;
29
    /** @var ServiceInterface */
30
    private $i18nService;
31
    /** @var  */
32
    private $textDomain;
33
34
    /**
35
     * DriverInterface constructor.
36
     *
37
     * @param ServiceInterface $i18nService
38
     */
39
    public function __construct(ServiceInterface $i18nService)
40
    {
41
        $this->i18nService = $i18nService;
42
        $this->translationPath = realpath(__DIR__.'/../../Translation');
43
        $this->textDomain = 'messages';
44
45
        if (!extension_loaded('gettext') || !function_exists('bindtextdomain')) {
46
            throw new RuntimeException('The gettext module is not loaded!');
47
        }
48
49
        $this->initDriver();
50
    }
51
52
    /**
53
     * Initializes the driver.
54
     */
55
    private function initDriver()
56
    {
57
        bind_textdomain_codeset($this->textDomain, $this->i18nService->getCodeSet());
58
        bindtextdomain($this->textDomain, $this->translationPath);
59
        textdomain($this->textDomain);
60
    }
61
62
    /**
63
     * Translates the given text.
64
     *
65
     * @param string $text
66
     * @return string
67
     */
68
    public function translate(string $text) : string
69
    {
70
        return gettext($text);
71
    }
72
}
73