IntlFormatter::format()   A
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 17
c 1
b 1
f 0
nc 24
nop 3
dl 0
loc 30
ccs 17
cts 17
cp 1
crap 6
rs 9.0777
1
<?php
2
/**
3
 *
4
 * This file is part of the Aura Project for PHP.
5
 *
6
 * @package aura/intl
7
 *
8
 * @license http://opensource.org/licenses/MIT MIT
9
 *
10
 */
11
namespace Aura\Intl;
12
13
use MessageFormatter;
14
use Aura\Intl\Exception;
15
16
/**
17
 *
18
 * Uses php intl extension to format messages
19
 *
20
 * @package aura/intl
21
 *
22
 */
23
class IntlFormatter implements FormatterInterface
24
{
25
    /**
26
     *
27
     * Constructor.
28
     *
29
     * @param string $icu_version The current ICU version; mostly used for
30
     * testing.
31
     *
32
     * @throws Exception\IcuVersionTooLow when the Version of ICU installed
33
     * is too low for Aura.Intl to work properly.
34
     *
35
     */
36 13
    public function __construct($icu_version = INTL_ICU_VERSION)
37
    {
38 13
        if (version_compare($icu_version, '4.8') < 0) {
39 1
            throw new Exception\IcuVersionTooLow('ICU Version 4.8 or higher required.');
40
        }
41 12
    }
42
43
    /**
44
     *
45
     * Format the message with the help of php intl extension
46
     *
47
     * @param string $locale
48
     * @param string $string
49
     * @param array $tokens_values
50
     * @return string
51
     * @throws Exception
52
     */
53 12
    public function format($locale, $string, array $tokens_values)
54
    {
55 12
        $values = [];
56 12
        foreach ($tokens_values as $token => $value) {
57
            // convert an array to a CSV string
58 11
            if (is_array($value)) {
59 1
                $value = '"' . implode('", "', $value) . '"';
60
            }
61
62 11
            $values[$token] = $value;
63
        }
64
65
        try {
66 12
            $formatter = new MessageFormatter($locale, $string);
67 11
            if (! $formatter) {
0 ignored issues
show
introduced by
$formatter is of type MessageFormatter, thus it always evaluated to true.
Loading history...
68 11
                $this->throwCannotInstantiateFormatter();
69
            }
70 1
        } catch (\Exception $e) {
71 1
            $this->throwCannotInstantiateFormatter();
72
        }
73
74 11
        $result = $formatter->format($values);
75 11
        if ($result === false) {
76 1
            throw new Exception\CannotFormat(
77 1
                $formatter->getErrorMessage(),
78 1
                $formatter->getErrorCode()
79
            );
80
        }
81
82 10
        return $result;
83
    }
84
85
    /**
86
     *
87
     * Throws exception
88
     *
89
     * @throws Exception\CannotInstantiateFormatter
90
     */
91 1
    protected function throwCannotInstantiateFormatter()
92
    {
93 1
        throw new Exception\CannotInstantiateFormatter(
94
            intl_get_error_message(),
95
            intl_get_error_code()
96
        );
97
    }
98
}
99