Completed
Push — master ( 5edddb...1f4f42 )
by Saif Eddin
10:22
created

TranslatorFactory::addResources()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 4
nc 4
nop 2
1
<?php
2
/**
3
 * This File is Part of the Validus Translation package.
4
 *
5
 * @copyright (c) 2018 Validus <https://github.com/ValidusPHP/>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Validus\Translation;
14
15
use InvalidArgumentException;
16
use Psr\Container\ContainerInterface;
17
use Symfony\Component\Translation\Formatter\MessageFormatter;
18
use Symfony\Component\Translation\Translator;
19
use Symfony\Component\Translation\TranslatorInterface;
20
use function array_merge;
21
22
class TranslatorFactory
23
{
24
    /**
25
     * @param ContainerInterface $container
26
     *
27
     * @return TranslatorInterface
28
     */
29
    public function __invoke(ContainerInterface $container): TranslatorInterface
30
    {
31
        $config = $container->has('config') ? $container->get('config') : [];
32
        $debug = $config['debug'] ?? true;
33
34
        $config = array_merge($this->getDefaultConfigurations(), $config['translation'] ?? []);
35
36
        $formatter = $container->has($config['formatter']) ? $container->get($config['formatter']) : new $config['formatter']();
37
38
        $cache = $debug ? null : $config['cache_dir'];
39
        $translator = new Translator($config['locale'], $formatter, $cache, $debug);
40
41
        $loaders = $config['loaders'];
42
43
        foreach ($loaders as $format => $loader) {
44
            if (\is_string($loader)) {
45
                $loader = $container->has($loader) ? $container->get($loader) : new $loader();
46
            }
47
            $translator->addLoader($format, $loader);
48
        }
49
50
        $this->addResources($translator, $config['resources']);
51
52
        $translator->setFallbackLocales($config['fallback']);
53
54
        $translator->setLocale($config['locale']);
55
56
        return $translator;
57
    }
58
59
    /**
60
     * Add resources to the given translator instance.
61
     *
62
     * @param Translator $translator
63
     * @param array      $resources
64
     */
65
    protected function addResources(Translator $translator, array $resources = []): void
66
    {
67
        foreach ($resources as $id => $resource) {
68
            if (!isset($resource['format'])) {
69
                throw new InvalidArgumentException("resource format is missing from resources configuration#{$id}.");
70
            }
71
72
            if (!isset($resource['resource'])) {
73
                throw new InvalidArgumentException("resource is missing from resources configuration#{$id}.");
74
            }
75
76
            $resource['locale'] = $resource['locale'] ?? $config['locale'] ?? 'en';
77
            $resource['domain'] = $resource['domain'] ?? 'messages';
78
79
            $translator->addResource($resource['format'], $resource['resource'], $resource['locale'], $resource['domain']);
80
        }
81
    }
82
83
    /**
84
     * get the default Translator configurations.
85
     *
86
     * @return array
87
     */
88
    protected function getDefaultConfigurations(): array
89
    {
90
        return [
91
            'locale' => 'en',
92
            'cache_dir' => null,
93
            'fallback' => [],
94
            'formatter' => MessageFormatter::class,
95
            'loaders' => [],
96
            'resources' => [],
97
        ];
98
    }
99
}
100