Passed
Pull Request — master (#78)
by Sergei
05:35 queued 02:50
created

IntlMessageFormatter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

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

2 Methods

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