LocaleProvider::getLocales()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Koff\Bundle\I18nFormBundle\Provider;
4
5
/**
6
 * Class LocaleProvider.
7
 *
8
 * @author Gonzalo Vilaseca <[email protected]> . Reiss Clothing Ltd.
9
 */
10
class LocaleProvider implements LocaleProviderInterface
11
{
12
    /** @var array */
13
    protected $locales;
14
15
    /** @var string */
16
    protected $defaultLocale;
17
18
    /** @var array */
19
    protected $requiredLocales;
20
21
    public function __construct(array $locales, string $defaultLocale, array $requiredLocales = [])
22
    {
23
        if (empty($locales)) {
24
            throw new \InvalidArgumentException('No locales were configured, but expected at least one locale. Check `i18n_form.locales` bundle configuration!');
25
        }
26
27
        if (!\in_array($defaultLocale, $locales, true)) {
28
            throw new \InvalidArgumentException(sprintf('Default locale `%s` not found within the configured locales `[%s]`. Perhaps you need to add it to your `i18n_form.locales` bundle configuration?', $defaultLocale, implode(',', $locales)));
29
        }
30
31
        if (array_diff($requiredLocales, $locales)) {
32
            throw new \InvalidArgumentException('Required locales should be contained in locales');
33
        }
34
35
        $this->locales = $locales;
36
        $this->defaultLocale = $defaultLocale;
37
        $this->requiredLocales = $requiredLocales;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getLocales(): array
44
    {
45
        return $this->locales;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getDefaultLocale(): string
52
    {
53
        return $this->defaultLocale;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getRequiredLocales(): array
60
    {
61
        return $this->requiredLocales;
62
    }
63
}
64