Passed
Push — master ( e00fc3...99b25d )
by Ben
02:47
created

Locale::region()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Locale\Values;
4
5
final class Locale
6
{
7
    /**
8
     * @var string Locale key identifier
9
     */
10
    private $value;
11
12 91
    private function __construct(string $value)
13
    {
14 91
        $this->value = $value;
15 91
    }
16
17 91
    public static function from(string $value)
18
    {
19 91
        return new static($value);
20
    }
21
22 3
    public function withoutRegion()
23
    {
24 3
        $value = $this->value;
25
26 3
        if($region = $this->region()) {
27 3
            $value = str_replace(['-' . $region, '_' . $region], '', $value);
28
        }
29
30 3
        return new static($value);
31
    }
32
33 4
    public function region(): ?string
34
    {
35 4
        $value = $this->value;
36
37 4
        if (false !== strpos($value, '-')) {
38 4
            return substr($value, strpos($value, '-')+1);
39
        }
40
41 3
        if (false !== strpos($value, '_')) {
42 3
            return substr($value, strpos($value, '_')+1);
43
        }
44
45 3
        return null;
46
    }
47
48 75
    public function get(): string
49
    {
50 75
        return $this->value;
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 73
    public function __toString(): string
59
    {
60 73
        return $this->get();
61
    }
62
}
63