Translator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 83
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A translateMessage() 0 10 2
A getStorage() 0 4 1
A setStorage() 0 6 1
B formatMessage() 0 22 6
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\l10n\Contracts\Format\TranslatorInterface;
22
use Limoncello\l10n\Contracts\Messages\BundleStorageInterface;
23
use MessageFormatter;
24
use function assert;
25
use function call_user_func;
26
use function is_object;
27
use function is_scalar;
28
use function method_exists;
29
30
/**
31
 * @package Limoncello\l10n
32
 */
33
class Translator implements TranslatorInterface
34
{
35
    /**
36
     * @var BundleStorageInterface
37
     */
38
    private $storage;
39
40
    /**
41
     * @param BundleStorageInterface $storage
42
     */
43 4
    public function __construct(BundleStorageInterface $storage)
44
    {
45 4
        $this->setStorage($storage);
46
    }
47
48
    /**
49
     * @inheritdoc
50
     *
51
     * @SuppressWarnings(PHPMD.ElseExpression)
52
     */
53 4
    public function translateMessage(string $locale, string $namespace, string $message, array $args = []): string
54
    {
55 4
        $translation = $this->getStorage()->get($locale, $namespace, $message);
56 4
        if ($translation !== null) {
57 4
            $message = $translation[BundleStorageInterface::INDEX_PAIR_VALUE];
58 4
            $locale  = $translation[BundleStorageInterface::INDEX_PAIR_LOCALE];
59
        }
60
61 4
        return static::formatMessage($locale, $message, $args);
62
    }
63
64
    /**
65
     * @return BundleStorageInterface
66
     */
67 4
    public function getStorage(): BundleStorageInterface
68
    {
69 4
        return $this->storage;
70
    }
71
72
    /**
73
     * @param BundleStorageInterface $storage
74
     *
75
     * @return TranslatorInterface
76
     */
77 4
    public function setStorage(BundleStorageInterface $storage): TranslatorInterface
78
    {
79 4
        $this->storage = $storage;
80
81 4
        return $this;
82
    }
83
84
    /**
85
     * @param string $locale
86
     * @param string $message
87
     * @param array  $args
88
     *
89
     * @return string
90
     *
91
     * @SuppressWarnings(PHPMD.StaticAccess)
92
     */
93 4
    protected static function formatMessage(string $locale, string $message, array $args): string
94
    {
95
        // underlying `format` method cannot work with arguments that are not convertible to string
96
        // therefore we have to check that only those that actually can be used
97
        assert(call_user_func(function () use ($args) : bool {
98 4
            $result = true;
99 4
            foreach ($args as $arg) {
100 1
                $result = $result &&
101 1
                    is_scalar($arg) === true ||
102 1
                    $arg === null ||
103 1
                    (is_object($arg) === true && method_exists($arg, '__toString') === true);
104
            }
105
106 4
            return $result;
107 4
        }));
108
109 4
        $formatter = MessageFormatter::create($locale, $message);
110 4
        $message   = $formatter->format($args);
111 4
        assert($message !== false, $formatter->getErrorMessage());
112
113 4
        return $message;
114
    }
115
}
116