UriDetectorDriver   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A routePrefix() 0 14 4
A getSegments() 0 4 1
A getAliasesToLocale() 0 19 2
1
<?php
2
3
namespace Vluzrmos\LanguageDetector\Drivers;
4
5
use Vluzrmos\LanguageDetector\Contracts\ShouldPrefixRoutesInterface as PrefixRoutes;
6
7
/**
8
 * Class UriDetectorDriver.
9
 */
10
class UriDetectorDriver extends SubdomainDetectorDriver implements PrefixRoutes
11
{
12
    /**
13
     * Minimun parts of the uri.
14
     *
15
     * @var int
16
     */
17
    protected $minParts = 1;
18
19
    /**
20
     * Get the prefix for routes based on a given locale.
21
     *
22
     * @param string $locale
23
     * @return string
24
     */
25
    public function routePrefix($locale)
26
    {
27
        $parts = $this->getSegments();
28
29
        if ($this->isPartsValid($parts) &&
30
            $parts[$this->getDefaultSegment()] == $locale
31
        ) {
32
            return $locale;
33
        }
34
35
        $aliases = $this->getAliasesToLocale($locale);
36
37
        return $aliases ? array_shift($aliases) : '';
38
    }
39
40
    /**
41
     * Get parts of the url.
42
     *
43
     * @return array
44
     */
45
    public function getSegments()
46
    {
47
        return array_filter(preg_split('/\//', $this->request->path()));
48
    }
49
50
    /**
51
     * Array of aliases to a given locale.
52
     *
53
     * @param $locale
54
     * @return array
55
     */
56
    protected function getAliasesToLocale($locale)
57
    {
58
        $aliases = array_keys($this->languages, $locale);
59
60
        $parts = $this->getSegments();
61
62
        if ($this->isPartsValid($parts)) {
63
            $segment = $parts[$this->getDefaultSegment()];
64
65
            return array_filter(
66
                $aliases,
67
                function ($item) use ($segment) {
68
                    return $segment == $item;
69
                }
70
            );
71
        }
72
73
        return [];
74
    }
75
}
76