Completed
Branch feature/router-decorator (f7629b)
by Frédéric
02:12
created

Base::removeLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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