Passed
Push — master ( 893d1e...31005e )
by Kirill
03:35
created

TranslatorConfig::hasLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Translator\Config;
13
14
use Spiral\Core\InjectableConfig;
15
use Spiral\Translator\Matcher;
16
use Symfony\Component\Translation\Dumper\DumperInterface;
17
use Symfony\Component\Translation\Loader\LoaderInterface;
18
19
final class TranslatorConfig extends InjectableConfig
20
{
21
    /**
22
     * Configuration section.
23
     */
24
    public const CONFIG = 'translator';
25
26
    /**
27
     * @var array
28
     */
29
    protected $config = [
30
        'locale'         => '',
31
        'fallbackLocale' => '',
32
        'directory'      => '',
33
        'cacheLocales'   => true,
34
        'autoRegister'   => true,
35
        'domains'        => [],
36
        'loaders'        => [],
37
        'dumpers'        => []
38
    ];
39
40
    /** @var Matcher */
41
    private $matcher = null;
42
43
    public function __construct(array $config = [])
44
    {
45
        parent::__construct($config);
46
        $this->matcher = new Matcher();
47
    }
48
49
    /**
50
     * Default translation domain.
51
     *
52
     * @return string
53
     */
54
    public function getDefaultDomain(): string
55
    {
56
        return 'messages';
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getDefaultLocale(): string
63
    {
64
        return $this->config['locale'];
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function getFallbackLocale(): string
71
    {
72
        return $this->config['fallbackLocale'] ?? $this->config['locale'];
73
    }
74
75
    /**
76
     * @return bool
77
     */
78
    public function isAutoRegisterMessages(): bool
79
    {
80
        return !empty($this->config['autoRegister']) || !empty($this->config['registerMessages']);
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getLocalesDirectory(): string
87
    {
88
        return $this->config['localesDirectory'] ?? $this->config['directory'];
89
    }
90
91
    /**
92
     * @param string $locale
93
     * @return string
94
     */
95
    public function getLocaleDirectory(string $locale): string
96
    {
97
        return $this->getLocalesDirectory() . $locale . '/';
98
    }
99
100
    /**
101
     * Get domain name associated with given bundle.
102
     *
103
     * @param string $bundle
104
     * @return string
105
     */
106
    public function resolveDomain(string $bundle): string
107
    {
108
        $bundle = strtolower(str_replace(['/', '\\'], '-', $bundle));
109
110
        foreach ($this->config['domains'] as $domain => $patterns) {
111
            foreach ($patterns as $pattern) {
112
                if ($this->matcher->matches($bundle, $pattern)) {
113
                    return $domain;
114
                }
115
            }
116
        }
117
118
        //We can use bundle itself as domain
119
        return $bundle;
120
    }
121
122
    /**
123
     * @param string $extension
124
     * @return bool
125
     */
126
    public function hasLoader(string $extension): bool
127
    {
128
        return isset($this->config['loaders'][$extension]);
129
    }
130
131
    /**
132
     * @param string $extension
133
     * @return LoaderInterface
134
     */
135
    public function getLoader(string $extension): LoaderInterface
136
    {
137
        $class = $this->config['loaders'][$extension];
138
139
        return new $class();
140
    }
141
142
    /**
143
     * @param string $dumper
144
     * @return bool
145
     */
146
    public function hasDumper(string $dumper): bool
147
    {
148
        return isset($this->config['dumpers'][$dumper]);
149
    }
150
151
    /**
152
     * @param string $dumper
153
     * @return DumperInterface
154
     */
155
    public function getDumper(string $dumper): DumperInterface
156
    {
157
        $class = $this->config['dumpers'][$dumper];
158
159
        return new $class();
160
    }
161
}
162