CombinedFilter::addLocale()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace CodeZero\BrowserLocale\Filters;
4
5
use CodeZero\BrowserLocale\Locale;
6
7
class CombinedFilter implements Filter
8
{
9
    /**
10
     * The filtered results.
11
     *
12
     * @var array
13
     */
14
    protected $filtered;
15
16
    /**
17
     * Filter the locales.
18
     *
19
     * @param array $locales
20
     *
21
     * @return array
22
     */
23 2
    public function filter(array $locales)
24
    {
25 2
        $this->filtered = [];
26
27 2
        foreach ($locales as $locale) {
28 1
            $this->filterLocale($locale);
29
        }
30
31 2
        return $this->filtered;
32
    }
33
34
    /**
35
     * Filter the given Locale.
36
     *
37
     * @param \CodeZero\BrowserLocale\Locale $locale
38
     *
39
     * @return void
40
     */
41 1
    protected function filterLocale(Locale $locale)
42
    {
43 1
        $language = substr($locale->locale, 0, 2);
44
45 1
        $this->addLocale($locale->locale);
46 1
        $this->addLocale($language);
47 1
    }
48
49
    /**
50
     * Add a locale to the results array.
51
     *
52
     * @param string $locale
53
     *
54
     * @return void
55
     */
56 1
    protected function addLocale($locale)
57
    {
58 1
        if ( ! in_array($locale, $this->filtered)) {
59 1
            $this->filtered[] = $locale;
60
        }
61 1
    }
62
}
63