Completed
Pull Request — master (#8)
by Ben
04:07
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 75
        return new static($originalLocale, Config::from(app('config')->get('thinktomorrow.locale', [])));
27
    }
28
29
    /**
30
     * Convert locale to application locale
31
     *
32
     * @return Locale
33
     */
34 73
    public function get(): Locale
35
    {
36 73
        $locale = $this->originalLocale;
37
38 73
        $convert_locales = $this->config->get('convert_locales');
39 73
        $conversions = $this->config->get('convert_locales_to', []);
40
41 73
        if('auto' === $convert_locales) {
42 3
            $locale = isset($conversions[$locale->get()])
43 1
                ? Locale::from($conversions[$locale->get()])
44 3
                : $locale->withoutRegion();
45
        }
46 70
        else if(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
}