SubdomainDetectorDriver::isPartsValid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Vluzrmos\LanguageDetector\Drivers;
4
5
/**
6
 * Class SubdomainDetectorDriver.
7
 */
8
class SubdomainDetectorDriver extends AbstractDetector
9
{
10
    /**
11
     * Minimun parts of the subdomain.
12
     *
13
     * @var int
14
     */
15
    protected $minParts = 3;
16
17
    /**
18
     * Return detected language.
19
     *
20
     * @return string
21
     */
22
    public function detect()
23
    {
24
        $parts = $this->getSegments();
25
26
        $locale = null;
27
28
        if ($this->isPartsValid($parts)) {
29
            $locale = $parts[$this->getDefaultSegment()];
30
        }
31
32
        return $locale ? $this->getAliasedLocale($locale) : null;
33
    }
34
35
    /**
36
     * Get parts of the subdomain.
37
     *
38
     * @return array
39
     */
40
    public function getSegments()
41
    {
42
        return preg_split('/\./', $this->request->getHost());
43
    }
44
45
    /**
46
     * Check if parts of the url are valid.
47
     *
48
     * @param array $parts
49
     * @return bool
50
     */
51
    public function isPartsValid(array $parts)
52
    {
53
        return count($parts) >= $this->minParts;
54
    }
55
}
56