Completed
Pull Request — 2.x (#27)
by
unknown
02:09
created

TranslatorLocator::getPackages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
 * A ServiceLocator implementation for loading and retaining translator objects.
16
 *
17
 * @package Aura.Intl
18
 *
19
 */
20
class TranslatorLocator
21
{
22
    /**
23
     *
24
     * A registry to retain translator objects.
25
     *
26
     * @var array
27
     *
28
     */
29
    protected $registry;
30
31
    /**
32
     *
33
     * The current locale code.
34
     *
35
     * @var string
36
     *
37
     */
38
    protected $locale;
39
40
    /**
41
     *
42
     * A translator factory.
43
     *
44
     * @var TranslatorFactory
45
     *
46
     *
47
     */
48
    protected $factory;
49
50
    /**
51
     *
52
     * A package locator.
53
     *
54
     * @var PackageLocator
55
     *
56
     */
57
    protected $packages;
58
59
    /**
60
     *
61
     * A formatter locator.
62
     *
63
     * @var FormatterInterface
64
     *
65
     */
66
    protected $formatters;
67
68
    /**
69
     *
70
     * Constructor.
71
     *
72
     * @param PackageLocator $packages The package locator.
73
     *
74
     * @param FormatterLocator $formatters The formatter locator.
75
     *
76
     * @param TranslatorFactory $factory A translator factory to
77
     * create translator objects for the locale and package.
78
     *
79
     * @param string $locale The default locale code to use.
80
     *
81
     */
82 8
    public function __construct(
83
        PackageLocator $packages,
84
        FormatterLocator $formatters,
85
        TranslatorFactory $factory,
86
        $locale
87
    ) {
88 8
        $this->packages = $packages;
89 8
        $this->factory = $factory;
90 8
        $this->formatters = $formatters;
0 ignored issues
show
Documentation Bug introduced by
It seems like $formatters of type object<Aura\Intl\FormatterLocator> is incompatible with the declared type object<Aura\Intl\FormatterInterface> of property $formatters.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
91 8
        $this->setLocale($locale);
92 8
    }
93
94
    /**
95
     *
96
     * Sets the default locale code.
97
     *
98
     * @param string $locale The new locale code.
99
     *
100
     * @return void
101
     *
102
     */
103 8
    public function setLocale($locale)
104
    {
105 8
        $this->locale = $locale;
106 8
    }
107
108
    /**
109
     *
110
     * Returns the default locale code.
111
     *
112
     * @return string
113
     *
114
     */
115 2
    public function getLocale()
116
    {
117 2
        return $this->locale;
118
    }
119
120
    /**
121
     *
122
     * The TranslatorFactory object
123
     *
124
     * @return TranslatorFactory
125
     */
126 1
    public function getFactory()
127
    {
128 1
        return $this->factory;
129
    }
130
131
    /**
132
     *
133
     * An object of type PackageLocator
134
     *
135
     * @return PackageLocator
136
     *
137
     */
138 1
    public function getPackages()
139
    {
140 1
        return $this->packages;
141
    }
142
143
    /**
144
     *
145
     * An object of type FormatterLocator
146
     *
147
     * @return FormatterLocator
148
     *
149
     */
150 1
    public function getFormatters()
151
    {
152 1
        return $this->formatters;
153
    }
154
155
    /**
156
     *
157
     * Gets a translator from the registry by package for a locale.
158
     *
159
     * @param string $name The translator package to retrieve.
160
     *
161
     * @param string $locale The locale to use; if empty, uses the default
162
     * locale.
163
     *
164
     * @return TranslatorInterface A translator object.
165
     *
166
     */
167 2
    public function get($name, $locale = null)
168
    {
169 2
        if (! $name) {
170 2
            return null;
171
        }
172
173 2
        if (! $locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
174 1
            $locale = $this->getLocale();
175 1
        }
176
177 2
        if (! isset($this->registry[$name][$locale])) {
178
179
            // get the package descriptor
180 2
            $package = $this->packages->get($name, $locale);
181
182
            // build a translator; note the recursive nature of the
183
            // 'fallback' param at the very end.
184 2
            $translator = $this->factory->newInstance(
185 2
                $locale,
186 2
                $package,
187 2
                $this->formatters->get($package->getFormatter()),
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Aura\Intl\FormatterInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
188 2
                $this->get($package->getFallback(), $locale)
189 2
            );
190
191
            // retain in the registry
192 2
            $this->registry[$name][$locale] = $translator;
193 2
        }
194
195 2
        return $this->registry[$name][$locale];
196
    }
197
}
198