Formatter::formatMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php declare (strict_types = 1);
2
3
namespace Limoncello\l10n\Format;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Contracts\L10n\FormatterInterface;
22
use Limoncello\l10n\Contracts\Format\TranslatorInterface;
23
use function assert;
24
25
/**
26
 * @package Limoncello\l10n
27
 */
28
class Formatter implements FormatterInterface
29
{
30
    /**
31
     * @var string
32
     */
33
    private $locale;
34
35
    /**
36
     * @var string
37
     */
38
    private $namespace;
39
40
    /**
41
     * @var TranslatorInterface
42
     */
43
    private $translator;
44
45
    /**
46
     * @param string              $locale
47
     * @param string              $namespace
48
     * @param TranslatorInterface $translator
49
     */
50 2
    public function __construct(string $locale, string $namespace, TranslatorInterface $translator)
51
    {
52 2
        assert(empty($locale) === false && empty($namespace) === false);
53
54 2
        $this->locale     = locale_canonicalize($locale);
55 2
        $this->namespace  = $namespace;
56 2
        $this->translator = $translator;
57
    }
58
59
    /**
60
     * @return string
61
     */
62 2
    public function getLocale(): string
63
    {
64 2
        return $this->locale;
65
    }
66
67
    /**
68
     * @return string
69
     */
70 2
    public function getNamespace(): string
71
    {
72 2
        return $this->namespace;
73
    }
74
75
    /**
76
     * @return TranslatorInterface
77
     */
78 2
    public function getTranslator(): TranslatorInterface
79
    {
80 2
        return $this->translator;
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 2
    public function formatMessage(string $message, array $args = []): string
87
    {
88 2
        $result = $this->getTranslator()->translateMessage($this->getLocale(), $this->getNamespace(), $message, $args);
89
90 2
        return $result;
91
    }
92
}
93