Passed
Push — master ( fe8484...90b95a )
by butschster
17:12 queued 18s
created

TranslatorConfig   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 36
dl 0
loc 141
ccs 37
cts 37
cp 1
rs 10
c 1
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getDirectories() 0 3 1
A __construct() 0 4 1
A getFallbackLocale() 0 3 1
A resolveDomain() 0 15 4
A getLocaleDirectory() 0 7 2
A getDefaultLocale() 0 3 1
A isAutoRegisterMessages() 0 3 2
A hasLoader() 0 3 1
A getLoader() 0 5 1
A getLocalesDirectory() 0 3 1
A getDumper() 0 5 1
A hasDumper() 0 3 1
A getDefaultDomain() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Translator\Config;
6
7
use Spiral\Core\InjectableConfig;
8
use Spiral\Translator\Matcher;
9
use Symfony\Component\Translation\Dumper\DumperInterface;
10
use Symfony\Component\Translation\Loader\LoaderInterface;
11
12
final class TranslatorConfig extends InjectableConfig
13
{
14
    /**
15
     * Configuration section.
16
     */
17
    public const CONFIG = 'translator';
18
19
    /**
20
     * @var array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
21
     *     locale: string,
22
     *     fallbackLocale?: string,
23
     *     directory: non-empty-string,
24
     *     directories: array<array-key, non-empty-string>,
25
     *     localesDirectory?: non-empty-string,
26
     *     registerMessages?: bool,
27
     *     cacheLocales: bool,
28
     *     autoRegister: bool,
29
     *     domains: array<non-empty-string, array<string>>,
30
     *     loaders: class-string<LoaderInterface>[],
31
     *     dumpers: class-string<DumperInterface>[]
32
     * }
33
     */
34
    protected array $config = [
35
        'locale'         => '',
36
        'directory'      => '',
37
        'directories'    => [],
38
        'cacheLocales'   => true,
39
        'autoRegister'   => true,
40
        'domains'        => [],
41
        'loaders'        => [],
42
        'dumpers'        => [],
43
    ];
44
45
    private readonly Matcher $matcher;
46
47 78
    public function __construct(array $config = [])
48
    {
49 78
        parent::__construct($config);
50 78
        $this->matcher = new Matcher();
0 ignored issues
show
Bug introduced by
The property matcher is declared read-only in Spiral\Translator\Config\TranslatorConfig.
Loading history...
51
    }
52
53
    /**
54
     * Default translation domain.
55
     */
56 23
    public function getDefaultDomain(): string
57
    {
58 23
        return 'messages';
59
    }
60
61 47
    public function getDefaultLocale(): string
62
    {
63 47
        return $this->config['locale'] ?? '';
64
    }
65
66 19
    public function getFallbackLocale(): string
67
    {
68 19
        return $this->config['fallbackLocale'] ?? $this->getDefaultLocale();
69
    }
70
71 17
    public function isAutoRegisterMessages(): bool
72
    {
73 17
        return !empty($this->config['autoRegister']) || !empty($this->config['registerMessages']);
74
    }
75
76
    /**
77
     * Returns application locales directory.
78
     *
79
     * @return non-empty-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
80
     */
81 51
    public function getLocalesDirectory(): string
82
    {
83 51
        return $this->config['localesDirectory'] ?? $this->config['directory'] ?? '';
84
    }
85
86
    /**
87
     * Returns additional locales directories.
88
     *
89
     * @return array<array-key, non-empty-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, non-empty-string> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, non-empty-string>.
Loading history...
90
     */
91 48
    public function getDirectories(): array
92
    {
93 48
        return $this->config['directories'] ?? [];
94
    }
95
96
    /**
97
     * @param non-empty-string $locale
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
98
     * @param non-empty-string|null $directory
99
     *
100
     * @return non-empty-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
101
     */
102 48
    public function getLocaleDirectory(string $locale, ?string $directory = null): string
103
    {
104 48
        if ($directory !== null) {
105 45
            return \rtrim($directory, '/') . '/' . $locale . '/';
106
        }
107
108 14
        return \trim($this->getLocalesDirectory(), '/') . '/' . $locale . '/';
109
    }
110
111
    /**
112
     * Get domain name associated with given bundle.
113
     */
114 30
    public function resolveDomain(string $bundle): string
115
    {
116 30
        $bundle = \strtolower(\str_replace(['/', '\\'], '-', $bundle));
117 30
        $domains = (array) ($this->config['domains'] ?? []);
118
119 30
        foreach ($domains as $domain => $patterns) {
120 30
            foreach ($patterns as $pattern) {
121 30
                if ($this->matcher->matches($bundle, $pattern)) {
122 29
                    return $domain;
123
                }
124
            }
125
        }
126
127
        //We can use bundle itself as domain
128 1
        return $bundle;
129
    }
130
131 41
    public function hasLoader(string $extension): bool
132
    {
133 41
        return isset($this->config['loaders'][$extension]);
134
    }
135
136 41
    public function getLoader(string $extension): LoaderInterface
137
    {
138 41
        $class = $this->config['loaders'][$extension];
139
140 41
        return new $class();
141
    }
142
143 2
    public function hasDumper(string $dumper): bool
144
    {
145 2
        return isset($this->config['dumpers'][$dumper]);
146
    }
147
148 2
    public function getDumper(string $dumper): DumperInterface
149
    {
150 2
        $class = $this->config['dumpers'][$dumper];
151
152 2
        return new $class();
153
    }
154
}
155