Completed
Push — master ( f0cb91...c4f1d3 )
by Ivan
02:22
created

CombinedFilter::filter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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