Localization::getLocales()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php namespace Mascame\Artificer;
2
3
use Mascame\Artificer\Options\AdminOption;
4
use Str;
5
6
class Localization
7
{
8
9
    /**
10
     * @var \Closure
11
     */
12
    protected $lang_closure;
13
14
    /**
15
     * @var array
16
     */
17
    protected $locales = array();
18
19
20
    public function __construct()
21
    {
22
        $closure = AdminOption::get('localization.lang_detection');
23
        $this->locales = $this->getConfigLocales();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getConfigLocales() of type * is incompatible with the declared type array of property $locales.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
24
25
        if ($closure && Artificer::isClosure($closure)) {
26
            $this->lang_closure = $closure;
27
        }
28
    }
29
30
    public function getConfigLocales()
31
    {
32
        return (!empty($this->locales)) ? $this->locales : $this->locales = AdminOption::get('localization.locales');
0 ignored issues
show
Documentation Bug introduced by
It seems like \Mascame\Artificer\Optio...'localization.locales') of type * is incompatible with the declared type array of property $locales.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
    }
34
35
    /**
36
     * @return array
37
     */
38
    public function getLocales()
39
    {
40
        $this->locales = $this->getConfigLocales();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getConfigLocales() of type * is incompatible with the declared type array of property $locales.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
42
        if (is_array($this->locales)) {
43
            return array_keys($this->locales);
44
        }
45
46
        return array();
47
    }
48
49
    public function getLocaleNative($locale)
50
    {
51
        $this->locales = $this->getConfigLocales();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getConfigLocales() of type * is incompatible with the declared type array of property $locales.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
53
        if (is_array($this->locales[$locale])) {
54
            return $this->locales[$locale]['native'];
55
        }
56
57
        return array();
58
    }
59
60
    /**
61
     * @param $column
62
     * @return bool|int|string
63
     */
64
    public function parseColumnLang($column)
65
    {
66
        if ($this->lang_closure) {
67
            return $this->lang_closure($column);
0 ignored issues
show
Bug introduced by
The method lang_closure() does not seem to exist on object<Mascame\Artificer\Localization>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
        }
69
70
        return $this->detectColumnLang($column);
71
    }
72
73
    /**
74
     * @param $column
75
     * @return bool|int|string
76
     */
77
    protected function detectColumnLang($column)
78
    {
79
        foreach ($this->getLanguageEndings() as $locale => $ending) {
80
            if (Str::endsWith($column, $ending)) {
81
                return $locale;
82
            }
83
        }
84
85
        return false;
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    protected function getLanguageEndings()
92
    {
93
        $endings = array();
94
95
        foreach ($this->getLocales() as $locale) {
96
            $endings[$locale] = '_' . $locale;
97
        }
98
99
        return $endings;
100
    }
101
}