Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cef7950

Browse files
committed
register formatters by name
1 parent 99eb699 commit cef7950

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/Symfony/Component/Translation/Translator.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
5454
/**
5555
* @var MessageFormatterInterface[] An array of formatters
5656
*/
57-
private $formatters;
57+
private $formatters = array();
5858

5959
/**
6060
* @var string
@@ -99,7 +99,9 @@ public function __construct($locale, $formatters = null, $cacheDir = null, $debu
9999
$formatters = [new TransMessageFormatter($formatters)];
100100
}
101101

102-
$this->formatters = $formatters;
102+
foreach ($formatters as $formatter) {
103+
$this->formatters[$formatter->getName()] = $formatter;
104+
}
103105
$this->cacheDir = $cacheDir;
104106
$this->debug = $debug;
105107
}
@@ -257,7 +259,7 @@ public function getCatalogue($locale = null)
257259
*/
258260
public function addFormatter(MessageFormatterInterface $formatter)
259261
{
260-
$this->formatters[] = $formatter;
262+
$this->formatters[$formatter->getName()] = $formatter;
261263
}
262264

263265
/**
@@ -475,13 +477,10 @@ private function getConfigCacheFactory()
475477
private function getFormatter($name)
476478
{
477479
$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));
483482
}
484483

485-
throw new \InvalidArgumentException(sprintf('The formatter with type "%s" is not registered.', $name));
484+
return $this->formatters[$name];
486485
}
487486
}

0 commit comments

Comments
 (0)