Passed
Pull Request — master (#3)
by Artem
02:51
created

Territory::getLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Sokil\IsoCodes\Database\Territory;
4
5
class Territory
6
{
7
    /**
8
     * @var string
9
     */
10
    private $alpha2;
11
12
    /**
13
     * @var array
14
     */
15
    private $languages;
16
17
    /**
18
     * @var array
19
     */
20
    private $sortedLanguages;
21
22
    /**
23
     * @var array
24
     */
25
    private $officialLanguages;
26
27
    /**
28
     * @var array
29
     */
30
    private $unofficialLanguages;
31
32
    /**
33
     * Territory constructor.
34
     *
35
     * @param string $alpha2
36
     * @param array $languages
37
     */
38
    public function __construct(
39
        $alpha2,
40
        $languages
41
    ) {
42
        $this->alpha2 = $alpha2;
43
        $this->languages = $languages;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getAlpha2()
50
    {
51
        return $this->alpha2;
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function getLanguages()
58
    {
59
        return $this->languages;
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getSortedLanguages()
66
    {
67
        if ($this->sortedLanguages === null) {
0 ignored issues
show
introduced by
The condition $this->sortedLanguages === null can never be true.
Loading history...
68
            $arr = $this->languages;
69
            ksort($arr);
70
            $arr = array_map(function ($k, $v) {
71
                $v['language'] = $k;
72
73
                return $v;
74
            }, array_keys($arr), array_values($arr));
75
            usort($arr, function ($x, $y) {
76
                if ($x['percent'] === $y['percent']) {
77
                    return 0;
78
                }
79
80
                return $x['percent'] > $y['percent'] ? -1 : 1;
81
            });
82
            $this->sortedLanguages = array_combine(array_map(function ($x) {
83
                return $x['language'];
84
            }, $arr), $arr);
85
        }
86
87
        return $this->sortedLanguages;
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function getOfficialLanguages()
94
    {
95
        if ($this->officialLanguages === null) {
0 ignored issues
show
introduced by
The condition $this->officialLanguages === null can never be true.
Loading history...
96
            $this->officialLanguages = array_keys(array_filter($this->getSortedLanguages(), function ($v) {
97
                return $v['official'];
98
            }));
99
        }
100
101
        return $this->officialLanguages;
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    public function getUnofficialLanguages()
108
    {
109
        if ($this->unofficialLanguages === null) {
0 ignored issues
show
introduced by
The condition $this->unofficialLanguages === null can never be true.
Loading history...
110
            $this->officialLanguages = array_keys(array_filter($this->getSortedLanguages(), function ($v) {
111
                return !$v['official'];
112
            }));
113
        }
114
115
        return $this->unofficialLanguages;
116
    }
117
}
118