Completed
Pull Request — master (#309)
by David
05:04
created

SimpleProvider::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 3
dl 0
loc 17
ccs 9
cts 10
cp 0.9
crap 4.016
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TranslationFormBundle package.
7
 *
8
 * (c) David ALLIX <http://a2lix.fr>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace A2lix\TranslationFormBundle\Locale;
15
16
class SimpleProvider implements LocaleProviderInterface
17
{
18
    /** @var array */
19
    protected $locales;
20
    /** @var string */
21
    protected $defaultLocale;
22
    /** @var array */
23
    protected $requiredLocales;
24
25 13
    public function __construct(array $locales, string $defaultLocale, array $requiredLocales = [])
26
    {
27 13
        if (!in_array($defaultLocale, $locales, true)) {
28 1
            if (count($locales)) {
29 1
                throw new \InvalidArgumentException(sprintf('Default locale `%s` not found within the configured locales `[%s]`. Perhaps you need to add it to your `a2lix_translation_form.locales` bundle configuration?', $defaultLocale, implode(',', $locales)));
30
            }
31
32
            throw new \InvalidArgumentException(sprintf('No locales were configured, but expected at least the default locale `%s`. Perhaps you need to add it to your `a2lix_translation_form.locales` bundle configuration?', $defaultLocale));
33
        }
34
35 13
        if (array_diff($requiredLocales, $locales)) {
36 1
            throw new \InvalidArgumentException('Required locales should be contained in locales');
37
        }
38
39 13
        $this->locales = $locales;
40 13
        $this->defaultLocale = $defaultLocale;
41 13
        $this->requiredLocales = $requiredLocales;
42 13
    }
43
44 10
    public function getLocales(): array
45
    {
46 10
        return $this->locales;
47
    }
48
49 10
    public function getDefaultLocale(): string
50
    {
51 10
        return $this->defaultLocale;
52
    }
53
54 9
    public function getRequiredLocales(): array
55
    {
56 9
        return $this->requiredLocales;
57
    }
58
}
59