@@ -54,7 +54,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
54
54
/**
55
55
* @var MessageFormatterInterface[] An array of formatters
56
56
*/
57
- private $ formatters ;
57
+ private $ formatters = array () ;
58
58
59
59
/**
60
60
* @var string
@@ -99,7 +99,9 @@ public function __construct($locale, $formatters = null, $cacheDir = null, $debu
99
99
$ formatters = [new TransMessageFormatter ($ formatters )];
100
100
}
101
101
102
- $ this ->formatters = $ formatters ;
102
+ foreach ($ formatters as $ formatter ) {
103
+ $ this ->formatters [$ formatter ->getName ()] = $ formatter ;
104
+ }
103
105
$ this ->cacheDir = $ cacheDir ;
104
106
$ this ->debug = $ debug ;
105
107
}
@@ -257,7 +259,7 @@ public function getCatalogue($locale = null)
257
259
*/
258
260
public function addFormatter (MessageFormatterInterface $ formatter )
259
261
{
260
- $ this ->formatters [] = $ formatter ;
262
+ $ this ->formatters [$ formatter -> getName () ] = $ formatter ;
261
263
}
262
264
263
265
/**
@@ -475,13 +477,10 @@ private function getConfigCacheFactory()
475
477
private function getFormatter ($ name )
476
478
{
477
479
$ name = $ name ?: $ this ->defaultFormatter ;
478
-
479
- foreach ($ this ->formatters as $ formatter ) {
480
- if ($ name === $ formatter ->getName ()) {
481
- return $ formatter ;
482
- }
480
+ if (!isset ($ this ->formatters [$ name ])) {
481
+ throw new \InvalidArgumentException (sprintf ('The formatter with type "%s" is not registered. ' , $ name ));
483
482
}
484
483
485
- throw new \ InvalidArgumentException ( sprintf ( ' The formatter with type "%s" is not registered. ' , $ name)) ;
484
+ return $ this -> formatters [ $ name] ;
486
485
}
487
486
}
0 commit comments