IntlMessageFormatter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 1
b 0
f 0
dl 0
loc 31
ccs 0
cts 13
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 15 3
A __construct() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Translator;
6
7
use MessageFormatter;
8
use RuntimeException;
9
10
use function extension_loaded;
11
12
final class IntlMessageFormatter implements MessageFormatterInterface
13
{
14
    public function __construct()
15
    {
16
        if (!extension_loaded('intl')) {
17
            throw new RuntimeException(
18
                'In order to use intl message formatter, intl extension must be installed and enabled.'
19
            );
20
        }
21
    }
22
23
    /**
24
     * This method uses {{@see MessageFormatter::format()}}
25
     *
26
     * @link https://php.net/manual/en/messageformatter.format.php
27
     */
28
    public function format(string $message, array $parameters, string $locale): string
29
    {
30
        if ($parameters === []) {
31
            return $message;
32
        }
33
34
        $formatter = new MessageFormatter($locale, $message);
35
36
        $result = $formatter->format($parameters);
37
38
        if ($result === false) {
39
            return $message;
40
        }
41
42
        return $result;
43
    }
44
}
45