Passed
Push — master ( d12f46...58955b )
by Ben
01:03
created

ApplicationLocale::equals()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
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 75
    private function __construct(Locale $originalLocale, Config $config)
16
    {
17 75
        $this->originalLocale = $originalLocale;
18 75
        $this->config = $config;
19 75
    }
20
21 75
    public static function from($originalLocale)
22
    {
23 75
        if (is_string($originalLocale)) {
24 33
            $originalLocale = Locale::from($originalLocale);
25
        }
26
27 75
        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 73
    public function get(): Locale
36
    {
37 73
        $locale = $this->originalLocale;
38
39 73
        $convert_locales = $this->config->get('convert_locales');
40 73
        $conversions = $this->config->get('convert_locales_to', []);
41
42 73
        if ('auto' === $convert_locales) {
43 3
            $locale = isset($conversions[$locale->get()])
44 1
                ? Locale::from($conversions[$locale->get()])
45 3
                : $locale->withoutRegion();
46 70
        } elseif (true === $convert_locales && isset($conversions[$locale->get()])) {
47 3
            $locale = Locale::from($conversions[$locale->get()]);
48
        }
49
50 73
        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 70
    public function __toString(): string
59
    {
60 70
        return $this->get()->get();
61
    }
62
}
63