ApplicationLocale::get()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 0
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 5
rs 9.3888
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Locale\Values;
6
7
class ApplicationLocale
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
8
{
9
    /** @var Config */
10
    private $config;
11
12
    /** @var Locale */
13
    private $originalLocale;
14
15 79
    private function __construct(Locale $originalLocale, Config $config)
16
    {
17 79
        $this->originalLocale = $originalLocale;
18 79
        $this->config = $config;
19 79
    }
20
21 79
    public static function from($originalLocale)
22
    {
23 79
        if (is_string($originalLocale)) {
24 36
            $originalLocale = Locale::from($originalLocale);
25
        }
26
27 79
        return new static($originalLocale, Config::from(app('config')->get('thinktomorrow.locale', [])));
28
    }
29
30
    /**
31
     * Convert locale to application locale.
32
     *
33
     * @return Locale
34
     */
35 77
    public function get(): Locale
36
    {
37 77
        $locale = $this->originalLocale;
38
39 77
        $convert_locales = $this->config->get('convert_locales');
40 77
        $conversions = $this->config->get('convert_locales_to', []);
41
42 77
        if ('auto' === $convert_locales) {
43 3
            $locale = isset($conversions[$locale->get()])
44 1
                ? Locale::from($conversions[$locale->get()])
45 3
                : $locale->withoutRegion();
46 74
        } elseif (true === $convert_locales && isset($conversions[$locale->get()])) {
47 3
            $locale = Locale::from($conversions[$locale->get()]);
48
        }
49
50 77
        return $locale;
51
    }
52
53 1
    public function equals(self $other): bool
54
    {
55 1
        return get_class($this) === get_class($other) && (string) $this === (string) $other;
56
    }
57
58 74
    public function __toString(): string
59
    {
60 74
        return $this->get()->get();
61
    }
62
}
63