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

Base   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.89%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 45
ccs 16
cts 18
cp 0.8889
rs 10
c 1
b 0
f 0
wmc 7
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A switchLocale() 0 7 1
A removeLocale() 0 7 1
A prefix() 0 12 3
A addLocale() 0 4 1
A locale() 0 4 1
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