Base::switchLocale()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace CaribouFute\LocaleRoute\Prefix;
4
5
use CaribouFute\LocaleRoute\LocaleConfig;
6
use Illuminate\Support\Str;
7
8
abstract class Base
9
{
10
    protected $localeConfig;
11
    protected $separator;
12
13 61
    public function __construct(LocaleConfig $localeConfig)
14
    {
15 61
        $this->localeConfig = $localeConfig;
16
    }
17
18 41
    public function switchLocale($locale, $string)
19
    {
20 41
        $unlocalized = $this->removeLocale($string);
21 41
        $localized = $this->addLocale($locale, $unlocalized);
22
23 41
        return $localized;
24
    }
25
26 45
    public function removeLocale($string)
27
    {
28 45
        $prefix = $this->prefix($string);
29 45
        $unlocalized = str_replace($prefix, '', $string);
30
31 45
        return $unlocalized;
32
    }
33
34 48
    public function prefix($string)
35
    {
36 48
        foreach ($this->localeConfig->locales() as $locale) {
37 48
            $prefix = $locale . $this->separator;
38
39 48
            if (Str::startsWith($string, $prefix)) {
40 7
                return $prefix;
41
            }
42
        }
43
44 43
        return '';
45
    }
46
47 44
    public function addLocale($locale, $string)
48
    {
49 44
        return $locale . $this->separator . $string;
50
    }
51
52 1
    public function locale($string)
53
    {
54 1
        return rtrim($this->prefix($string), $this->separator);
55
    }
56
}
57