Completed
Push — master ( 017917...362f74 )
by Frédéric
02:12
created

UrlLocalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CaribouFute\LocaleRoute\Routing;
4
5
use Config;
6
use Illuminate\Translation\Translator;
7
8
class UrlLocalizer
9 5
{
10
    protected $translator;
11 5
12
    public function __construct(Translator $translator)
13
    {
14 2
        $this->translator = $translator;
15
    }
16 2
17
    public function addLocaleConfig()
18
    {
19 2
        return Config::get('localeroute.add_locale_to_url');
20
    }
21 2
22
    public function getRouteUrl($locale, $route, array $urls = [])
23
    {
24
        $unlocaleUrl = isset($urls[$locale]) ? $urls[$locale] : $this->translator->get('routes.' . $route, [], $locale);
25
        $url = $this->addLocale($locale, $unlocaleUrl);
26
        return $url;
27
    }
28
29
    public function addLocale($locale, $url)
30
    {
31
        return $this->addLocaleConfig() ? $locale . '/' . $url : $url;
32
    }
33
34
    public function removeLocale($locale, $url)
35
    {
36
        return $this->addLocaleConfig() ? str_replace($locale . '/', '', $url) : $url;
37
    }
38
}
39