Completed
Push — 2.x ( dbb9af...517b3d )
by Hari
02:53
created

Translator::getMessage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 3
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
/**
14
 *
15
 * Translator to translate the message
16
 *
17
 * @package Aura.Intl
18
 *
19
 */
20
class Translator implements TranslatorInterface
21
{
22
    /**
23
     *
24
     * A fallback translator.
25
     *
26
     * @var TranslatorInterface
27
     *
28
     */
29
    protected $fallback;
30
31
    /**
32
     *
33
     * The formatter to use when translating messages.
34
     *
35
     * @var FormatterInterface
36
     *
37
     */
38
    protected $formatter;
39
40
    /**
41
     *
42
     * The locale being used for translations.
43
     *
44
     * @var string
45
     *
46
     */
47
    protected $locale;
48
49
    /**
50
     *
51
     * The message keys and translations.
52
     *
53
     * @var array
54
     *
55
     */
56
    protected $messages = [];
57
58
    /**
59
     *
60
     * Constructor
61
     *
62
     * @param string $locale The locale being used.
63
     *
64
     * @param array $messages The message keys and translations.
65
     *
66
     * @param FormatterInterface $formatter A message formatter.
67
     *
68
     * @param TranslatorInterface $fallback A fallback translator.
69
     *
70
     */
71 5
    public function __construct(
72
        $locale,
73
        array $messages,
74
        FormatterInterface $formatter,
75
        TranslatorInterface $fallback = null
76
    ) {
77 5
        $this->locale    = $locale;
78 5
        $this->messages  = $messages;
79 5
        $this->formatter = $formatter;
80 5
        $this->fallback  = $fallback;
81 5
    }
82
83
    /**
84
     *
85
     * Gets the message translation by its key.
86
     *
87
     * @param string $key The message key.
88
     *
89
     * @return mixed The message translation string, or false if not found.
90
     *
91
     */
92 4
    protected function getMessage($key)
93
    {
94 4
        if (isset($this->messages[$key])) {
95 3
            return $this->messages[$key];
96
        }
97
98 3
        if ($this->fallback) {
99
            // get the message from the fallback translator
100 1
            $message = $this->fallback->getMessage($key);
101
            // speed optimization: retain locally
102 1
            $this->messages[$key] = $message;
103
            // done!
104 1
            return $message;
105
        }
106
107
        // no local message, no fallback
108 2
        return false;
109
    }
110
111
    /**
112
     *
113
     * Translates the message indicated by they key, replacing token values
114
     * along the way.
115
     *
116
     * @param string $key The message key.
117
     *
118
     * @param array $tokens_values Token values to interpolate into the
119
     * message.
120
     *
121
     * @return string The translated message with tokens replaced.
122
     *
123
     */
124 4
    public function translate($key, array $tokens_values = [])
125
    {
126
        // get the message string
127 4
        $message = $this->getMessage($key);
128
129
        // do we have a message string?
130 4
        if (! $message) {
131
            // no, return the message key as-is
132 2
            $message = $key;
133 2
        }
134
135
        // are there token replacement values?
136 4
        if (! $tokens_values) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tokens_values of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
137
            // no, return the message string as-is
138 3
            return $message;
139
        }
140
141
        // run message string through formatter to replace tokens with values
142 2
        return $this->formatter->format($this->locale, $message, $tokens_values);
143
    }
144
}
145