Completed
Push — master ( 812692...5d2ed7 )
by Ivan
07:38
created

Localizer::localeIsAcceptable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace CodeZero\Localizer;
4
5
use CodeZero\Localizer\Detectors\Detector;
6
use Illuminate\Support\Facades\App;
7
8
class Localizer
9
{
10
    /**
11
     * Supported locales.
12
     *
13
     * @var \Illuminate\Support\Collection|array
14
     */
15
    protected $locales;
16
17
    /**
18
     * \CoderZero\Localizer\Detectors\Detector instances.
19
     *
20
     * @var \Illuminate\Support\Collection|array
21
     */
22
    protected $detectors;
23
24
    /**
25
     * \CoderZero\Localizer\Stores\Store instances.
26
     *
27
     * @var \Illuminate\Support\Collection|array
28
     */
29
    protected $stores;
30
31
    /**
32
     * Create a new Localizer instance.
33
     *
34
     * @param \Illuminate\Support\Collection|array $locales
35
     * @param \Illuminate\Support\Collection|array $detectors
36
     * @param \Illuminate\Support\Collection|array $stores
37
     */
38 15
    public function __construct($locales, $detectors, $stores = [])
39
    {
40 15
        $this->locales = $locales;
41 15
        $this->detectors = $detectors;
42 15
        $this->stores = $stores;
43 15
    }
44
45
    /**
46
     * Detect any supported locale and return the first match.
47
     *
48
     * @return string|false
49
     */
50 14
    public function detect()
51
    {
52 14
        foreach ($this->detectors as $detector) {
53 14
            $detector = $this->getInstance($detector);
54 14
            $locales = (array) $detector->detect();
55
56 14
            foreach ($locales as $locale) {
57 14
                if ($this->localeIsAcceptable($locale, $detector)) {
58 14
                    return $locale;
59
                }
60
61
            }
62
        }
63
64 1
        return false;
65
    }
66
67
    /**
68
     * Store the given locale.
69
     *
70
     * @param string $locale
71
     *
72
     * @return void
73
     */
74 9
    public function store($locale)
75
    {
76 9
        foreach ($this->stores as $store) {
77 9
            $this->getInstance($store)->store($locale);
78
        }
79 9
    }
80
81
    /**
82
     * Determine if the given locale may be returned.
83
     *
84
     * @param string $locale
85
     * @param \CodeZero\Localizer\Detectors\Detector $detector
86
     *
87
     * @return bool
88
     */
89 14
    protected function localeIsAcceptable($locale, Detector $detector)
90
    {
91 14
        if (empty($locale)) {
92 1
            return false;
93
        }
94
95 14
        return ! $detector->shouldValidate() || $this->isSupportedLocale($locale);
96
    }
97
98
    /**
99
     * Check if the given locale is supported.
100
     *
101
     * @param mixed $locale
102
     *
103
     * @return bool
104
     */
105 11
    protected function isSupportedLocale($locale)
106
    {
107 11
        return in_array($locale, $this->locales);
108
    }
109
110
    /**
111
     * Get the class from Laravel's IOC container if it is a string.
112
     *
113
     * @param mixed $class
114
     *
115
     * @return mixed
116
     */
117 15
    protected function getInstance($class)
118
    {
119 15
        if (is_string($class)) {
120 8
            return App::make($class);
121
        }
122
123 7
        return $class;
124
    }
125
}
126